ANDROID - ExpandableListView?

I assume, you have your data structured somehow, like: ArrayList where Parent {name:String, checked:boolean, children:ArrayList} and Child {name:String} If so, you only have to do two things: create proper layouts for your parent item renderer and child item renderer expand BaseExpandableListAdapter to build up the list yourself Here is a small sample about what's in my mind: layout/grouprow. Xml :? Xml version="1.0" encoding="utf-8"?

> parents after you assign a value to this member / load the list of parents, you should also attach your adapter to this view: this. SetListAdapter(new MyELAdapter()) and that's it. You have a checkable-expandable list, and in the CheckUpdateListener s onCheckedChanged(CompoundButton buttonView, boolean isChecked) method you can update your parent object's checked state Note, that the background color is determined in the getGroupView method, so you don't have to change it anywhere, just call the adapter's notifyDataSetChanged() method if needed Update you can download the sample source code from this link It is an eclipse project, but if you are using other developer environment, just simply copy the necessary source files (java + xml).

I assume, you have your data structured somehow, like: ArrayList where Parent {name:String, checked:boolean, children:ArrayList} and Child {name:String} If so, you only have to do two things: create proper layouts for your parent item renderer and child item renderer expand BaseExpandableListAdapter to build up the list yourself. Here is a small sample about what's in my mind: layout/grouprow. Xml: layout/childrow.

Xml: MyELAdapter class: I've declared this as an inner class of MyExpandableList, so I could reach the list of parents directly, without having to pass it as parameter. Private class MyELAdapter extends BaseExpandableListAdapter { private LayoutInflater inflater; public MyELAdapter() { inflater = LayoutInflater. From(MyExpandableList.

This); } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parentView) { final Parent parent = parents. Get(groupPosition); convertView = inflater. Inflate(R.layout.

Grouprow, parentView, false); ((TextView) convertView. FindViewById(R.id. Parentname)).

SetText(parent.getName()); CheckBox checkbox = (CheckBox) convertView. FindViewById(R.id. Checkbox); checkbox.

SetChecked(parent.isChecked()); checkbox. SetOnCheckedChangeListener(new CheckUpdateListener(parent)); if (parent.isChecked()) convertView. SetBackgroundResource(R.color.

Red); else convertView. SetBackgroundResource(R.color. Blue); return convertView; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parentView) { final Parent parent = parents.

Get(groupPosition); final Child child = parent.getChildren(). Get(childPosition); convertView = inflater. Inflate(R.layout.

Childrow, parentView, false); ((TextView) convertView. FindViewById(R.id. Childname)).

SetText(child.getName()); return convertView; } @Override public Object getChild(int groupPosition, int childPosition) { return parents. Get(groupPosition).getChildren(). Get(childPosition); } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public int getChildrenCount(int groupPosition) { return parents.

Get(groupPosition).getChildren().size(); } @Override public Object getGroup(int groupPosition) { return parents. Get(groupPosition); } @Override public int getGroupCount() { return parents.size(); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public void notifyDataSetChanged() { super. NotifyDataSetChanged(); } @Override public boolean isEmpty() { return ((parents == null) || parents.isEmpty()); } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } @Override public boolean hasStableIds() { return true; } @Override public boolean areAllItemsEnabled() { return true; } } You must already have a public class MyExpandableList extends ExpandableListActivity which has a member: private ArrayList parents; after you assign a value to this member / load the list of parents, you should also attach your adapter to this view: this.

SetListAdapter(new MyELAdapter()); and that's it. You have a checkable-expandable list, and in the CheckUpdateListener's onCheckedChanged(CompoundButton buttonView, boolean isChecked) method you can update your parent object's checked state. Note, that the background color is determined in the getGroupView method, so you don't have to change it anywhere, just call the adapter's notifyDataSetChanged() method if needed.

Update you can download the sample source code from this link. It is an eclipse project, but if you are using other developer environment, just simply copy the necessary source files (java + xml).

Thanx rekaszeru. Since im a begginer would you care to share a working sample? – no9 Apr 13 at 8:21 sure thing, i'll just need a couple of minutes to build up one.It would be usefull though if i'd know from where you get the data (server or local).

– rekaszeru Apr 13 at 8:53 if you can, please build a very simple example (just some test data with bunch of arrays). I am getting the data tru sort of XML service designed for the compact framework treeview. I will have to format the data myself, yet at this stage im not sure yet how.

Right now im interested in the structure of expendable list itself. Checkboxes, radiobuttons etc – no9 Apr 13 at 9:41 check out the update, there is a link to the sample project. – rekaszeru Apr 13 at 10:55 thanx alot rekaszeru!

If I may ask one more question... How would you customize children if you wanted the pattern I put in my question. Under one parent I want multiselection on children (checkboxes) in another parent I want a singleselection on children (radio buttons). Nevertheless im acception your anwser because you put your time and knowledge to help a newbie!

– no9 Apr 13 at 11:14.

I have checked it already and I can say that this one does not work. It has many bugs, for instance it looks weird, second of the checkboxes do not work properly and parent is not checkable. – no9 Apr 13 at 6:20.

I'm planning to add expandablelistview in my activity. I did some researches on the Internet but none of them fits what I want. I read also that expandablelistview has parent and child elements.

What I want to do is that the parent would be the categories let's say it's fruits, then its children are Apple, Lemon, Almonds, etc. I'd like to try that by using arrays, but am not so familiar with List and Hash.. Please give me some snippets of codes to start with.. appreciate any help.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions