【发布时间】:2019-01-23 10:39:19
【问题描述】:
在我的 ExpandableListView 中,我有一些包含项目的类别。每个项目的末尾都有一个复选框。当我单击此复选框时,状态应更改为已选中,然后该项目应移至列表末尾的另一个(硬编码)类别。应该像我在原始类别中那样选中复选框。
我在示例中找到的移动项目的方式。 但是新类别中未选中项目复选框。相反,将在原始类别中检查(无需单击)已移动项目的旧位置上的项目。
示例(o 表示未选中;x 表示已选中):
启动应用后:
- 第 1 类
item1 o
item2 o
item3o - 第 2 类
item4o
点击类别1中item1的复选框后
- 第 1 类
项目 2 x
item3o - 第 2 类
item4o - 硬编码类别
item1 o
这是我的适配器类
public class MyCustomAdapter extends BaseExpandableListAdapter {
private LayoutInflater inflater;
private ArrayList<Parent> mParent;
private Context Kontext;
private int count = 0;
Children child;
private CheckBox childCheckBox;
public MyCustomAdapter(Context context, ArrayList<Parent> parent) {
mParent = parent;
inflater = LayoutInflater.from(context);
Kontext = context;
}
@Override
//counts the number of group/parent items so the list knows how many times calls getGroupView() method
public int getGroupCount() {
return mParent.size();
}
@Override
//counts the number of children items so the list knows how many times calls getChildView() method
public int getChildrenCount(int groupPosition) {
Log.e("EXPANDABLE LIST", "GROUP " + groupPosition + " " + mParent.get(groupPosition).getArrayChildren()
.size() + "");
return mParent.get(groupPosition).getArrayChildren().size();
}
@Override
//gets the title of each parent/group
public Object getGroup(int i) {
return mParent.get(i).getTitle();
}
@Override
//gets the name of each item
public Object getChild(int i, int i1) {
return mParent.get(i).getArrayChildren().get(i1);
}
@Override
public long getGroupId(int i) {
return i;
}
@Override
public long getChildId(int i, int i1) {
return i1;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public int getChildTypeCount() {
return 6;
}
@Override
//in this method you must set the text to see the parent/group on the list
public View getGroupView(int groupPosition, boolean b, View convertView, ViewGroup viewGroup) {
ViewHolder holder = new ViewHolder();
holder.groupPosition = groupPosition;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item_parent, viewGroup, false);
holder.groupTitle = (TextView) convertView.findViewById(R.id.list_item_text_view);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.groupTitle.setText(getGroup(groupPosition).toString());
//return the entire view
return convertView;
}
@Override
//in this method you must set the text to see the children on the list
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup viewGroup) {
final ViewHolderChild holder;
child = mParent.get(groupPosition).getArrayChildren().get
(childPosition);
if (convertView == null) {
holder = new ViewHolderChild();
convertView = inflater.inflate(R.layout.child_list_layout, viewGroup, false);
holder.childTitle = (TextView) convertView.findViewById(R.id.list_item_text_child);
holder.childCheckBox = (CheckBox) convertView.findViewById((R.id.cbListItemChild));
convertView.setTag(holder);
} else {
holder = (ViewHolderChild) convertView.getTag();
}
holder.childTitle.setText(mParent.get(groupPosition).getArrayChildren().get(childPosition).getArtikelName());
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Kontext.getApplicationContext(), "gekauft : " +mParent.get(groupPosition).getArrayChildren().get
(childPosition).gekauft
+ "", Toast.LENGTH_SHORT).show();
}
});
holder.childCheckBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Kontext.getApplicationContext(), "You have selected item : " +mParent.get(groupPosition).getArrayChildren().get
(childPosition).name
+ "", Toast.LENGTH_SHORT).show();
addChildToGroup(mParent.get(groupPosition).getArrayChildren().get
(childPosition), mParent.size() - 1, "Parent 25 (Hardcoded)");
removeChildFromGroup(groupPosition, childPosition);
notifyDataSetChanged();
}
});
//return the entire view
return convertView;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
public void removeGroup(int groupPosition) {
mParent.remove(groupPosition);
Log.e("removeGroup", "group size " + mParent.size());
notifyDataSetChanged();
}
/**
* Removes child at childPosition from group at groupPosition. If it is the last child in group
* the group will also be deleted.
*
* @param groupPosition Position of the group in which the child exists
* @param childPosition Position of the child in the group
*/
public void removeChildFromGroup(int groupPosition, int childPosition) {
Parent parent = mParent.get(groupPosition);
parent.getArrayChildren().remove(childPosition);
if (parent.getArrayChildren().isEmpty()) {
removeGroup(groupPosition);
} else {
notifyDataSetChanged();
}
}
public void addChildToGroup(Children childName, int groupPosition, String sectionTitle) {
Parent parent = mParent.get(groupPosition);
List<Children> arrayChildren = parent.getArrayChildren();
if (!parent.getTitle().equals(sectionTitle)) {
parent = new Parent();
arrayChildren = new ArrayList<>();
parent.setTitle(sectionTitle);
mParent.add(parent);
}
arrayChildren.add(childName);
parent.setArrayChildren(arrayChildren);
notifyDataSetChanged();
}
protected class ViewHolder {
protected int groupPosition;
protected TextView groupTitle;
protected TextView childTitle;
}
private class ViewHolderChild {
TextView childTitle;
CheckBox childCheckBox;
View viewDivider;
}}
谁能帮忙解决这个问题? 感谢所有抽出时间的人。
【问题讨论】:
标签: android checkbox expandablelistview