在 expandableListView 中使用选中的复选框更改项目的位置
change position of Item with checked checkbox in expandableListView
在我的 ExpandableListView 中,我有一些项目类别。每个项目的末尾都有一个复选框。当我单击此复选框时,状态应更改为已选中,然后该项目应移至列表末尾的另一个(硬编码)类别。应该像我在原始类别中那样选中复选框。
我在示例中找到的移动项目的方法。
但是在新类别中未选中项目复选框。相反,移动项目的旧位置上的项目将在原始类别中被选中(无需点击)。
示例(o 表示未选中;x 表示选中):
启动应用后:
- 类别 1
item1 o
item2 o
item3 o
- 类别 2
item4 o
点击类别 1 中项目 1 的复选框后
- 类别 1
item2 x
item3 o
- 类别 2
item4 o
- 硬编码类别
item1 o
这是我的适配器-Class
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;
}}
有人可以帮忙解决这个问题吗?
感谢所有抽出时间的人。
试试这个
1.InsideChildrenclass,添加一个布尔变量保存勾选状态:
public class Children {
private boolean checked = false;
//
// Original codes of the Children class.
//
}
2.Change getChildView(),根据保存的checked状态设置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;
}
希望对您有所帮助!
在我的 ExpandableListView 中,我有一些项目类别。每个项目的末尾都有一个复选框。当我单击此复选框时,状态应更改为已选中,然后该项目应移至列表末尾的另一个(硬编码)类别。应该像我在原始类别中那样选中复选框。
我在示例中找到的移动项目的方法。 但是在新类别中未选中项目复选框。相反,移动项目的旧位置上的项目将在原始类别中被选中(无需点击)。
示例(o 表示未选中;x 表示选中):
启动应用后:
- 类别 1
item1 o
item2 o
item3 o - 类别 2
item4 o
点击类别 1 中项目 1 的复选框后
- 类别 1
item2 x
item3 o - 类别 2
item4 o - 硬编码类别
item1 o
这是我的适配器-Class
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;
}}
有人可以帮忙解决这个问题吗? 感谢所有抽出时间的人。
试试这个
1.InsideChildrenclass,添加一个布尔变量保存勾选状态:
public class Children {
private boolean checked = false;
//
// Original codes of the Children class.
//
}
2.Change getChildView(),根据保存的checked状态设置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;
}
希望对您有所帮助!