Reputation: 495
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 listView?
Upvotes: 1
Views: 1780
Reputation: 719
"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 :
Upvotes: 0
Reputation: 114
This is the example i used.. you can change it and use it.
// ListView code
public class FevList extends ExpandableListActivity {
String title;
String url;
SQLiteDatabase database;
DbUtil db;
HashMap<String, String> map = new HashMap<String, String>();
ArrayList<HashMap<String, String>> subjectList = new ArrayList<HashMap<String, String>>();
@SuppressWarnings("unchecked")
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.exlist);
db = new DbUtil();
database = db.openConnection(this);
// Thread for getting values of title from DataBase.
SimpleExpandableListAdapter expListAdapter = new SimpleExpandableListAdapter(
this, createGroupList(), R.layout.group_row,
new String[] { "subject" }, new int[] { R.id.row_name },
createChildList(), R.layout.child_row,
new String[] { "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 = new ArrayList<HashMap<String, String>>();
@SuppressWarnings("unchecked")
private List createGroupList() {
// write your code here.
return (List) result;
}
/* creatin the HashMap for the children */
@SuppressWarnings("unchecked")
private List createChildList() {
// write your code here.
return result;
}
public void onContentChanged() {
System.out.println("onContentChanged");
super.onContentChanged();
}
/* This function is called on each child click */
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// write your code here.
return true;
}
/* This function is called on expansion of the group */
public void onGroupExpand(int groupPosition) {
try {
System.out.println("Group exapanding Listener => groupPosition = "
+ groupPosition);
} catch (Exception e) {
System.out.println(" groupPosition Errrr +++ " + e.getMessage());
}
}
}
Upvotes: 1
Reputation: 451
Try something like this:
public class PharmaciesListScreen extends ExpandableListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ExpandableListView expandbleLis = getExpandableListView();
List<RegionItem> regionsList = new RegionsDBUtils(getApplication())
.getAllRegions();
PharmaciesListAdapter mNewAdapter = new PharmaciesListAdapter(regionsList);
mNewAdapter
.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE), this);
getExpandableListView().setAdapter(mNewAdapter);
}
Adapter code:
public class PharmaciesListAdapter extends BaseExpandableListAdapter {
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
//set up here you child elements.
//you can use inflater here
}
@Override
public 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;
}
public void setInflater(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
Upvotes: 0