gpt4 book ai didi

Android:在 ExpandableListView 中动态标记复选框

转载 作者:行者123 更新时间:2023-11-29 14:40:51 24 4
gpt4 key购买 nike

我正在使用 ExpandableListView,每个子行都有一个 TextView 和其中的复选框。

我想要做的是,当用户按下按钮时, Activity 会找出“选中”的项目并对这些项目执行某些操作。

我阅读了很多关于 ExpandableListView 和复选框目前如何无法正常工作的帖子,以及我如何需要保留一个额外的数据结构来跟踪已选择的内容,以及未选择的内容。这篇文章涵盖了这个问题: ExpandableListView and checkboxes

完成!我现在可以跟踪已选择的内容和未选择的内容。

现在我想解决复选框不显示其状态的问题。我注意到复选框在以下情况下会随机更改:

  • 屏幕旋转变化
  • 一组折叠/打开

我知道可以检测到这些事件。

我想知道是否有办法手动设置复选框。我最初尝试过

ExpandableListView elv = getExpandableListView();
for(int i = 0; i < elv.getChildCount(); i++) {
CheckBox checkbox = ... somehow get checbox within the list
checkbox.setChecked(checkList.at(i, j));
}

其中 i 和 j 表示哪个组和子级,checkList 跟踪已检查/未检查的项目。

但是,我注意到 getChildCount() 的值取决于打开的组数,并不一定告诉我可以操作哪个列表项。

ExpandableListView 已经用过了,肯定有人有办法绕过这个问题。有没有聪明的解决方案?具体来说,我正在寻找在 onGroupExpand 和 onConfigurationChange(或类似事件)的列表中选中/取消选中适当项目的方法。

提前致谢!

最佳答案

检查以下示例可扩展列表适配器

/**
* A simple adapter which maintains an ArrayList of photo resource Ids.
* Each photo is displayed as an image. This adapter supports clearing the
* list of photos and adding a new photo.
*
*/
public class MyExpandableListAdapter extends BaseExpandableListAdapter {


// Sample data set. children[i] contains the children (String[]) for groups[i].
private String[] groups = { "People Names", "Dog Names", "Cat Names", "Fish Names" };
private String[][] children = {
{ "Arnold", "Barry", "Chuck", "David" },
{ "Ace", "Bandit", "Cha-Cha", "Deuce" },
{ "Fluffy", "Snuggles" },
{ "Goldy", "Bubbles" }
};

private ArrayList<ArrayList<Boolean>> checkboxStatus = new ArrayList<ArrayList<Boolean>>();

public MyExpandableListAdapter() {
int groupCount = groups.length;
for(int i=0; i<groupCount; i++)
{
int childCount = children[i].length;
ArrayList<Boolean> childStatus = new ArrayList<Boolean>();
for(int j=0; j<childCount; j++) {
childStatus.add(false);
}
checkboxStatus.add(childStatus);
}
}

public Object getChild(int groupPosition, int childPosition) {
return children[groupPosition][childPosition];
}

public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}

public int getChildrenCount(int groupPosition) {
return children[groupPosition].length;
}

public TextView getGenericView() {

// Layout parameters for the ExpandableListView
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, 64);

TextView textView = new TextView(ExpandableListCheckboxActivity.this);
textView.setLayoutParams(lp);
// Center the text vertically
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
// Set the text starting position
textView.setPadding(36, 0, 0, 0);
return textView;
}

public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {

CheckBox chkbox = new CheckBox(ExpandableListCheckboxActivity.this);
chkbox.setTag("" + groupPosition + "," + childPosition);
chkbox.setOnCheckedChangeListener(checkboxListener);
chkbox.setText(children[groupPosition][childPosition]);

chkbox.setChecked(checkboxStatus.get(groupPosition).get(childPosition));

return chkbox;
}

public Object getGroup(int groupPosition) {
return groups[groupPosition];
}

public int getGroupCount() {
return groups.length;
}

public long getGroupId(int groupPosition) {
return groupPosition;
}

public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getGroup(groupPosition).toString());

return textView;
}

public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}

public boolean hasStableIds() {
return true;
}

private OnCheckedChangeListener checkboxListener = new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
String positions = buttonView.getTag().toString();
int groupPosition = Integer.valueOf(positions.split(",")[0]);
int childPosition = Integer.valueOf(positions.split(",")[1]);
checkboxStatus.get(groupPosition).set(childPosition, isChecked);
}
};

}

关于Android:在 ExpandableListView 中动态标记复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10736241/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com