3

I am using a AutoCompleteTextView in my code and loading the list from database using SimpleCursorAdapter.

AutoCompleteTextView cocktailIngredientView = (AutoCompleteTextView) findViewById(R.id.item);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                android.R.layout.simple_spinner_item, mCursor,
                new String[] { "field" },
                new int[] { android.R.id.text1 });
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cocktailIngredientView.setAdapter(adapter);
cocktailIngredientView.setThreshold(0);

It populates the list correctly but I have two issues:

  1. I want this list to be sorted
  2. Whatever I enter, it displays the complete list. I want it to filter based on matching patterns in the list. e.g. if the list contains values Page, Tools...then if I enter T in the box, the drop-down should show only Tools. The idea is to display options which contain the entered pattern anywhere in the string text.

How can this be done?

1
  • Further when i select any item in the drop down it shows android.database.sqlite...
    – Sapan
    Apr 16, 2011 at 9:53

2 Answers 2

4

You have to tell the adapter what items to display. I tried implementing something similar to this by using a FilterQueryProvider that queries the database for the items that I want to display in the dropdown.

FilterQueryProvider filter = new FilterQueryProvider() {

    @Override
    public Cursor runQuery(CharSequence constraint) {
        // Make a DB query that filters based on the constraint

        return //whatever query results;
    }
};
myAdapter.setFilterQueryProvider(filter);

As for the situation when you select an item on the list, you have to override the CursorToStringConverter of the SimpleCursorAdapter. Something like:

SimpleCursorAdapter.CursorToStringConverter conv = new SimpleCursorAdapter.CursorToStringConverter() {

    @Override
    public CharSequence convertToString(Cursor cursor) {
        int numCol = cursor.getColumnIndexOrThrow("whateverFieldYouNeed");
        String term = cursor.getString(numCol);
        return term;
    }
};
myAdapter.setCursorToStringConverter(conv);
2
  • Thanks for the answer. It really worked for me.But using the filterquery provider, one thing that i observed is that the last item in the filtered list doesnt appear fully. I see a scroll bar, and to view it fully I have to scroll down. It is by default visible only half.
    – Sapan
    Apr 21, 2011 at 8:20
  • Try playing around with the setDropDownHeight() [1] method to adjust how many items are visible in the dropdown menu. [1] developer.android.com/reference/android/widget/…
    – Zarah
    Apr 21, 2011 at 18:18
0

Instead of the CursorToStringConverter you could also use

mAdapter.setStringConversionColumn(mCursor.getColumnIndexOrThrow("whateverFieldYouNeed"));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.