在我的ExpandableListView中,我有一些类别的项目。每个项目的末尾都有一个复选框。当我单击此复选框时,状态应更改为已检查,然后该项目应移到列表末尾的另一个(硬编码)类别。就像我在原始类别中所做的一样,应该选中该复选框。
我在示例中找到的移动项目的方式。 但是在新类别中未选中项目复选框。取而代之的是,将检查(无需单击)原始类别中已移动项目的旧位置上的项目。
示例(o表示未选中; x表示选中):
启动应用后:
点击类别1中第1项的复选框
这是我的适配器类
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;
}}
有人可以帮助解决这个问题吗? 感谢所有花费时间的人。
答案 0 :(得分:0)
尝试一下
1。在 Children 类中,添加一个布尔变量以保存检查状态:
public class Children {
private boolean checked = false;
//
// Original codes of the Children class.
//
}
2。更改 getChildView(),根据保存的选中状态设置CheckBox:
@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());
holder.childCheckBox.setChecked(child.checked); // Added.
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();
child = mParent.get(groupPosition).getArrayChildren().get(childPosition); // Added.
child.checked = !child.checked; // Added.
addChildToGroup(child, mParent.size() - 1, "Parent 25 (Hardcoded)"); // Changed.
removeChildFromGroup(groupPosition, childPosition);
notifyDataSetChanged();
}
});
//return the entire view
return convertView;
}
希望有帮助!