android custom adapter filter not working with custom list item
<p>I've been trying forever but I can't find out what the problem is. 
In my case I got several 'modules' which are filled with data. It's possible to filter the list with a EditText, but it does not return the correct row ID in my custom adapters, even though it's the same (except the object is different).</p>

<p>Here are my 2 files, in RecordsListAdapter.java the filter works correctly, in ContactsListAdapter.java it returns the wrong rowID.</p>

<p>Hope you can help me out!</p>

<p>-- EDIT --
I think the problem is with the rowID returned, it does not return the 'original' rowID, it returns the 'filtered' rowID, so tapping the first row in both cases returns 1, but when filtered it should not.</p>

<pre><code>package nl.cuse.crm.adapters;

import java.util.ArrayList;

import nl.cuse.crm.objects.lists.RecordsListItem;
import android.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.TextView;

public class RecordsListAdapter extends ArrayAdapter&lt;RecordsListItem&gt; {
    private ArrayList&lt;RecordsListItem&gt; original;
    private ArrayList&lt;RecordsListItem&gt; filtered;
    private ArrayList&lt;RecordsListItem&gt; fulllist;
    private Filter filter;
    int resource;

    public RecordsListAdapter(Context context, int textViewResourceId,
            ArrayList&lt;RecordsListItem&gt; items) {
        super(context, textViewResourceId, items);
        this.original = items;
        this.fulllist = new ArrayList&lt;RecordsListItem&gt;(items);
        this.filtered = new ArrayList&lt;RecordsListItem&gt;(items);
        resource = textViewResourceId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View v = convertView;

        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.simple_list_item_1, null);
        }

        RecordsListItem m = original.get(position);

        if (m != null) {

            TextView tt = (TextView) v.findViewById(R.id.text1);

            tt.setText(m.get_name());
        } else {
            System.out.println("m is null");
        }

        return v;

    }

    @Override
    public Filter getFilter() {
        if (filter == null)
            filter = new CustomFilter();

        return filter;
    }

    private class CustomFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            System.out.println("Called it: " + constraint);
            FilterResults results = new FilterResults();
            String prefix = constraint.toString().toLowerCase();

            if ((prefix == null) || (prefix.length() == 0)) {
                ArrayList&lt;RecordsListItem&gt; list = new ArrayList&lt;RecordsListItem&gt;(
                        fulllist);
                results.values = list;
                results.count = list.size();
            } else {
                final ArrayList&lt;RecordsListItem&gt; list = new ArrayList&lt;RecordsListItem&gt;(
                        fulllist);
                final ArrayList&lt;RecordsListItem&gt; nlist = new ArrayList&lt;RecordsListItem&gt;();
                int count = list.size();

                for (int i = 0; i &lt; count; i++) {
                    final RecordsListItem pkmn = list.get(i);
                    final String value = pkmn.get_name().toLowerCase();

                    if (value.contains(prefix)) {
                        nlist.add(pkmn);
                    }
                }
                results.values = nlist;
                results.count = nlist.size();
            }
            return results;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,
                FilterResults results) {
            filtered = (ArrayList&lt;RecordsListItem&gt;) results.values;

            clear();
            int count = filtered.size();
            for (int i = 0; i &lt; count; i++) {
                RecordsListItem pkmn = filtered.get(i);
                add(pkmn);
            }
        }

    }

}
</code></pre>

<hr>

<pre><code>package nl.cuse.crm.adapters;

import java.util.ArrayList;

import nl.cuse.crm.R;
import nl.cuse.crm.objects.lists.ContactListItem;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.TextView;

public class ContactListAdapter extends ArrayAdapter&lt;ContactListItem&gt; {
    private ArrayList&lt;ContactListItem&gt; original;
    private ArrayList&lt;ContactListItem&gt; filtered;
    private ArrayList&lt;ContactListItem&gt; fulllist;
    private Filter filter;
    int resource;

    public ContactListAdapter(Context context, int textViewResourceId,
            ArrayList&lt;ContactListItem&gt; items) {
        super(context, textViewResourceId, items);
        this.original = items;
        this.fulllist = new ArrayList&lt;ContactListItem&gt;(items);
        this.filtered = new ArrayList&lt;ContactListItem&gt;(items);
        resource = textViewResourceId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View v = convertView;

        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.custom_list_item_contacts, null);
        }

        ContactListItem m = original.get(position);

        if (m != null) {

            TextView tt = (TextView) v.findViewById(R.id.rd_text1);
            TextView tt2 = (TextView) v.findViewById(R.id.rd_text2);

            if (tt != null) {
                tt.setText(m.get_firstname() + " " + m.get_lastname());
            }
            if (tt2 != null) {
                tt2.setText(m.get_title());
            }
        }

        return v;

    }

    @Override
    public Filter getFilter() {
        if (filter == null)
            filter = new CustomFilter();

        return filter;
    }

    private class CustomFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            System.out.println("Called it: " + constraint);
            FilterResults results = new FilterResults();
            String prefix = constraint.toString().toLowerCase();

            if ((prefix == null) || (prefix.length() == 0)) {
                ArrayList&lt;ContactListItem&gt; list = new ArrayList&lt;ContactListItem&gt;(
                        fulllist);
                results.values = list;
                results.count = list.size();
            } else {
                final ArrayList&lt;ContactListItem&gt; list = new ArrayList&lt;ContactListItem&gt;(
                        fulllist);
                final ArrayList&lt;ContactListItem&gt; nlist = new ArrayList&lt;ContactListItem&gt;();
                int count = list.size();

                for (int i = 0; i &lt; count; i++) {
                    final ContactListItem pkmn = list.get(i);
                    final String value = pkmn.get_firstname().toLowerCase()
                            + "" + pkmn.get_lastname().toLowerCase();

                    if (value.contains(prefix)) {
                        nlist.add(pkmn);
                    }
                }
                results.values = nlist;
                results.count = nlist.size();
            }
            return results;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,
                FilterResults results) {
            filtered = (ArrayList&lt;ContactListItem&gt;) results.values;

            clear();
            int count = filtered.size();
            for (int i = 0; i &lt; count; i++) {
                ContactListItem pkmn = filtered.get(i);
                add(pkmn);
            }
        }

    }

}
</code></pre>
