【发布时间】:2014-02-01 04:22:48
【问题描述】:
我已经阅读了大约 30 页的 SO 以及有关在列表中跟踪检查状态的教程,但是在可扩展列表视图中这样做的信息(尤其是工作信息)很少。
我让孩子们填充了复选框,但是当我选中 1 组中的孩子的框时,其他组中的随机孩子也会检查。我需要阻止这一切。我认为我阅读的最佳信息是将复选框设置为单独的标签,但我不知道如何设置多个标签,当我尝试时,将复选框与转换视图进行比较时出现类不匹配错误。
有没有人遇到过跟踪可展开列表视图子视图中复选框状态的好方法?
我进行了多项更改,因此我将放入整个适配器代码。请检查 getGroupView 的前几行和整个 getChildView 并帮助我看看我做错了什么。
编辑: 现在发生的情况是,当我选中一个框然后展开另一个组时,所有选中的框都取消选中:
public class MyExpandableListAdapter extends BaseExpandableListAdapter
implements OnCheckedChangeListener {
private Context mContext;
private ArrayList<ContactNameItems> mListDataHeader;
private HashMap<String, List<ContactPhoneItems>> mListDataChild;
private boolean[] mGetChecked;
private HashMap<String, boolean[]> mChildCheckStates;
private ArrayList<String> selectedNumbers;
private ChildViewHolder childViewHolder;
private GroupViewHolder groupViewHolder;
private String numberText;
public MyExpandableListAdapter(Context context,
ArrayList<ContactNameItems> listDataHeader,
HashMap<String, List<ContactPhoneItems>> listDataChild,
ArrayList<String> listOfNumbers) {
mContext = context;
mListDataHeader = listDataHeader;
mListDataChild = listDataChild;
selectedNumbers = listOfNumbers;
mChildCheckStates = new HashMap<String, boolean[]>();
}
@Override
public int getGroupCount() {
return mListDataHeader.size();
}
@Override
public ContactNameItems getGroup(int groupPosition) {
return mListDataHeader.get(groupPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String contactName = getGroup(groupPosition).getName();
Bitmap contactImage = getGroup(groupPosition).getImage();
mGetChecked = new boolean[getChildrenCount(groupPosition)];
mChildCheckStates.put(contactName, mGetChecked);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.contact_name_item, null);
groupViewHolder = new GroupViewHolder();
groupViewHolder.mContactName = (TextView) convertView
.findViewById(R.id.lblListHeader);
groupViewHolder.mContactImage = (ImageView) convertView
.findViewById(R.id.ivContactPhoto);
convertView.setTag(groupViewHolder);
} else {
groupViewHolder = (GroupViewHolder) convertView.getTag();
}
if (contactImage != null) {
groupViewHolder.mContactImage.setImageBitmap(contactImage);
} else {
groupViewHolder.mContactImage
.setImageResource(R.drawable.default_contact);
}
groupViewHolder.mContactName.setText(contactName);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return mListDataChild.get(mListDataHeader.get(groupPosition).getName())
.size();
}
@Override
public ContactPhoneItems getChild(int groupPosition, int childPosition) {
return mListDataChild.get(mListDataHeader.get(groupPosition).getName())
.get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String contactName = getGroup(groupPosition).getName();
final int mChildPosition = childPosition;
numberText = getChild(groupPosition, childPosition).getNumber();
String typeText = getChild(groupPosition, childPosition).getPhoneType();
mGetChecked = mChildCheckStates.get(contactName);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.contact_detail_item, null);
childViewHolder = new ChildViewHolder();
childViewHolder.mPhoneNumber = (TextView) convertView
.findViewById(R.id.tv_phone_number);
childViewHolder.mPhoneType = (TextView) convertView
.findViewById(R.id.tv_phone_type);
childViewHolder.mCheckBox = (CheckBox) convertView
.findViewById(R.id.checkBox);
convertView.setTag(R.layout.contact_detail_item, childViewHolder);
} else {
childViewHolder = (ChildViewHolder) convertView
.getTag(R.layout.contact_detail_item);
}
childViewHolder.mPhoneNumber.setText(numberText);
childViewHolder.mPhoneType.setText(typeText);
childViewHolder.mCheckBox.setChecked(mGetChecked[mChildPosition]);
childViewHolder.mCheckBox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
boolean isChecked = childViewHolder.mCheckBox.isChecked();
Log.d("Debug", "isChecked = " + String.valueOf(isChecked));
if (isChecked) {
selectedNumbers.add(numberText);
} else {
selectedNumbers.remove(numberText);
}
childViewHolder.mCheckBox.setChecked(isChecked);
mGetChecked[mChildPosition] = isChecked;
mChildCheckStates.put(contactName, mGetChecked);
}
});
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
@Override
public boolean hasStableIds() {
return false;
}
public ArrayList<String> getSelectedNumbers() {
return selectedNumbers;
}
public final class GroupViewHolder {
TextView mContactName;
ImageView mContactImage;
}
public final class ChildViewHolder {
TextView mPhoneNumber;
TextView mPhoneType;
CheckBox mCheckBox;
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
Log.d("Debug", "onCheckChangedListener : " + String.valueOf(isChecked));
}
}
【问题讨论】:
标签: android expandablelistview android-checkbox