朋友,我正在使用一个可扩展列表视图来显示各种类别及其子项目。我正在使用复选框来选择子项,以便可以从各种类别中选择多个子项。但是在这里,由于意见的循环利用,我面临一个问题。当我展开类别并在其中勾选子项的复选框时,然后在单击另一个类别时,已经选中了某个复选框。我认为这是由于视图的回收而发生的。我想解决这个问题。我也遇到了与recyclerview类似的问题。请在这方面指导我。先感谢您。 我正在粘贴我的适配器代码以供参考-
public class DayBookAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<DayBookData> listDataHeader;
String msubItem,mcategory;
ArrayList<String> selecteditems;
ArrayList<String> selectedcategories;
public DayBookAdapter(Context context, List<DayBookData> listDataHeader, ArrayList<String> selectedItems,ArrayList<String> selectedcategories) {
this._context = context;
this.listDataHeader = listDataHeader;
this.selecteditems = selectedItems;
this.selectedcategories = selectedcategories;
}
@Override
public ArrayList<DayBookDetailsData> getChild(int groupPosition, int childPosititon) {
return this.listDataHeader.get(groupPosition).getCh_list();
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@SuppressLint("InflateParams")
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final DayBookDetailsData childText = listDataHeader.get(groupPosition).getCh_list().get(childPosition);
Boolean checkstatus = childText.isIschecked();
Log.d("checkstatus", "getChildView: "+checkstatus);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.day_book_details_data_item, null);
}
TextView item_txt_sub_cat_id = convertView.findViewById(R.id.item_id);
item_txt_sub_cat_id.setText(childText.getItem_id());
CheckBox item_txt_sub_cat_name = convertView.findViewById(R.id.item_name);
item_txt_sub_cat_name.setText(childText.getItem_name());
item_txt_sub_cat_name.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final CheckBox cb = (CheckBox) v;
msubItem = listDataHeader.get(groupPosition).getCh_list().get(childPosition).getItem_id();
mcategory = listDataHeader.get(groupPosition).getCategory_id();
if (cb.isChecked()) {
childText.setIschecked(true);
selecteditems.add(msubItem);
selectedcategories.add(mcategory);
Log.d("itemschecked", "items -"+selecteditems+"categories-"+selectedcategories);
} else {
childText.setIschecked(false);
selecteditems.remove(msubItem);
selectedcategories.remove(mcategory);
Log.d("itemschecked", "items -"+selecteditems+"categories-"+selectedcategories);
}
}
});
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this.listDataHeader.get(groupPosition).getCh_list().size();
}
@Override
public DayBookData getGroup(int groupPosition) {
return this.listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this.listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@SuppressLint("InflateParams")
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
DayBookData headerTitle = getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.day_book_data_item, null);
}
TextView item_txt_category_id = convertView.findViewById(R.id.category_id);
TextView item_txt_category_name = convertView.findViewById(R.id.category_name);
item_txt_category_id.setText(headerTitle.getCategory_id());
item_txt_category_name.setText(headerTitle.getCategory_name());
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
让我知道您是否还有其他需要。 谢谢。
已编辑- 这是我的子项目的pojo文件,谁能告诉我要进行哪些更改?
public class DayBookDetailsData {
private String item_name;
private String item_id;
public boolean isIschecked() {
return ischecked;
}
public void setIschecked(boolean ischecked) {
this.ischecked = ischecked;
}
boolean ischecked;
public DayBookDetailsData(String item_name, String item_id,boolean ischecked) {
this.item_name = item_name;
this.item_id = item_id;
this.ischecked = ischecked;
}
public String getItem_name() {
return item_name;
}
public String getItem_id() {
return item_id;
}
}
这是我的JSON解析代码,我在其中设置布尔值-
bData.setCategory_id(jObjIn.getString("cat_id"));
bData.setCategory_name(jObjIn.getString("c_name"));
JSONArray jArrItemData = new JSONArray(jObjIn.getString("itemdata"));
for (int j = 0; j < jArrItemData.length(); j++) {
JSONObject jObjItem = new JSONObject(jArrItemData.getString(j));
try {
String item_id = jObjItem.getString("id");
String item_name = jObjItem.getString("item");
Boolean checkstatus = false;
bData.getCh_list().add(new DayBookDetailsData(item_name, item_id,checkstatus));
} catch (JSONException e) {
e.printStackTrace();
}
}
listData.add(bData);
}
exp_listview.setAdapter(new DayBookAdapter(getActivity(), listData,selectedItems,selectedcategory));
我已经按照您的建议更新了文件,但对我没有帮助。请您告诉我我的代码有什么问题。
答案 0 :(得分:0)
在每个子模型对象中保留一个“ isChecked”布尔值,并在您检查/取消选中项目时对其进行更新。然后,在获取视图时,显式设置复选框的选中状态。
回收视图时,最安全的做法是假定回收视图中的每个项目都处于不想要的状态,因此应进行显式设置。
例如:
getChildView(...){
checkbox.setChecked(getChild(groupPosition, childPosition).getCheckedStatus());
}
onChildClick(){
getChild(groupPosition, childPosition).setCheckedStatus(...)
}