1

I want to add a permanent search result in autocomplete text view in android. For ex:If i enter 'x' in the autocomplete and if it shows the list of hotels...xyz1,xyz2.etc...Then the last result must be "NOT IN LIST" value. If the user cant find their hotel then they can select NOT IN LIST option..

Even if the user types in the text that the predictive search could not give then 'NOT IN LIST' should be the only suggestion that autocomplete should give. I am new to Android.I am doing a small app.Plz help..If i have to use custom autocomplete text view then what method should i override? If so ,tell me with a method code that i have to override

4

2 回答 2

1

这是AutoCompleteAdapter我在我的一个应用程序中使用的。我希望这能解决你的问题

将适配器从下方设置为您的AutoCompleteTextView控件:

    ArrayAdapter<String> adapter = new AutoCompleteAdapter(this,
            R.layout.dropdown_item);

    autoComplete.setAdapter(adapter);

样品适配器:

private class AutoCompleteAdapter extends ArrayAdapter<String>
        implements Filterable {
    private ArrayList<String> mData;

    public AutoCompleteAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
        mData = new ArrayList<String>();
    }

    @Override
    public int getCount() {
        try {
            return mData.size();
        } catch (NullPointerException e) {
            return 0;
        }
    }

    @Override
    public String getItem(int index) {
        return mData.get(index);
    }

    @Override
    public Filter getFilter() {
        Filter myFilter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults filterResults = new FilterResults();
                if (constraint != null) {
                    //This shows a progress to the user in my app. you don't need to use this
                    handle.sendEmptyMessage(MSG_SHOW_PROGRESS);
                    try {
                        //Fill mData with your data
                        mData = XmlParser.searchLocations(constraint
                                .toString());
                    } catch (Exception e) {
                        handle.sendEmptyMessage(MSG_HIDE_PROGRESS);
                    }
                    mData.add("NOT IN LIST");
                    filterResults.values = mData;
                    filterResults.count = mData.size();
                }
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence contraint,
                    FilterResults results) {
                if (results != null && results.count > 0) {
                    notifyDataSetChanged();
                    handle.sendEmptyMessage(MSG_HIDE_PROGRESS);
                } else {
                    notifyDataSetInvalidated();
                    handle.sendEmptyMessage(MSG_HIDE_PROGRESS);
                }
            }
        };
        return myFilter;
    }
}
于 2012-12-06T15:59:31.897 回答
0
if (constraint != null) {

    try {
        for(int i=0;i<temp.size();i++)
        {
        if(temp.get(i).toString().startsWith(constraint.toString().toUpperCase()))
        {

        mData.add(temp.get(i).toString());  

        }
        }


        mData.add("SEARCH NOT THERE");

    } catch (Exception e) {

    }


         filterResults.values = mData;
         filterResults.count = mData.size();




}
return filterResults;

我已经完成了过滤...仍然显示所有结果而不是我在其中输入的任何内容...:(

于 2012-12-23T14:13:30.227 回答