English 中文(简体)
A.A.Adapter Filter
原标题:No results with custom ArrayAdapter Filter

I m 采用ArrayAdapter在AutoCompleteTextView上。 结果,价值具有预期价值,但我没有列入统一分类表。

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

    }
}
最佳回答

Looks like overriding a couple more functions did the job. Full source of working version:

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

}

}
问题回答

Custom Ar

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

并修改你的出版商法:

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

几天前,我一直在研究如何用海图进行阵列清单研究,幸运的是,我走过这个岗位。

我对阵列清单做了调整;我做了工作。 现在,我与你分享,你们会知道有人像使用阿雷拉派和哈希姆普一样。 感谢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();
    }

}




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签