views:

1104

answers:

2

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

A: 

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
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.
"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
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.
Alright, I think I may have figured it. Setup a OnItemSelectedListener(). I'll answer after I test it.
:: 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
+1  A: 

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 them correct doc pages.

James