Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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

    Cursor cComments = myAdapter.getDistinctComments();
    scaComments = new SimpleCursorAdapter(this,R.layout.auto_complete_item,cComments,new String[] {DBAdapter.KEY_LOG_COMMENT},new int[]{R.id.text1});

    mComment.setAdapter(scaComments);

auto_complete_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

and thi is the xml for the actual control

<AutoCompleteTextView
                        android:id="@+id/comment"
                        android:hint="@string/COMMENT"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:textSize="18dp"/>

The dropdown appears to work correctly, and shows a list of items. When I make a selection from the list I get a sqlite object ('android.database.sqlite.SQLiteCursor@'... ) in the textview. Anyone know what would cause this, or how to resolve this?

thanks

Ok I am able to hook into the OnItemClick event, but the TextView.setText() portion of the AutoCompleteTextView widget is updated after this point. The OnItemSelected() event never gets fired, and the onNothingSelected() event gets fired when the dropdown items are first displayed.

       mComment.setOnItemClickListener( new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub

            SimpleCursorAdapter sca = (SimpleCursorAdapter) arg0.getAdapter();


            String str = getSpinnerSelectedValue(sca,arg2,"comment");

            TextView txt = (TextView) arg1;
            txt.setText(str);
            Toast.makeText(ctx, "onItemClick", Toast.LENGTH_SHORT).show();

        }

    });
    mComment.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {


            Toast.makeText(ctx, "onItemSelected", Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
            Toast.makeText(ctx, "onNothingSelected", Toast.LENGTH_SHORT).show();
        }

    });

Anyone alse have any ideas on how to override the updating of the TextView?

thanks

patrick

like image 908
bugzy Avatar asked Jan 29 '10 03:01

bugzy


People also ask

What is autocompletetextview in Android?

AutoCompleteTextView is yet another basic controls in Android. The main purpose is to provide a suggestion list while you type on the textbox (as in the image below). This time instead of listing a regular ArrayList, I will show how to populate the list from a offline database table through SQLite.

How to use SELECT query in Android SQLite?

This example demonstrate about How to use SELECT Query in Android sqlite. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.

What is an open source SQLite database for Android?

SQLite is an opensource SQL database that stores data to a text file on a device. Android comes in with built in SQLite database implementation. SQLite supports all the relational database features. In order to access this database, you don't need to establish any kind of connections for it like JDBC, ODBC etc.

What is the purpose of a suggestion list in SQLite?

The main purpose is to provide a suggestion list while you type on the textbox (as in the image below). This time instead of listing a regular ArrayList, I will show how to populate the list from a offline database table through SQLite.


2 Answers

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.

like image 106
plainjimbo Avatar answered Oct 13 '22 11:10

plainjimbo


[Late answer, just for the record. EDITed to remove my suggestion that subclassing is necessary.]

To use SimpleCursorAdapter with an AutoCompleteTextView, you need to set two handlers on the adapter: The CursorToStringConverter, and the FilterQueryProvider. Pseudocode follows:

    adapter.setCursorToStringConverter(new CursorToStringConverter() {
        public String convertToString(android.database.Cursor cursor) {
            // Assume that "someColumn" contains the strings that we want to
            // use to identify rows in the result set.
            final int columnIndex = cursor.getColumnIndexOrThrow("someColumn");
            final String str = cursor.getString(columnIndex);
            return str;
        }
    });

    adapter.setFilterQueryProvider(new FilterQueryProvider() {
        public Cursor runQuery(CharSequence constraint) {
            // runSomeQuery will look for all rows in the database
            // that match the given constraint.
            Cursor cursor = runSomeQuery(constraint);
            return cursor;
        }
    });
like image 44
Dan Breslau Avatar answered Oct 13 '22 11:10

Dan Breslau