【发布时间】:2016-12-13 14:08:20
【问题描述】:
我有一个用于 listView 的 ArrayAdapter,里面有复选框。 当复选框值更改时,我想获取带有选中复选框的项目列表。
在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() 似乎也不起作用。
此行找不到任何视图(不返回错误,但无法获取/设置值):
c = (CheckBox) v.findViewById(R.id.item_app_check);
TextView aka = (TextView) v.findViewById(R.id.item_app_name);
我尝试了不同的方法,但无法成功。现在我的猜测是我正在覆盖适配器的 getView 方法并且它们相互冲突,但我无法想出任何解决方案如何覆盖 getView 以使其适用于两者。
【问题讨论】:
标签: android listview android-arrayadapter