Я прочитал около 30 страниц из SO, а также учебные пособия по отслеживанию состояний проверки в списках, но информации (особенно рабочей информации) недостаточно для этого в расширяемом ListView.
У меня есть дети, заполненные флажками, но когда я устанавливаю флажок для ребенка из 1 группы, случайные дети в других группах также проверяют. Мне нужно остановить это. Лучшая информация, которую я прочитал, заключалась в том, чтобы установить флажок как отдельный тег, но я не знаю, как установить несколько тегов, когда я попытался, я получил ошибку несоответствия класса, сравнивая флажок с convertview.
Кто-нибудь сталкивался с хорошим способом отслеживать состояния флажков в дочерних элементах расширяемого списка?
Я внес несколько изменений, поэтому добавляю весь код адаптера. Пожалуйста, проверьте первые несколько строк 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));
}
}