简体   繁体   English

在Android上单击列表视图项时,过滤列表视图未显示正确的结果

[英]Filtering Listview not showing correct result when listview item is clicked on Android

I'm getting the wrong result when I click on a item from a lisview after the listview data has been filtered. 过滤列表视图数据后,单击lisview中的项目时,得到错误的结果。 I understand the position value changes when the listview is filtered, I can't work out how I get the right position value. 我知道过滤列表视图时位置值会发生变化,我无法确定如何获得正确的位置值。

    mListView.setAdapter(mAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, data));
    mListView.setTextFilterEnabled(true);
    setupSearchView();
    mAdapter.setNotifyOnChange(true);

    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            getActivityMain().pushNextFragment(FragmentWebview.newInstance(bbcNewsList.get(position).link,"roundfeed",bbcNewsList.get(position).title,bbcNewsList.get(position).source,bbcNewsList.get(position).image));
            System.out.println(" id " + id + " pos " + position);
        }
    });

Do I need to use a custom adaptor? 我需要使用自定义适配器吗?

You can't with how your code currently is as there's no way to sync your external list with that of the adapters...especially when filtering is involved. 您无法确定当前的代码状态,因为无法将外部列表与适配器的列表同步...尤其是在涉及过滤时。 You'll need a custom adapter that instead stores the data type of your bbcNewsList . 您将需要一个自定义适配器,而不是存储bbcNewsList的数据类型。 For sake of this answer lets say that List is storing Foo objects. 为了这个答案,可以说List正在存储Foo对象。 You'd want to do something like: 您想要执行以下操作:

List<Foo> bbcNewsList;
// Fill bbcNewsList with data

mAdapter = new MyCustomAdapter(getActivity(),bbcNewsList);
mListView.setAdapter(mAdapter);
//Continue instantiation as needed

You could try extending the ArrayAdapter for your custom solution but that may be problematic. 您可以尝试为自定义解决方案扩展ArrayAdapter ,但这可能会出现问题。 First you'll need to override the getView() method so it knows how to display your custom Foo object. 首先,您需要重写getView()方法,以便它知道如何显示自定义Foo对象。 Otherwise it'll just convert your Foo object to a String and display that. 否则,它将仅将Foo对象转换为String并显示它。 With filtering in the picture, you are stuck with the default logic. 使用图片过滤功能时,您将陷入默认逻辑。 In your case, the ArrayAdapter would convert your Foo object to a String and filter based on that result. 在您的情况下, ArrayAdapter会将Foo对象转换为String并根据该结果进行过滤。 You may be fine with that behavior and in which case, you are fine with this approach. 您可能会喜欢这种行为,在这种情况下,您会使用这种方法。 However I will warn that the ArrayAdapter has a pretty nasty filtering bug if you plan on modifying it on the fly. 但是,我警告说,如果您打算即时修改ArrayAdapter会有一个非常讨厌的过滤错误

Another alternative is to write your own adapter from scratch. 另一种选择是从头开始编写自己的适配器。 Aka implementing the BaseAdapter class. BaseAdapter实现BaseAdapter类。 While not too daunting, it can be rather challenging for those just learning Android or not sure how adapters work in generally. 虽然不太令人生畏,但对于那些刚学习Android或不确定适配器通常如何工作的人来说,这可能是一个挑战。 This solution would also require you to implement all the filtering code as well. 此解决方案还需要您实施所有过滤代码。

Finally a third option is use a third party library. 最后,第三个选择是使用第三方库。 I suggest the AbsArrayAdapter class. 我建议使用AbsArrayAdapter类。 You'll basically do here what you would with the first option but gives you option of specifying how to filter your Foo object. 您基本上将在此处执行第一个选项的操作,但可以选择指定如何过滤Foo对象。 It also doesn't suffer from the filtering bug in Android's ArrayAdapter . 它还不受Android ArrayAdapter的过滤错误的困扰。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM