Filter in android is taking the wrong position
Asked Answered
A

1

1

I am trying to sort the list by EditText using filter in adapter.

Filter is processing well but the position of listview click is always constant.

i.e. ListView is getting filter but after selection of item its taking the same position in the list view.

FirstScreen looks like this and when when the albania is selected albania is displayed properly but when after sorting behrain get selected and same albania is displayed since poition is 0 in the list view how to rectify this issue

when the albania is selected when the albania is selected After sorting and selecting behrain the same albania is get selected

Some of the code in adapter is :

@Override
public Filter getFilter() {

    Filter filter = new Filter() {

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,
                FilterResults results) {

            country_inf = (ArrayList<CustomCountryCodesPojo>) results.values;
            notifyDataSetChanged();
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            FilterResults results = new FilterResults();
            ArrayList<CustomCountryCodesPojo> FilteredArrayNames = new ArrayList<CustomCountryCodesPojo>();

            if (mOriginalNames == null) {
                mOriginalNames = new ArrayList<CustomCountryCodesPojo>(
                        country_inf);
            }
            if (constraint == null || constraint.length() == 0) {
                results.count = mOriginalNames.size();
                results.values = mOriginalNames;
            } else {
                constraint = constraint.toString().toLowerCase();
                for (int i = 0; i < mOriginalNames.size(); i++) {
                    CustomCountryCodesPojo dataNames = mOriginalNames
                            .get(i);
                    if (dataNames.countryName.toString().toLowerCase()
                            .contains(constraint.toString())) {
                        FilteredArrayNames.add(dataNames);
                    }
                }

                results.count = FilteredArrayNames.size();
                // System.out.println(results.count);

                results.values = FilteredArrayNames;
                // Log.e("VALUES", results.values.toString());
            }

            return results;
        }
    };

    return filter;
}

My problem is that in list item click after some search its taking from the first position

ListView click code is

lv_list.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            Intent i = new Intent();
            i.putExtra("flagId", flagId);
            i.putExtra("name", allRecords.get(arg2).countryName);
            setResult(RESULT_OK, (i).setAction("ok"));
            finish();

        }
    });
Alodie answered 16/11, 2013 at 11:58 Comment(3)
So your filtering is OK. But on item Click, have problem with these lines. i.putExtra("flagId", flagId); i.putExtra("name", allRecords.get(arg2).countryName);. Right?Vannessavanni
I am passing those values to other layout. here arg2 is 1 in both the cases so the value remains same. How to avoid it.Alodie
https://mcmap.net/q/473842/-list-filter-custom-adapter-dont-give-resultCharmainecharmane
V
1

This is a common problem when using a filter for the list.

Eg:
Problem is:

  • Before filtering,
    ArrayList-->[AA,BB,AC]
    List-->
    AA
    BB
    AC

  • After filtering,
    ArrayList-->[AA,BB,AC]
    List-->
    AA
    AC

So, when usingarg2(position), after filtering, if you click on 'AC', arraylist.get(arg2) will return 'BB'.

Solution:

Use arg1(View) instead. It is the current single row item View, which you have clicked.


I think you are using an ImageView and a TextView for custom list row. Then for country name, I'll code as,

((TextView)arg1.findViewById(R.id.textview1)).getText.toString()
Vannessavanni answered 16/11, 2013 at 13:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.