且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

onItemClick给出可见页面上项目的索引/位置...不是列表中项目的实际索引..启用setTextFilterEnabled时的问题

更新时间:2022-12-12 17:05:10

我最近对这个问题进行了一些摔跤,解决方案结果相当简单。您可以在 ListActivity 上使用 getListAdapter()检索可见列表,这反映了当前的筛选视图列表。

I had a bit of a wrestle with this problem recently, and the solution turns out to be fairly simple. You can retrieve the "visible list" by using getListAdapter() on the ListActivity, which reflects the current filtered view of the list.

例如,在你的 ListActivity 子类的 onCreate()

final ListView listView = getListView();
final ListAdapter listAdapter = getListAdapter();

listView .setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        MyClass item = (MyClass) listAdapter .getItem(position);
        // now do something with that item
    }
});

所以,忽略你放入列表适配器的原始列表,而不是要求每次事件进入时都会从适配器列出。

So, ignore the "original" list that you put into the list adapter, and instead ask for the list from the adapter each time the event comes in.