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;
}
}