10

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

bugzy
  • 7,086
  • 9
  • 42
  • 44

4 Answers4

9

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.

plainjimbo
  • 7,070
  • 9
  • 41
  • 55
3

[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;
        }
    });
Dan Breslau
  • 11,472
  • 2
  • 35
  • 44
0

When I make a selection from the list I get a sqlite object ('android.database.sqlite.SQLiteCursor@'... ) in the textview.

You do not say what this "textview" is or how it relates to the Spinner.

I am going to take an educated guess and assume that you are simply assigning the selected item out of the Spinner into the TextView.

The selected item from a Spinner using a SimpleCursorAdapter is a Cursor, pointing at the row the user selected. The toString() implementation of Cursor will give you something akin to android.database.sqlite.SQLiteCursor@ depending on where the Cursor came from.

More likely, you are going to want to call getString() on that Cursor, to retrieve some column value, and assign it to the TextView in question.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Yes, you are correct. I am simply selecting one of the options from the spinner which is created as part of the AutoCompleteTextView. Where or how do I intercept the functionlity to process the selection; use getString() on the cursor, before it is set as the text of the control. I also will add my xml. – bugzy Jan 29 '10 at 12:30
  • "Where or how do I intercept the functionlity to process the selection" -- you're already doing that. Somewhere, you are calling `setText()` on the TextView. Edit that code. – CommonsWare Jan 29 '10 at 12:46
  • That's the point...Im not. It is the built in behavior of the AutoCompleteTextView widget that is populating the TextView portion of the AutoCompleteTextView widget. – bugzy Jan 29 '10 at 13:32
  • Alright, I think I may have figured it. Setup a OnItemSelectedListener(). I'll answer after I test it. – bugzy Jan 29 '10 at 13:57
  • :: smacks forehead :: Sorry, I was having a mental block here. I haven't used a `SimpleCursorAdapte`r with an `AutoCompleteTextView`, so I don't know the correct recipe for that combination. – CommonsWare Jan 29 '10 at 14:43
0

To solve the problem I just extended SimpleCursorAdapter and implemented the method convertToString(). Then I created an instance and set it as the adapter.

In order to allow filtering in AutoCompleteTextView when using CursorAdapters I also used setFilterQueryProvider(). See this question.

My extended class inside the Activity looks as:

private static class AutoCompleteCursorAdapter extends SimpleCursorAdapter {

    public AutoCompleteCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
        super(context, layout, c, from, to);
    }

    @Override
    public CharSequence convertToString(Cursor cursor) {
        // This is the method that does the trick (return the String you need)
        return cursor.getString(cursor.getColumnIndex("name"));
    }
}
Community
  • 1
  • 1
Caumons
  • 9,341
  • 14
  • 68
  • 82