0

我一直在尝试,但我无法找出问题所在。就我而言,我得到了几个充满数据的“模块”。可以使用 EditText 过滤列表,但它不会在我的自定义适配器中返回正确的行 ID,即使它是相同的(除了对象不同)。

这是我的 2 个文件,在 RecordsListAdapter.java 中过滤器正常工作,在 ContactsListAdapter.java 中它返回错误的 rowID。

希望你能帮帮我!

-- 编辑 -- 我认为问题在于返回的 rowID,它不返回“原始”rowID,它返回“过滤”的 rowID,因此在两种情况下点击第一行都会返回 1,但过滤时不应该.

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<RecordsListItem> {
    private ArrayList<RecordsListItem> original;
    private ArrayList<RecordsListItem> filtered;
    private ArrayList<RecordsListItem> fulllist;
    private Filter filter;
    int resource;

    public RecordsListAdapter(Context context, int textViewResourceId,
            ArrayList<RecordsListItem> items) {
        super(context, textViewResourceId, items);
        this.original = items;
        this.fulllist = new ArrayList<RecordsListItem>(items);
        this.filtered = new ArrayList<RecordsListItem>(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<RecordsListItem> list = new ArrayList<RecordsListItem>(
                        fulllist);
                results.values = list;
                results.count = list.size();
            } else {
                final ArrayList<RecordsListItem> list = new ArrayList<RecordsListItem>(
                        fulllist);
                final ArrayList<RecordsListItem> nlist = new ArrayList<RecordsListItem>();
                int count = list.size();

                for (int i = 0; i < 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<RecordsListItem>) results.values;

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

    }

}

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<ContactListItem> {
    private ArrayList<ContactListItem> original;
    private ArrayList<ContactListItem> filtered;
    private ArrayList<ContactListItem> fulllist;
    private Filter filter;
    int resource;

    public ContactListAdapter(Context context, int textViewResourceId,
            ArrayList<ContactListItem> items) {
        super(context, textViewResourceId, items);
        this.original = items;
        this.fulllist = new ArrayList<ContactListItem>(items);
        this.filtered = new ArrayList<ContactListItem>(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<ContactListItem> list = new ArrayList<ContactListItem>(
                        fulllist);
                results.values = list;
                results.count = list.size();
            } else {
                final ArrayList<ContactListItem> list = new ArrayList<ContactListItem>(
                        fulllist);
                final ArrayList<ContactListItem> nlist = new ArrayList<ContactListItem>();
                int count = list.size();

                for (int i = 0; i < 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<ContactListItem>) results.values;

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

    }

}
4

1 回答 1

0

我刚刚发现我做错了什么,并修复了它。我会为其他有同样问题的人解释。过滤后,列表会获得新的索引,因此列表中的第一个始终是第 1 行。我的侦听器是用 ArrayList.get(rowID) 编写的,在过滤时它不起作用,因为 rowID 被更改。我为解决该问题所做的是制作 ArrayAdapter 的副本并过滤该副本,这显然用过滤器更改了列表索引

于 2012-12-30T13:11:59.063 回答