问题
i am trying to do checkbox in expanablelistview (in groupview and child view). But i am not understanding this concept. so guys if u have any idea please share it with me. This is the first time i am working in this concept(expanablelistview).
check box in groupview (CheckBox checkBoxGroup)
- if i check checkbox in groupview(groupposition 0) all item in child(childposition 0) must get check.
- same as if i uncheck groupview(groupposition 0)all item in child(childposition 0) must get uncheck.
check box in childview (CheckBox checkBoxChild;)
- just a multiple selection in checkbox.
thank u for ur kind helps.
here is my code below
public class DetailAdapter extends BaseExpandableListAdapter {
private LayoutInflater mInflater;
private ArrayList<PatientSampleDetails> sampleArray;
private ArrayList<lstServiceOrderDetails> serviceLst;
private Activity activity;
ArrayAdapter<String> adapter;
View row;
public LayoutInflater minflater;
Context mContext;
int chldPosition;
// ServiceOrderList elementChild
PatientSampleDetails elementChild;
public static HashMap<Integer,Boolean> selectedGroupMembers = new HashMap<Integer, Boolean>();
private static View container ;
ActionItem nextItem;
int posi;
lstServiceOrderDetails child;
View rowView;
static class ViewHolder {
private CheckBox checkBoxChild;
public CheckBox checkBoxGroup;
public Button button;
public TextView service;
}
public WorkDetailAdapter(Activity activity, ArrayList<PatientSampleDetails> sampleArray) {
this.sampleArray = sampleArray;
this.activity = activity;
mInflater = LayoutInflater.from(activity);
}
public void setInflater(LayoutInflater mInflater) {
this.minflater = mInflater;
}
public Object getChild(int groupPosition, int childPosition) {
return sampleArray.get(groupPosition).lstServiceOrderDetails.get(childPosition);
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
Log.i("getChildView-getChild", groupPosition + "/" + childPosition);
child = (lstServiceOrderDetails) getChild(groupPosition, childPosition);
Log.i("child", "child data"+ child.getServiceName());
rowView = convertView;
final ViewHolder holder = new ViewHolder();
if (convertView == null) {
convertView = mInflater.inflate(R.layout.detaillistrowview, null);
holder.serviceText = (TextView) convertView.findViewById(R.id.sevice);
holder.checkBoxChild = (CheckBox)convertView.findViewById(R.id.checkBox);
holder.checkBoxChild.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
elementChild = (PatientSampleDetails) holder.checkBoxChild.getTag();
elementChild.setSelected(buttonView.isChecked());
Log.i("buttonView-checkBox", "getChildId :" + getChildId(groupPosition, childPosition));
Log.i("status", ""+ elementChild.isSelected());
} else {
elementChild.setSelected(false);
}
}
});
if (selectedGroupMembers.containsKey(groupPosition) == true){
holder.checkBoxChild.setActivated(true);
Log.i("true", "true");
holder.checkBoxChild.setSelectAllOnFocus(true);
} else {
holder.checkBoxChild.setActivated(false);
Log.i("false", "false");
holder.checkBoxChild.setSelectAllOnFocus(true);
}
convertView.setTag(holder);
holder.checkBoxChild.setTag(sampleArray.get(groupPosition));
holder.serviceText.setText(Util.formatN2H(child.getServiceName()));
Log.i("child", "child data ---> "+ child.getServiceName() +"/"+child.getServiceName());
//Now write here code for set colors for rows
/* if(childPosition % 2 == 0) {
convertView.setBackgroundColor(Color.parseColor("#E7E8E8"));
} else {
convertView.setBackgroundColor(Color.parseColor("#C9CACC"));
}*/
// To change the color of the Listview row.
// / convertView.setBackgroundColor((childPosition & 1) == 1 ? Color.parseColor("#E7E8E8") : Color.parseColor("#C9CACC"));
return convertView;
}
public int getChildrenCount(int groupPosition) {
serviceLst = sampleArray.get(groupPosition).getServiceOrderList();
return serviceLst.size();
}
public Object getGroup(int groupPosition) {
return sampleArray.get(groupPosition);
}
public int getGroupCount() {
return sampleArray.size();
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
final ViewHolder holder = new ViewHolder();
PatientSampleDetails group = (PatientSampleDetails) getGroup(groupPosition);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.getgroupviewdetail, null);
convertView.setId(groupPosition);
holder.checkBoxGroup = (CheckBox) convertView.findViewById(R.id.checkBoxGroup);
holder.checkBoxGroup.setFocusable(false);
holder.checkBoxGroup.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
PatientSampleDetails elementGroup ;
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked == true) {
selectedGroupMembers.put(groupPosition, Boolean.valueOf(true)); //new Boolean(true));
elementGroup = (PatientSampleDetails) holder.checkBoxGroup.getTag();
elementGroup.setSelected(buttonView.isChecked());
Log.i("Clicked : ", "group" + groupPosition);
Log.i("buttonView", "getItemId :" + getGroupId(groupPosition));
Log.i("status", ""+ elementGroup.isSelected());
Log.i("buttonView", "chldPosition :" + getChildId(groupPosition, chldPosition));
notifyDataSetChanged();
} else if (selectedGroupMembers.containsKey(groupPosition)) {
selectedGroupMembers.remove(groupPosition);
elementGroup.setSelected(false);
Log.i(" Removed : ", "group" + groupPosition);
notifyDataSetChanged();
}
}
});
convertView.setTag(holder);
holder.checkBoxGroup.setTag(sampleArray.get(groupPosition));
} else {
((ViewHolder) convertView.getTag()).checkBoxGroup.setTag(sampleArray.get(groupPosition));
}
return convertView;
}
public boolean hasStableIds() {
return false;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
回答1:
Try this,Adapter
will be refreshed each and every minutes when you scrolling your List.You must set tag for each and every item, else part you will get tag value
if (convertView == null) {
//Add ur code
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
it will identify unique value in your ListView
.
回答2:
take an arraylist on application context to save state that will be used to save current status of your list. first time get data from webservice or anywhere and populate it.
Pass this arraylist to adapter to populate list.
on groupview checkbox changed update that list. and call notifyDatasetChanged.
来源:https://stackoverflow.com/questions/14575652/how-to-do-checkbox-in-expanablelistview-in-groupview-and-child-view