package com.wm.example;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
public class ExpandableListViewCheckbox extends Activity implements OnClickListener{
private static final String TAG="ExpandableListViewCheckboxActivity";
private Button mBtn;
private ExpandableListView mListView;
private ExpandableListViewAdapter mListViewAdapter = null;
private List<String> mGroupData = new ArrayList<String>();
private List<HashMap<Integer,String>> mChildData= new ArrayList<HashMap<Integer,String>>();
//control select button status(SelectedAll or UnselectAll)
private Boolean mAllSelected = false;
private List<HashMap<Integer,Boolean>> mCheckedObj = new ArrayList<HashMap<Integer,Boolean>>();
private int mAllSelectedSize = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mBtn=(Button) findViewById(R.id.select_btn);
mBtn.setOnClickListener(this);
mListView = (ExpandableListView) findViewById(R.id.expandableListView);
mListView.setGroupIndicator(null);
InitData();
mListView.setOnGroupClickListener(new OnGroupClickListener(){
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// TODO Auto-generated method stub
return false;
}
});
mListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
//Log.w(TAG,"********onChildClick*********");
mListViewAdapter.setItemCheckBoxStatus(v, groupPosition,childPosition);
if(mCheckedObj.get(groupPosition).containsKey(childPosition)){
// Log.w(TAG,"****onItemClick***NEED REMOVE***");
mCheckedObj.get(groupPosition).remove(childPosition);
}
else{
// Log.w(TAG,"****onItemClick***NEED put***");
mCheckedObj.get(groupPosition).put(childPosition, true);
}
if(getCheckedObjSize() == 0){
// Log.w(TAG,"****onItemClick**NO CHECKED***");
mAllSelected = false;
mBtn.setText("SelectAll");
}
else if(getCheckedObjSize() == mAllSelectedSize){
mAllSelected = true;
mBtn.setText("UnselectAll");
}
return false;
}
});
mListViewAdapter = new ExpandableListViewAdapter(getLayoutInflater(),this,mGroupData,mChildData);
mListView.setAdapter(mListViewAdapter);
}
private HashMap<Integer,String> setHashMap(int size,String mstring){
HashMap<Integer,String> temp = new HashMap<Integer,String>();
for(int i=0;i< size; ++i){
temp.put(i, mstring+i);
}
return temp;
}
private void InitData(){
mGroupData.add("aaa");
mChildData.add(setHashMap(2,"a"));
mGroupData.add("bbb");
mChildData.add(setHashMap(4,"b"));
mGroupData.add("ccc");
mChildData.add(setHashMap(3,"c"));
mGroupData.add("ddd");
mChildData.add(setHashMap(1,"d"));
mGroupData.add("eee");
mChildData.add(setHashMap(2,"e"));
mGroupData.add("fff");
mChildData.add(setHashMap(5,"f"));
mGroupData.add("ggg");
mChildData.add(setHashMap(1,"g"));
for(int groupId = 0; groupId < mGroupData.size();++groupId){
HashMap<Integer,Boolean> temp = new HashMap<Integer,Boolean>();
for(int childId = 0; childId < mChildData.get(groupId).size();++childId){
mAllSelectedSize++;
}
mCheckedObj.add(temp);
}
}
private int getCheckedObjSize(){
int size =0;
for(int groupId = 0; groupId < mGroupData.size();++groupId){
size += mCheckedObj.get(groupId).size();
}
return size;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.select_btn:{
if(mAllSelected){
mAllSelected = false;
mBtn.setText("SelectAll");
mListViewAdapter.setAllCheckBoxStatus(false);
for(int groupId = 0; groupId < mGroupData.size();++groupId){
mCheckedObj.get(groupId).clear();
}
}
else{
mAllSelected = true;
mBtn.setText("UnselectAll");
mListViewAdapter.setAllCheckBoxStatus(true);
for(int groupId = 0; groupId < mGroupData.size();++groupId){
for(int childId = 0; childId < mChildData.get(groupId).size();++childId){
mCheckedObj.get(groupId).put(childId, true);
}
}
}
//Refresh data
mListViewAdapter.notifyDataSetChanged();
}
break;
default:break;
}
}
}
- 1
- 2
- 3
- 4
- 5
前往页