Changing Listview To Expandablelistview
I have a ListView in my Activity but now I want to change it to an ExpandableListView. Do I need to change my listview completely or can I add expandableListView to my existing lis
Solution 1:
This is the example i used.. you can change it and use it.
// ListView code
publicclassFevListextendsExpandableListActivity {
String title;
String url;
SQLiteDatabase database;
DbUtil db;
HashMap<String, String> map = newHashMap<String, String>();
ArrayList<HashMap<String, String>> subjectList = newArrayList<HashMap<String, String>>();
@SuppressWarnings("unchecked")
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.exlist);
db = newDbUtil();
database = db.openConnection(this);
// Thread for getting values of title from DataBase.SimpleExpandableListAdapter expListAdapter = newSimpleExpandableListAdapter(
this, createGroupList(), R.layout.group_row,
newString[] { "subject" }, new int[] { R.id.row_name },
createChildList(), R.layout.child_row,
newString[] { "title" }, new int[] { R.id.grp_child });
setListAdapter(expListAdapter);
registerForContextMenu(getExpandableListView());
} catch (Exception e) {
e.printStackTrace();
}
}
// code for adapter/* Creating the Hashmap for the row */ArrayList<HashMap<String, String>> result = newArrayList<HashMap<String, String>>();
@SuppressWarnings("unchecked")
privateListcreateGroupList() {
// write your code here.return (List) result;
}
/* creatin the HashMap for the children */@SuppressWarnings("unchecked")
privateListcreateChildList() {
// write your code here.return result;
}
publicvoidonContentChanged() {
System.out.println("onContentChanged");
super.onContentChanged();
}
/* This function is called on each child click */publicbooleanonChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// write your code here.returntrue;
}
/* This function is called on expansion of the group */publicvoidonGroupExpand(int groupPosition) {
try {
System.out.println("Group exapanding Listener => groupPosition = "
+ groupPosition);
} catch (Exception e) {
System.out.println(" groupPosition Errrr +++ " + e.getMessage());
}
}
}
Solution 2:
Try something like this:
publicclassPharmaciesListScreenextendsExpandableListActivity {
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ExpandableListViewexpandbleLis= getExpandableListView();
List<RegionItem> regionsList = newRegionsDBUtils(getApplication())
.getAllRegions();
PharmaciesListAdaptermNewAdapter=newPharmaciesListAdapter(regionsList);
mNewAdapter
.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE), this);
getExpandableListView().setAdapter(mNewAdapter);
}
Adapter code:
publicclassPharmaciesListAdapterextendsBaseExpandableListAdapter {
@Overridepublic View getChildView(int groupPosition, finalint childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
//set up here you child elements.//you can use inflater here
}
@Overridepublic View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = minflater.inflate(R.layout.grouprow, null);
}
((CheckedTextView) convertView).setText(mRegionsList.get(groupPosition).region);
((CheckedTextView) convertView).setChecked(isExpanded);
return convertView;
}
publicvoidsetInflater(LayoutInflater mInflater, Activity act) {
this.minflater = mInflater;
activity = act;
}
}
Note: in adapter you'll need to implement more methods. Also, take a look at this post: Android ExpandableListView - Looking for a tutorial
Solution 3:
"Here is a pretty good example for ExpandableListView"
ExpandableListView inherits from ListView. It add more things to the ListView implementation like expanding and collapsing property, header and child views etc. When moving from listView to expandable list view you'll need to make changes in :
- XML view
- Adapter to implement ExpandableListAdapter
- While Setting the adapter you'll need to new ExpandableListAdapter(this, list of DataHeader for each list, list of DataChild that you want to populate beneath each header);
Post a Comment for "Changing Listview To Expandablelistview"