过滤后获取 ArrayAdapter 中的所有项目
Getting all the items in an ArrayAdapter after filtering
相关问题:Getting all of the items from an ArrayAdapter
我有一个 AutoCompleteTextView
支持 ArrayAdapter
。建议列表在运行时发生变化。为了使更改持久化,我需要获取适配器中所有项目的列表。做这个的最好方式是什么?我是否需要遍历列表以获取所有元素?
上述问题中的方法 1(保持对支持列表的引用)不起作用,因为根据 source,过滤后 ArrayAdapter
创建原始列表的副本并对其进行操作,add
将项目发送到适配器不再更改后备列表。
使用 BaseAdapter
也不适用,因为 AutoCompleteTextView
需要 Filterable
适配器。
据我了解你的问题,你的选择是:
扩展基本适配器并提供您自己的过滤。这可能是最好的,但需要最多的努力。
管理与适配器列表并行的列表,向两个列表添加和删除项目。这很容易,但跟踪重复数据对我来说似乎很脏。
清除适配器过滤器,然后抓取整个数据集。这也不漂亮,但它有效。我不知道你的应用程序的用例,所以我不知道清除过滤器是否有任何副作用。
此示例为适配器设置了一些字符串,当用户按下按钮时,它会清除过滤器,然后获取项目总数。此时您可以遍历适配器项以检索所有项。
List<String> stringList = new ArrayList<>();
stringList.add("hello");
stringList.add("hell");
stringList.add("help");
stringList.add("heck");
stringList.add("dude");
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, stringList);
AutoCompleteTextView ac = (AutoCompleteTextView)findViewById(R.id.auto_complete);
ac.setThreshold(1);
ac.setAdapter(adapter);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Filter f = adapter.getFilter();
f.filter("", new Filter.FilterListener() {
@Override
public void onFilterComplete(int count) {
int count2 = adapter.getCount();
mainText.setText("Count is: " + count);
adapter.add("hellraiser");
}
});
}
});
mainText = (TextView) findViewById(R.id.main_text);
相关问题:Getting all of the items from an ArrayAdapter
我有一个 AutoCompleteTextView
支持 ArrayAdapter
。建议列表在运行时发生变化。为了使更改持久化,我需要获取适配器中所有项目的列表。做这个的最好方式是什么?我是否需要遍历列表以获取所有元素?
上述问题中的方法 1(保持对支持列表的引用)不起作用,因为根据 source,过滤后 ArrayAdapter
创建原始列表的副本并对其进行操作,add
将项目发送到适配器不再更改后备列表。
使用 BaseAdapter
也不适用,因为 AutoCompleteTextView
需要 Filterable
适配器。
据我了解你的问题,你的选择是:
扩展基本适配器并提供您自己的过滤。这可能是最好的,但需要最多的努力。
管理与适配器列表并行的列表,向两个列表添加和删除项目。这很容易,但跟踪重复数据对我来说似乎很脏。
清除适配器过滤器,然后抓取整个数据集。这也不漂亮,但它有效。我不知道你的应用程序的用例,所以我不知道清除过滤器是否有任何副作用。
此示例为适配器设置了一些字符串,当用户按下按钮时,它会清除过滤器,然后获取项目总数。此时您可以遍历适配器项以检索所有项。
List<String> stringList = new ArrayList<>();
stringList.add("hello");
stringList.add("hell");
stringList.add("help");
stringList.add("heck");
stringList.add("dude");
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, stringList);
AutoCompleteTextView ac = (AutoCompleteTextView)findViewById(R.id.auto_complete);
ac.setThreshold(1);
ac.setAdapter(adapter);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Filter f = adapter.getFilter();
f.filter("", new Filter.FilterListener() {
@Override
public void onFilterComplete(int count) {
int count2 = adapter.getCount();
mainText.setText("Count is: " + count);
adapter.add("hellraiser");
}
});
}
});
mainText = (TextView) findViewById(R.id.main_text);