Go Back   Android Forums > Android Development > Application Development
Application Development Dev Lounge for the Coder Folks
Gamers - Check out our new sister sites!
Nintendo Wii U!    |    OUYA - $99 Android System!

test: Reply
 
LinkBack Thread Tools
Old April 24th, 2010, 02:00 PM   #1 (permalink)
Junior Member
Thread Author (OP)
 
Join Date: Apr 2010
Posts: 85
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 16 Times in 15 Posts
Default Android - ExpandableListView is driving me insane.

So, this problem has been driving me to the brink of insanity, and I thought I'd start asking around to see if someone else knows the solution. I have an Android app which requires the use of an ExpandableListView, but I want the children of each group to be checkable. Hence I'm using CheckedTextView as you might expect. The solution needs to have the ability to select multiple children as well. Hence my little bit of code:
Code:
listView.setItemsCanFocus(false);
listView.setChoiceMode(ExpandableListView.CHOICE_MODE_MULTIPLE);
Now, I've scoured the Android developer site and everywhere on the internet I could reasonably look for examples on how this is done. The best I've come up with is an example for a ListView with multiple choice items. I have no problems with this example, and it compiles and works fine. However, when I try to make this work on the inherited ExpandableListView, it all falls to pieces and I can't - however hard I try - select multiple (much less a single) CheckBox in the children.

Does anybody have any idea what I'm doing wrong (code is below)? Is there just no way to have an ExpandableListView "do" multiple choice check boxes? Any help would be appreciated!
Code:
public class Test extends ExpandableListActivity {
	
	final Context mContext = this;
	
	public static final String GROUP_ID = "Group";
	public static final String CHILD_ID = "Child";
	public static final int GROUPS = 2;
	public static final int CHILDREN = 2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		/* Set up the adapter for the expandable list */
		setListAdapter(new SimpleExpandableListAdapter(
				mContext,
				getGroups(),
				android.R.layout.simple_expandable_list_item_1,
				new String[] {GROUP_ID}, new int[] {android.R.id.text1},
				getChildren(),
				android.R.layout.simple_list_item_multiple_choice,
				new String[] {CHILD_ID}, new int[] {android.R.id.text1}));
		
		/* Get the list, and set the choice mode to multiple - why no WORKY? */
		final ExpandableListView listView = getExpandableListView();
		listView.setItemsCanFocus(false);
		listView.setChoiceMode(ExpandableListView.CHOICE_MODE_MULTIPLE);
	}

	/* Generate the names of the groups - not the problem, works fine. */
	public List<Map<String, String>> getGroups() {
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		Map<String, String> map;
		for (int i = 0; i < GROUPS; ++i) {
			map = new HashMap<String, String>();
			map.put(GROUP_ID, "Group " + i);
			list.add(map);	
		}
		return list;
	}
	
	/* Generates the names of the children - not the problem, works fine. */
	public List<List<Map<String, String>>> getChildren() {
		List<List<Map<String, String>>> list = new ArrayList<List<Map<String, String>>>();
		List<Map<String, String>> subList = new ArrayList<Map<String, String>>();
		Map<String, String> map;
		for (int j = 0; j < CHILDREN; ++j) {
			map = new HashMap<String, String>();
			map.put(CHILD_ID, "Child " + j);
			subList.add(map);
		}
		for (int i = 0; i < GROUPS; ++i) {
			list.add(subList);
		}
		return list;
	}
}

Boogs is offline  
Reply With Quote
Sponsors
Old July 20th, 2010, 09:51 PM   #2 (permalink)
New Member
 
Join Date: Jul 2010
Posts: 1
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Skype™ to congli.sw
Default

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ExpandableListView.OnChildClickList ener;

public class ExpandableListChoiceActivity extends ExpandableListActivity {
public static final String GROUP_ID = "Group";
public static final String CHILD_ID = "Child";
public static final int GROUPS = 3;
public static final int CHILDREN = 3;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

/* Set up the adapter for the expandable list */
setListAdapter(new SimpleExpandableListAdapter(
ExpandableListChoiceActivity.this,
getGroups(),
android.R.layout.simple_expandable_list_item_1,
new String[] {GROUP_ID}, new int[] {android.R.id.text1},
getChildren(),
android.R.layout.simple_list_item_multiple_choice,
new String[] {CHILD_ID}, new int[] {android.R.id.text1}));

/* Get the list, and set the choice mode to multiple - why no WORKY? */
final ExpandableListView listView = getExpandableListView();
listView.setItemsCanFocus(false);
listView.setChoiceMode(ExpandableListView.CHOICE_M ODE_MULTIPLE);

Quote:
listView.setOnChildClickListener(cl);
}
Quote:
private OnChildClickListener cl = new OnChildClickListener(){

@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {

int position = groupPosition*(CHILDREN + 1) + childPosition + 1;
if(getExpandableListView().isItemChecked(position) ){
getExpandableListView().setItemChecked(position, false);
}else{
getExpandableListView().setItemChecked(position, true);
}

Toast.makeText(ExpandableListChoiceActivity.this,
"groupPosition:"+
groupPosition
+ "childPosition:" +
childPosition
+ "id:" +
id
+ "position:" +
position
+ "is checked? " +
getExpandableListView().isItemChecked(position),
Toast.LENGTH_SHORT).show();

return false;
}

};
Quote:
Please catch the blue colour section where you are looking forward to.
/* Generate the names of the groups - not the problem, works fine. */
public List<Map<String, String>> getGroups() {
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
Map<String, String> map;
for (int i = 0; i < GROUPS; ++i) {
map = new HashMap<String, String>();
map.put(GROUP_ID, "Group " + i);
list.add(map);
}
return list;
}

/* Generates the names of the children - not the problem, works fine. */
public List<List<Map<String, String>>> getChildren() {
List<List<Map<String, String>>> list = new ArrayList<List<Map<String, String>>>();
List<Map<String, String>> subList = new ArrayList<Map<String, String>>();
Map<String, String> map;
for (int j = 0; j < CHILDREN; ++j) {
map = new HashMap<String, String>();
map.put(CHILD_ID, "Child " + j);
subList.add(map);
}
for (int i = 0; i < GROUPS; ++i) {
list.add(subList);
}
return list;
}
}
congli.sw is offline  
Last edited by congli.sw; July 20th, 2010 at 10:13 PM. Reason: Adjust layout.
Reply With Quote
Old September 8th, 2010, 03:06 PM   #3 (permalink)
New Member
 
Join Date: Sep 2009
Posts: 4
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the code,
only now the checkboxes arent always shown as checked,
and also when expanding and collapsing the list the checked boxes are varying in status... :S
Seems the same problem als found on other forums:
https://groups.google.com/group/android-developers/browse_thread/thread/9cc00ab935611ece
My life with Android :-): Expandable lists and check boxes

I really don't get the behaviour of these checkboxes... :'(
joeyvanderbie is offline  
Reply With Quote
Old January 22nd, 2013, 09:37 AM   #4 (permalink)
New Member
 
Join Date: Jan 2013
Posts: 5
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default


I have just found a tutorial about how to custom expandable list view with image, text and check box from here"
How to create custom ExpandableListView in Android | Develop Android Application
I hope this helpful!
nghien_rbc is offline  
Reply With Quote
Reply


Go Back   Android Forums > Android Development > Application Development
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 03:25 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.