I'm getting the wrong result when I click on a item from a lisview after the listview data has been filtered. 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
. For sake of this answer lets say that List
is storing Foo
objects. 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. First you'll need to override the getView()
method so it knows how to display your custom Foo
object. Otherwise it'll just convert your Foo
object to a String
and display that. 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. 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.
Another alternative is to write your own adapter from scratch. Aka implementing the BaseAdapter
class. While not too daunting, it can be rather challenging for those just learning Android or not sure how adapters work in generally. 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. You'll basically do here what you would with the first option but gives you option of specifying how to filter your Foo
object. It also doesn't suffer from the filtering bug in Android's ArrayAdapter
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.