Android - AutoCompleteTextView wildcard suggestion

Good afternoon. I have an AutoCompleteTextView app in an Android app and it works fine. However, I noticed that sentences are based on the first character of the substrings of the list provided in AutoCompleteTextView. However, this is in itself, but I want it to also show elements that contain user input.

For example, let me use this list:

  • Fat
  • Bad Wolf
  • Cybermen
  • Far away

Input in adwill offer Adipose, however I also want to offer Bad Wolf, as it contains adin Bad. This will not happen, because AutoCompleteTextView looks only at the beginning of the substrings (the substring is separated by a space) in the list items, and not inside these substrings.

Is there a way to get AutoCompleteTextViews to suggest elements that contain input text, regardless of where the text falls inside the list item?

Thanks for any help.

EDIT / UPDATE

Please see psklink below. I tried to implement the same solution as follows.

The logic from what I suggested is that it should be used SimpleCursorAdapter, not general ArrayAdater. Then I created FilterQueryProviderfor SimpleCursorAdapter. Using the method runQuery FilterQueryProvider, I can now start the filter algorithm by searching my input list for user restrictions. Here is the code:

//initialize the ACTV
AutoCompleteTextView search = (AutoCompleteTextView) findViewById(R.id.actvCatalogueSearch);
search.setThreshold(1); //set threshold

//experiment time!!

//I honestly don't know what this is for
int[] to = { android.R.id.text1 };

//initializing the cursorAdapter. 
//please note that pdflist is the array I used for the ACTV value
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, 
        android.R.layout.simple_dropdown_item_1line, null, pdflist, to, 0);

cursorAdapter.setStringConversionColumn(1);

//FilterQueryProvider here
FilterQueryProvider provider = new FilterQueryProvider(){
    @Override
    public Cursor runQuery(CharSequence constraint) {
        // TODO Auto-generated method stub
        Log.d("hi", "runQuery constraint: " + constraint);
        if (constraint == null) {
            return null;
        }
        String[] columnNames = { Columns._ID, "name" };
        MatrixCursor c = new MatrixCursor(columnNames);

        try {
            //loop through the array, then when an array element contains the constraint, add.
            for (int i = 0; i < pdflist.length; i++) {
                if(pdflist[i].contains(constraint)){
                    c.newRow().add(i).add(pdflist[i]);  
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return c;
    }
};

cursorAdapter.setFilterQueryProvider(provider);
search.setAdapter(cursorAdapter);

The Log for statement is shown runQuery constraint, after which the application crashes, and I get this error in my logcat:

requesting column name with table name -- <first element of array here> 
.....
java.lang.IllegalArugmentException: column <first element of array here> does not exist

logCat jar, . , , , - , String[] columnNames MatrixCursor.

- ? - , , .

. .

+4
1

, . . , , , runQuery .

/, runQuery. .

//create ACTV Here
AutoCompleteTextView search = (AutoCompleteTextView) findViewById(R.id.actvCatalogueSearch);
search.setThreshold(1);

//I don't know what these are for, honestly.
String[] from = { "name" };
int[] to = { android.R.id.text1 };

//create a simple cursorAdapter
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, 
        android.R.layout.simple_dropdown_item_1line, null, from, to, 0);

//again, I don't know what this is for
cursorAdapter.setStringConversionColumn(1);

//create the filter query provider
FilterQueryProvider provider = new FilterQueryProvider(){
    @Override
    public Cursor runQuery(CharSequence constraint) {
        // TODO Auto-generated method stub
        //I need to do this because my list items are in all caps
        String constrain = (String) constraint;
        constrain = constrain.toUpperCase(); 

        if (constraint == null) {
            return null;
        }

        //I'll be honest again, no clue what these lines do. 
        String[] columnNames = { Columns._ID, "name" };
        MatrixCursor c = new MatrixCursor(columnNames);

        try {
            //here what I do, I go though my Array (pdflist)
            //when a list item contains the user input, I add that to the Matrix Cursor
            //this matrix cursor will be returned and the contents will be displayed 
            for (int i = 0; i < pdflist.length; i++) {
                if(pdflist[i].contains(constrain)){
                    c.newRow().add(i).add(pdflist[i]);  
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return c;
    }
};

//use the filter query provider on the cursor adapter
cursorAdapter.setFilterQueryProvider(provider);

//finally, use the adapter on your ACTV
search.setAdapter(cursorAdapter);

, . , , "/" . -, / - AutoCompleteTextView, .

, .

+2

All Articles