Method for Spinner item click listener with SimpleCursorAdapter and ViewBinder

橙三吉。 提交于 2020-01-15 11:42:47

问题


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

        }

        });


回答1:


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.




回答2:


here we go:

its neccessary to set adapter.setDropDownViewResource(R.layout.spinner_row); the DropDownView defines the look of the DropDownView and the layout defined in the SimpleCursorAdapter constructor defines the layout of the items of the (closed) spinner object itself (not its drop down view!).

so, its nice to have a different layout for the DropDownView which is exacly like the one defined in the SimpleCursorAdapter so the values pushed to it can be set to the right corresponding fields except with the difference that i use android:layout_height="?android:attr/listPreferredItemHeight" for the textview of the dropdownview layout and android:layout_height="wrap_content" for the textview of the spinner layout!



来源:https://stackoverflow.com/questions/8030417/method-for-spinner-item-click-listener-with-simplecursoradapter-and-viewbinder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!