0

I require an AutoCompleteTextView in an AlertDialog which I've placed in onOptionsItemSelected(). I've made a custom adapter overriding the getFilter() method. I can't figure out why but the AutoCompleteTextView does not show the dropdown menu.

Code for onOptionsItemSelected():

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    switch (item.getItemId()) {
    case R.id.action_add: {

        AlertDialog.Builder alert = new AlertDialog.Builder(this);

        LinearLayout ll = new LinearLayout(BaseActivity.this);
        ll.setOrientation(LinearLayout.VERTICAL);

        final AutoCompleteTextView code = new   AutoCompleteTextView(BaseActivity.this);

        alert.setMessage("Add");

        code.setHint("Stock Code");
        acAdapter = new AutoCompleteAdapter(BaseActivity.this, android.R.layout.simple_dropdown_item_1line); 
        code.setAdapter(acAdapter);
        code.setThreshold(1);
        ll.addView(code);

        alert.setView(ll);

        alert.setPositiveButton("Add",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                            int whichButton) {
                                          ...
                }
         }
         break;
    }
    }
}   

Code for AutoCompleteAdapter Class:

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

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

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

    @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) {
                    mData.add("google");
                    filterResults.values = mData;
                    filterResults.count = mData.size();
                }  
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence contraint, FilterResults results) {
                if(results != null && results.count > 0) {
                    notifyDataSetChanged();
                }
                else {
                    notifyDataSetInvalidated();
                }
            }
        };
        return myFilter;
    }
}

What went wrong with my implementation?

PS: The performFiltering() method here merely adds "google" to the ArrayList for simplicity sake. I have a genuine requirement for creating a custom adapter.

Thank you.

Bob Bob
  • 154
  • 1
  • 11
  • use `FilterQueryProvider` so you dont need to implement custom `Filter` – pskink Sep 04 '15 at 14:52
  • Thanks for the suggestion but isn't mine a simpler technique? – Bob Bob Sep 05 '15 at 12:43
  • no its's not, it is more complex, BTW what is your Filter for if you dont use `constraint` parameter in `performFiltering` method in any way? – pskink Sep 05 '15 at 15:53
  • Oh ok. I shall look into it. That part of code I posted was for the sake of simplicity. I actually use `constraint` to query a web API. Is this possible with `FilterQueryProvider`? – Bob Bob Sep 05 '15 at 16:07
  • 1
    try this ~30 lines of code: http://stackoverflow.com/a/19860624/2252830, don't forget to add `INTERNET` permission in the manifest – pskink Sep 05 '15 at 16:39
  • BTW you can also override `CursorAdapter#runQueryOnBackgroundThread` in order to do the same (no need for `FilterQueryProvider`) – pskink Sep 05 '15 at 16:46
  • I used the first method you suggested and it works fine. Thanks! – Bob Bob Sep 15 '15 at 17:01

0 Answers0