自定义ArrayAdapter过滤器没有结果

时间:2011-11-24 10:11:36

标签: android filter android-arrayadapter

我在AutoCompleteTextView上使用ArrayAdapter。 results.values具有预期值但我在UI上没有列表。

public class CustomArrayAdapter extends ArrayAdapter<String> implements Filterable {
    private final Object mLock = new Object();
    private CustomFilter mFilter;
    public ArrayList<String> mItems;
    public ArrayList<String> mFiltered;

    public CustomArrayAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
        mItems = new ArrayList<String>();
        mFiltered = new ArrayList<String>();
    }

    public Filter getFilter() {
        if (mFilter == null) {
            mFilter = new CustomFilter();
        }
        return mFilter;
    }

    @Override
    public void add(String s) { 
        mItems.add(s);
    }

    private class CustomFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();

            if (mItems == null) {
                synchronized (mLock) {
                    mItems = new ArrayList<String>();
                }
            }

            if (constraint == null || constraint.length() == 0) {
                synchronized (mLock) {
                    results.values = mItems;
                    results.count = mItems.size();
                }
            } else {
                final ArrayList<String> newItems = new ArrayList<String>();

                for (int i = 0; i < mItems.size(); i++) {
                    final String item = mItems.get(i);
                    if(item.contains(constraint)) {
                        newItems.add(item);
                    }
                }
                results.values = newItems;
                results.count = newItems.size();
            }

            return results;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,
                FilterResults results) {
            mFiltered.clear();
            mFiltered.addAll((ArrayList<String>) results.values);
            notifyDataSetChanged();
        }

    }
}

3 个答案:

答案 0 :(得分:14)

看起来覆盖了几个更多的功能完成了这项工作。 完整的工作版本来源:

public class CustomArrayAdapter extends ArrayAdapter<String> implements Filterable {
private List<String> mOrigionalValues;
private List<String> mObjects;
private Filter mFilter;

public CustomArrayAdapter(Context context, int textViewResourceId) {
    super(context, textViewResourceId);
    mOrigionalValues = new ArrayList<String>();
    mObjects = new ArrayList<String>();
}

public void add(String object) {
    mOrigionalValues.add(object);
    this.notifyDataSetChanged();
}

@Override
public int getCount() {
    return mObjects.size();
}

@Override
public String getItem(int position) {
    return mObjects.get(position);
}

public Filter getFilter() {
    if (mFilter == null) {
        mFilter = new CustomFilter();
    }
    return mFilter;
}

@Override
public void notifyDataSetChanged() {
    super.notifyDataSetChanged();
}

private class CustomFilter extends Filter {

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults results = new FilterResults();

        if(constraint == null || constraint.length() == 0) {
            ArrayList<String> list = new ArrayList<String>(mOrigionalValues);
            results.values = list;
            results.count = list.size();
        } else {
            ArrayList<String> newValues = new ArrayList<String>();
            for(int i = 0; i < mOrigionalValues.size(); i++) {
                String item = mOrigionalValues.get(i);
                if(item.contains(constraint)) {
                    newValues.add(item);
                }
            }
            results.values = newValues;
            results.count = newValues.size();
        }       

        return results;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint,
            FilterResults results) {
        mObjects = (List<String>) results.values;
        Log.d("CustomArrayAdapter", String.valueOf(results.values));
        Log.d("CustomArrayAdapter", String.valueOf(results.count));
        notifyDataSetChanged();
    }

}

}

答案 1 :(得分:1)

您需要在CustomArrayAdapter中覆盖getCount方法:

@Override
public int getCount() {
    return mFiltered.size();
}

修改您的publishResults方法代码:

protected void publishResults(CharSequence constraint,FilterResults results) {
            mFiltered = (List<String>) results.values;
            notifyDataSetChanged();
        }

答案 2 :(得分:1)

有些日子我一直在网上研究如何使用hashmap进行arraylist的研究,幸运的是我遇到过这篇文章。

我改编自arraylist&gt;它起作用了。现在我和你分享,你会发现有人喜欢使用ArrayList和HashMap这样的问题。谢谢Pim Reijersen!

public class CategoriaAdapterModel extends BaseAdapter implements Filterable {

private Activity context;
private ArrayList<HashMap<String, String>> mDataShown;
private ArrayList<HashMap<String, String>> mAllData;
private Filter mFilter;
private LayoutInflater inflater;
DownloadImagemUtil downloader;

public CategoriaAdapterModel(Activity context,
        ArrayList<HashMap<String, String>> data) {
    this.context = context;
    this.mAllData = data;
    this.mDataShown = data;
    downloader = new DownloadImagemUtil(context);
}

public void add(HashMap<String, String> object) {
    mAllData.add(object);
    this.notifyDataSetChanged();
}

public int getCount() {
    return mDataShown.size();
}

public Object getItem(int position) {

    return mDataShown.get(position);
}

public long getItemId(int position) {
    return position;

}

public Filter getFilter() {
    if (mFilter == null) {
        mFilter = new CustomFilter();
    }
    return mFilter;
}

@Override
public void notifyDataSetChanged() {
    super.notifyDataSetChanged();
}

private class CustomFilter extends Filter {

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults results = new FilterResults();

        if (constraint == null || constraint.length() == 0) {
            ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(
                    mAllData);
            results.values = list;
            results.count = list.size();
        } else {
            ArrayList<HashMap<String, String>> newValues = new ArrayList<HashMap<String, String>>();
            for (int i = 0; i < mAllData.size(); i++) {
                HashMap<String, String> item = mAllData.get(i);
                if (item.get(JsonFragmentCategoriaCONN.TAG_NOME)
                        .toLowerCase()
                        .contains(constraint.toString().toLowerCase())) {
                    newValues.add(item);
                }
            }
            results.values = newValues;
            results.count = newValues.size();
        }

        return results;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint,
            FilterResults results) {
        mDataShown = (ArrayList<HashMap<String, String>>) results.values;
        Log.d("CustomArrayAdapter", String.valueOf(results.values));
        Log.d("CustomArrayAdapter", String.valueOf(results.count));
        notifyDataSetChanged();
    }

}