作为标题,如何删除由AutoCompleteTextView使用的ArrayAdapter上的过滤以获取原始列表?如何删除AutoCompleteTextView中使用的ArrayAdapter上的筛选器?
多一点背景:
这一切都来自可悲的事实是,在传递给onItemClick()的“位置”值是没用的开始。 “位置”是指阵列过滤后的位置,但我需要知道它的实际位置。所以,我试图做的是当我得到所选项目的文本(通过使用getItemAtPosition(position)),我将它与支持ArrayAdapter的原始字符串数组逐一比较。但是,我发现当调用onItemClick()时,适配器已经过滤,我不再有权访问原始数组。所以我想如果我可以删除过滤器,也许我可以找回原始数组并查找其中的选定项目。
ArrayAdapter<String> mAdapter;
public void onCreate() {
// Create an adapter and remembere it as a class member.
mAdapter = new ArrayAdapter<String>(this, layoutId);
// Add 100 strings to it and attach it to an AutoCompleteTextView
for (int i = 0; i < 100; i++)
mAdapter.add("random text");
((AutoCompleteTextView)findViewById(id)).setAdapter(mAdapter);
}
@Override
public void onItemClick(AdapterView<?> actv, View view, int position, long id) {
if (actv.getAdapter().equals(mAdapter))
Log.d("The adapter contained in actv is the same one I created earlier.");
// And, I can get the text of the item the user selected
String selected = (String)actv.getItemAtPosition(position);
// However, although the adapter passed in is still the same one, but the
// number of items in it is only 1! Because the array has been filtered.
int numItems = actv.getAdapter.getCount();
// So, I'm thinking if I can somehow remove the filtering here, then I can
// get back those 100 items, and do a search like following:
for (int i = 0; i < actv.getAdapter.getCount(); i++)
if (selected == actv.getAdapter.getItem(i))
break; // Eureka!!!
}
要解决获得所选项目的实际位置的问题:
是否有利用“ID”值的方法吗?比如,为每个项目分配一个id,然后希望onItemClick()会传回正确的id。
就像我上面说过的,删除过滤器(有可能),取回原来的100个项目,并执行一个一个的搜索。
这是最后的手段,我知道它会起作用,但我不想这样做:一旦我得到所选文本的文本,我就会回到数据源(从一个数据库),查询这100个项目,并执行搜索。
另一个跛脚最后一招:要避免的开销上再次访问该数据库在#3,当的onCreate(),同时创造一个ArrayAdapter,我用我自己的一个ArrayList记住所有那些100个字符串。
我是不是全都做错了?从AutoCompleteTextView获取所选项目的真实位置的“正确”方式是什么?
非常感谢!
(我读的地方,一些团购似乎是来自谷歌Android团队说,应该使用getFirstVisiblePosition()来解决的位置,但我想不出如何。)
我正在做类似的事情! http://stackoverflow.com/questions/12854336/autocompletetextview-backed-by-cursorloader – toobsco42 2012-10-30 19:47:14