Filter in android takes the wrong position

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

The filter handles well, but the click position on the list is always constant.

i.e. The ListView gets a filter, but after selecting an item, it takes the same position as a list.

FirstScreen looks like this, and when you select Albania, Albania is displayed correctly, but when, after sorting, it selects and displays the same Albania, since the list displays 0, how to fix this problem.

when the albania is selectedwhen the albania is selectedAfter sorting and selecting behrain the same albania is get selected

Some of the code adapters are:

@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 a list item, click after some search to take it from the first position

ListView Click Code

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();

        }
    });
+1
source share
1

.

:
:


  • ArrayList → [AA, BB, AC]


    .. BB
    AC


  • ArrayList → [AA, BB, AC]


    .. AC

, arg2 (position) , "AC", arraylist.get(arg2) "BB".

:

arg1 (). , .


, ImageView TextView . , ,

((TextView)arg1.findViewById(R.id.textview1)).getText.toString()
+1

Source: https://habr.com/ru/post/1609517/


All Articles