AutoCompleteTextView displays 'android.database.sqlite.SQLiteCursor@'… after making selection

前端 未结 4 1745
春和景丽
春和景丽 2021-02-08 10:25

I am using the following code to set the adapter (SimpleCursorAdapter) for an AutoCompleteTextView

mComment = (AutoCompleteTextView) findViewById(R.id.comment);
         


        
4条回答
  •  梦如初夏
    2021-02-08 11:13

    I don't think you should have to update the text for the AutoCompleteTextView. It should do it automatically. It does this by calling the [CursorAdapter.convertToString(...)][1] method. if you read the description of the method it points this out. So if you were writing your own CursorAdapter you would override that method to return whatever text you would want to show up in the list of suggestions. This guy does a good job of explaining how to do it:

    Line 86 - http://thinkandroid.wordpress.com/2010/02/08/writing-your-own-autocompletetextview/

    However, since you are using a SimpleCursorAdapter, you can't override this method. Instead you need implement/create a [SimpleCursorAdapter.CursorToStringConverter][2] and pass it into [SimpleCursorAdapter.setCursorToStringConverter(...)][3]:

     SimpleCursorAdapter adapter = new SimpleCursorAdapter(context, layout, cursor, from, to);
     CursorToStringConverter converter = new CursorToStringConverter() {
    
        @Override
        public CharSequence convertToString(Cursor cursor) {
           int desiredColumn = 1;
           return cursor.getString(desiredColumn);
        }
     }; 
    
     adapter.setCursorToStringConverter(converter);
    

    Or if you don't want to create a CursorToStringConverter then use the [SimpleCursorAdapter. setStringConversionColumn(...)][4] method. But I think you still have to explicitly set the CursorToStringConverter to null:

     int desiredColumn = 1;
     adapter.setCursorToStringConverter(null);
     adapter.setStringConversionColumn(desiredColumn);
    

    Sorry, but the spam blocker won't let me post the links to the Android Documentation that describes the links I posted above. But a quick google search will point you to the correct doc pages.

提交回复
热议问题