Android 在 listView 中获取子项
Android Get child items in listView
我有一个用于 listView 的 ArrayAdapter,里面有复选框。
更改复选框值时,我想获取已选中复选框的项目列表。
我的 activity
的 onCreate
函数内部
ArrayAdapter<DrawerDetail> adapter = new ArrayAdapter<DrawerDetail>(this, R.layout.settings_minidrawer_item, apps) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = getLayoutInflater().inflate(R.layout.settings_minidrawer_item, null);
}
ImageView appIcon = (ImageView)convertView.findViewById(R.id.item_app_icon);
appIcon.setImageDrawable(apps.get(position).icon);
appIcon.setTag(apps.get(position).name);
TextView appLabel = (TextView)convertView.findViewById(R.id.item_app_name);
appLabel.setText(apps.get(position).label);
appLabel.setText(Integer.toString(position));
CheckBox appCheck = (CheckBox)convertView.findViewById(R.id.item_app_check);
appCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
UpdatePreferences();
}
});
return convertView;
}
};
list.setAdapter(adapter);
updatePreferences
public void UpdatePreferences() {
SharedPreferences.Editor prefEditor = prefs.edit();
SparseBooleanArray checked = list.getCheckedItemPositions();
List<String> drawerListArray = new ArrayList<String>();
View v;
CheckBox c;
for(int i = 0; i < list.getAdapter().getCount(); i++) {
v = list.getAdapter().getView(i, null, list);
c = (CheckBox) v.findViewById(R.id.item_app_check);
TextView aka = (TextView) v.findViewById(R.id.item_app_name);
aka.setText("dd");
Log.e("checked: ", Boolean.toString(c.isChecked()));
}
}
如果您检查 SparsBooleanArray checked
值,它始终为空。另外 list.getAdapter().getView()
似乎不起作用。
此行找不到任何视图(没有 return 错误,但不能 get/set 值):
c = (CheckBox) v.findViewById(R.id.item_app_check);
TextView aka = (TextView) v.findViewById(R.id.item_app_name);
我尝试了不同的方法,但都无法正常工作。现在我的猜测是我正在覆盖适配器的 getView
方法并且它们相互冲突,但我无法想出任何解决方案如何以对两者都适用的方式覆盖 getView。
不要检索这些视图。在您的 onCheckedChangeListener 中传递 checked/unchecked 信息 - 例如字符串和信息,如果是 checked/unchecked。然后你可以在数组中收集选中的项目。
尝试做类似的事情:
public class TestClass {
private List<String> checkedStrings = new ArrayList<>();
...
void updateArray(boolean checked, String checkedText){
if(checked){
if(!checkedStrings.contains(checkedText)){
checkedStrings.add(checkedText);
}
} else {
checkedStrings.remove(checkedText);
}
}
}
并且在您的 ArrayAdapter 中:
appCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
updateArray(isChecked, apps.get(position).label);
}
});
请尝试替换您的代码:
c = (CheckBox) v.findViewById(R.id.item_app_check);
使用此代码:
View nextChild = null;
// Find CheckBox inside the parent view
for (int i = 0; i < ((ViewGroup) v).getChildCount(); ++i) {
nextChild = ((ViewGroup) v).getChildAt(i);
if (nextChild instanceof CheckBox) {
// Do your work;
break;
}
}
我有一个用于 listView 的 ArrayAdapter,里面有复选框。 更改复选框值时,我想获取已选中复选框的项目列表。
我的 activity
的onCreate
函数内部
ArrayAdapter<DrawerDetail> adapter = new ArrayAdapter<DrawerDetail>(this, R.layout.settings_minidrawer_item, apps) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = getLayoutInflater().inflate(R.layout.settings_minidrawer_item, null);
}
ImageView appIcon = (ImageView)convertView.findViewById(R.id.item_app_icon);
appIcon.setImageDrawable(apps.get(position).icon);
appIcon.setTag(apps.get(position).name);
TextView appLabel = (TextView)convertView.findViewById(R.id.item_app_name);
appLabel.setText(apps.get(position).label);
appLabel.setText(Integer.toString(position));
CheckBox appCheck = (CheckBox)convertView.findViewById(R.id.item_app_check);
appCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
UpdatePreferences();
}
});
return convertView;
}
};
list.setAdapter(adapter);
updatePreferences
public void UpdatePreferences() {
SharedPreferences.Editor prefEditor = prefs.edit();
SparseBooleanArray checked = list.getCheckedItemPositions();
List<String> drawerListArray = new ArrayList<String>();
View v;
CheckBox c;
for(int i = 0; i < list.getAdapter().getCount(); i++) {
v = list.getAdapter().getView(i, null, list);
c = (CheckBox) v.findViewById(R.id.item_app_check);
TextView aka = (TextView) v.findViewById(R.id.item_app_name);
aka.setText("dd");
Log.e("checked: ", Boolean.toString(c.isChecked()));
}
}
如果您检查 SparsBooleanArray checked
值,它始终为空。另外 list.getAdapter().getView()
似乎不起作用。
此行找不到任何视图(没有 return 错误,但不能 get/set 值):
c = (CheckBox) v.findViewById(R.id.item_app_check);
TextView aka = (TextView) v.findViewById(R.id.item_app_name);
我尝试了不同的方法,但都无法正常工作。现在我的猜测是我正在覆盖适配器的 getView
方法并且它们相互冲突,但我无法想出任何解决方案如何以对两者都适用的方式覆盖 getView。
不要检索这些视图。在您的 onCheckedChangeListener 中传递 checked/unchecked 信息 - 例如字符串和信息,如果是 checked/unchecked。然后你可以在数组中收集选中的项目。
尝试做类似的事情:
public class TestClass {
private List<String> checkedStrings = new ArrayList<>();
...
void updateArray(boolean checked, String checkedText){
if(checked){
if(!checkedStrings.contains(checkedText)){
checkedStrings.add(checkedText);
}
} else {
checkedStrings.remove(checkedText);
}
}
}
并且在您的 ArrayAdapter 中:
appCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
updateArray(isChecked, apps.get(position).label);
}
});
请尝试替换您的代码:
c = (CheckBox) v.findViewById(R.id.item_app_check);
使用此代码:
View nextChild = null;
// Find CheckBox inside the parent view
for (int i = 0; i < ((ViewGroup) v).getChildCount(); ++i) {
nextChild = ((ViewGroup) v).getChildAt(i);
if (nextChild instanceof CheckBox) {
// Do your work;
break;
}
}