如何去除在AutoCompleteTextView使用的ArrayAdapter过滤器? [英] How to remove the filter on an ArrayAdapter used in an AutoCompleteTextView?
问题描述
正如标题,我怎么能去除由AutoCompleteTextView使用的ArrayAdapter滤波来获得原始列表回来了?
As the title, how can I remove the filtering on an ArrayAdapter used by an AutoCompleteTextView to get the original list back?
一个多一点背景:
这一切都来自可悲的事实是在传递给onItemClick()的位置值是无用的启动。 位置指的是阵列后,被过滤的位置,但我需要知道它的实际位置。所以,我想要做的是,当我有选择的项目的文本(通过使用getItemAtPosition(位置)),我比较一下一个接一个与原始字符串数组,备份一个ArrayAdapter。然而,我发现,当onItemClick()被调用时,适配器已过滤,我不再有访问原始数组。所以我想,如果我可以取下过滤网,也许我可以拿回原始数组,并期待在它所选的项目。
It all started from the sad fact that the "position" value passed in to onItemClick() is useless. The "position" refers to the position AFTER the array has been filtered, but I need to know its REAL position. So, what I'm trying to do is when I've got the text of the selected item (by using getItemAtPosition(position)), I compare it one-by-one with the original string array that backs the ArrayAdapter. However, I found that when onItemClick() is called, the adapter is already filtered, I no longer have access to the original array. So I thought if I can remove the filter, maybe I can get back the original array and look for the selected item in it.
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!!!
}
要解决获得所选项目的实际位置的问题:
To tackle the problem of obtaining the REAL position of the selected item:
-
有没有利用ID价值的方式?就像,为每个项目的ID,然后希望onItemClick()将传回正确的ID。
Is there a way to utilize the "id" value? Like, assign each item an id, then hopefully onItemClick() would pass back the correct id.
就像我前面所说,取下过滤网(有可能),找回原来的100个项目,并执行一个接一个的搜索。
Like I said above, remove the filter (is it possible), get back the original 100 items, and perform a one-by-one search.
这是不得已而为之,我知道它会工作,但我并不想这样做:一旦我得到所选文本的文字,我回去的数据源(从数据库),查询这些100个项目出来,并进行搜索。
This is the last resort, I know it'll work, but I don't want to do it: Once I get the text of the selected text, I go back to the source of the data (from a database), query those 100 items out, and perform the search.
另外一个瘸子最后一招:为了避免再次访问该数据库在#3的开销,当的onCreate(),同时创造一个ArrayAdapter,我用我自己的ArrayList记住所有那些100个字符串。
Another lame last resort: To avoid the overhead on accessing the database again as in #3, when in onCreate(), while creating the ArrayAdapter, I use an ArrayList of my own to remember all those 100 strings.
我做这一切错了吗?什么是从AutoCompleteTextView获得所选项目的实际位置的正确的方式?
Am I doing it all wrong? What's the "right" way of obtaining the real position of the selected item from an AutoCompleteTextView?
非常感谢你!
(我读的地方,一些团购似乎是来自谷歌Android团队说,应该使用getFirstVisiblePosition()来解决的立场。但我不知道如何。)
(I read somewhere, some buy that seemed to be from Google Android team, said that one should use getFirstVisiblePosition() to resolve the position. But I can't figure out how.)
推荐答案
我不知道你是否仍然有兴趣,但我发现这个回答类似的问题:<一href=\"http://stackoverflow.com/questions/5944314/problem-with-autocompletetextview-and-spinner-using-the-same-adapter\">Problem与AutoCompleteTextView和微调使用相同的适配器
I don't know if you're still interested, but I found this answering a similar question: Problem with AutoCompleteTextView and Spinner using the same Adapter
复制在 AutoCompleteTextView
源$ C $ c中的方法:
Copying the method in the AutoCompleteTextView
source code:
Filter filter = mAdapter.getFilter();
filter = null;
请参阅我在grep的code链接对上述问题的回应。
See my response in the above question for the grepcode link.
这篇关于如何去除在AutoCompleteTextView使用的ArrayAdapter过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!