Method for Spinner item click listener with SimpleCursorAdapter and ViewBinder

omni picture omni · Nov 6, 2011 · Viewed 9.4k times · Source

I got a Spinner element which I populate with data from a Cursor using a SimpleCursorAdapter. Also I'm using setViewBinder for a custom row layout of the Spinner. Everything works out fine, the Spinner gets the data and the Spinner items use the custom layout.

But clicking the items from the Spinner drop down view doesn't do anything. It doesn't set the selected item as selected and doesn't close the drop down view. I don't know what I have to do so the selected item from the list is passed to the Spinner logic and runs like it should. Here's the layout I'm using:

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

<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:baselineAligned="false"
    android:clickable="true"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:src="@drawable/ic_launcher" />


    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="6dp"
        android:layout_weight="1"
        android:textColor="#424242"
        android:gravity="center_vertical"
        android:text="Textfield" />

</LinearLayout>
</LinearLayout>

and here's the ViewBinder:

static final ViewBinder VIEW_BINDER = new ViewBinder(){
    public boolean setViewValue(View view, Cursor cursor, int columnIndex){

        if (view.getId() == R.id.text){

            String local = view.getResources().getString(cursor.getInt(columnIndex));
            ((TextView) view).setText( local );

            return true;
        }
        if (view.getId() == R.id.icon){

            int icon = cursor.getInt(columnIndex);
            ((ImageView) view).setImageResource(icon);

            return true;
        }

        return false;
    }
};

and here's how I add the data to the Spinner:

private Spinner spinner;
private DBHandler dbhandler;
private SimpleCursorAdapter adapter;
private final String[] from = new String[]{dbhandler.LIB_LOCAL, dbhandler.LIB_ICON};
private final int[] to = { R.id.text, R.id.icon };  
@Override
protected void onResume(){
    super.onResume();

    Cursor cursor = dbhandler.getLibEntries();


    adapter = new SimpleCursorAdapter(this, R.layout.spinner_row, cursor, from, to);
    adapter.setViewBinder(VIEW_BINDER);
    spinner.setAdapter(adapter);
}

Adding a OnItemSelectedListener like suggested down in this post was implemented like below, but doesn't solve the problem. Also I'm not sure how the setOnItemSelectedListener could help me to get the data fields I need later on:

    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub

        }

        });

the problem

Answer

Kurtis Nusbaum picture Kurtis Nusbaum · Nov 6, 2011

What you should do is implement an OnItemSelectedListener. In the listener, when ever an item is selected save that item to some sort of variable you can access after the spinner is closed.