[英]Get selected items from listview using custom adapter on submit button pressed in Android
I have created my own adapter, InteractiveArrayAdapter, and set it to as adapter for the listview (MyListView) I have in my xml layout file, let's say MyActivity. 我创建了自己的适配器InteractiveArrayAdapter,并将其设置为xml布局文件中的列表视图(MyListView)的适配器,比如说MyActivity。 Then, from MyActivity activity, I perform this:
然后,从MyActivity活动中执行以下操作:
MyActivity.java : MyActivity.java :
ListView listView = (ListView) findViewById(R.id.MyListView);
ArrayAdapter<Model> adapter = new InteractiveArrayAdapter(this, model);
listView.setAdapter(adapter);
My custom adapter uses a custom model, Model, that I have created to work with each element in the listview. 我的自定义适配器使用我创建的自定义模型Model来与listview中的每个元素一起使用。 Each listview item, consists of a TextView and a CheckBox.
每个listview项都包含一个TextView和一个CheckBox。
My goal is to obtain the complete list of checked items when user press a submit button placed in the MyActivity xml layout file so I perform the following steps to get rid of this: 我的目标是,当用户按下MyActivity xml布局文件中的提交按钮时,获得已检查项目的完整列表,因此我执行以下步骤来消除此问题:
In my custom adapter, InteractiveArrayAdapter, I declare a private Set to store the identifiers of each element selected in the listview. 在我的自定义适配器InteractiveArrayAdapter中,我声明了一个私有Set来存储在列表视图中选择的每个元素的标识符。 I use my own identifier insteaf of using the position in the list.
我使用自己的标识符代替了使用列表中的位置。 This identifier comes from the primary key in the database from where in a previous step I have extracted all the desired items to populate the listview.
该标识符来自数据库中的主键,在上一步中,我从中提取了所有所需项以填充列表视图。 As identifiers for items cannot be repeated, I use a Set.
由于项目的标识符无法重复,因此我使用了Set。 This Set is updated each time an item in the listview is checked or unchecked.
每次选中或取消选中列表视图中的项目时,都会更新此Set。 Also I implement a public method to return this set.
我也实现了一个公共方法来返回这个集合。
public class InteractiveArrayAdapter extends ArrayAdapter<Model> { ... private Set<Integer> checked = new HashSet<Integer>(); ... @Override public View getView(int position, View convertView, ViewGroup parent) { View view = null; viewHolder.checkbox = (CheckBox) view.findViewById(R.id.interactiveRowCheck); viewHolder.checkbox .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { Model element = (Model) viewHolder.checkbox .getTag(); element.setSelected(buttonView.isChecked()); Integer id = element.getId(); if (isChecked) { checked.add(id); } else if (checked.contains(id)) { checked.remove(id); } } }); } public Set<Integer> getCheckedItems() { return this.checked; } }
in MyActivity.java I handle click on the button and I try to read the list of items checked by doing this: 在MyActivity.java中,我单击了按钮,然后尝试通过执行以下操作读取已检查的项目列表:
public void OnClickCompareButton(View view) { InteractiveArrayAdapter adapter; Set<Integer> checkedItems = adapter.getCheckedItems(); Iterator<Integer> it = checkedItems.iterator(); int size = checkedItems.size(); for (int i = 0; i < size; i++) { // Do whatever... } }
But Android Studio is throwing an error on compiling: 但是Android Studio在编译时抛出错误:
Error:(82, 37) error: variable adapter might not have been initialized
If then I use the variable adapter initialized at the beginning of this post when setting the adapter, that is: 若我使用变量设置转接适配器时在这篇文章开始时被初始化,那就是:
public void OnClickCompareButton(View view) {
Set<Integer> checkedItems = this.adapter.getCheckedItems();
Iterator<Integer> it = checkedItems.iterator();
int size = checkedItems.size();
for (int i = 0; i < size; i++) {
// Do whatever...
}
}
below error is shown on compiling: 编译时显示以下错误:
Error:(82, 49) error: cannot find symbol method getCheckedItems()
So how to solve this? 那么如何解决呢?
Error:(82, 37) error: variable adapter might not have been initialized
错误:(82、37)错误:变量适配器可能尚未初始化
This is indeed the case - adapter
had been declared just above and not initialised, so would be null
anyway if the compiler hadn't got there first. 确实是这种情况-
adapter
仅在上面声明并且没有初始化,因此如果编译器未首先到达adapter
将始终为null
。
Your second method seems, at a quick glance, much more likely to work, so: 快速浏览第二种方法似乎更有可能起作用,因此:
Error:(82, 49) error: cannot find symbol method getCheckedItems()
错误:(82、49)错误:找不到符号方法getCheckedItems()
This is because the declared type of the Adapter is ListAdapter
(see getAdapter() , arguably the preferred way to access fields), not your subclass InteractiveArrayAdapter
. 这是因为Adapter的声明类型是
ListAdapter
(请参见getAdapter() ,可以说是访问字段的首选方法),而不是您的子类InteractiveArrayAdapter
。
You could cast it: 您可以将其转换为:
Set<Integer> checkedItems = ((InteractiveArrayAdapter) getListAdapter()).getCheckedItems();
and to avoid confusion with the ArrayAdapter
you could simplify the instantiation / passing to your view: 并避免与
ArrayAdapter
混淆,您可以简化实例化/传递给视图:
ListView listView = (ListView) findViewById(R.id.MyListView);
listView.setAdapter(new InteractiveArrayAdapter(this, model));
Also, a couple of minor code style points: 另外,还有一些次要的代码样式点:
this.
this.
in a method (eg return this.checked;
-> return checked;
) return this.checked;
-> return checked;
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.