1

我得到了一个Spinner元素,我使用来自 a 的数据填充了Cursor一个SimpleCursorAdapter. 我也setViewBinder用于自定义行布局Spinner。一切正常,Spinner获取数据和Spinner项目使用自定义布局。

但是从Spinner下拉视图中单击项目不会执行任何操作。它不会将所选项目设置为选中,也不会关闭下拉视图。我不知道我必须做什么,以便将列表中的选定项目传递给Spinner逻辑并按应有的方式运行。这是我正在使用的布局:

<?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>

这是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;
    }
};

这是我将数据添加到的方式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);
}

在这篇文章中添加一个OnItemSelectedListener类似建议的方法如下所示,但不能解决问题。此外,我不确定如何setOnItemSelectedListener帮助我获得稍后需要的数据字段:

    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

        }

        });

问题

4

2 回答 2

0

你应该做的是实现一个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.

于 2011-11-06T21:21:26.180 回答
0

开始了:

设置adapter.setDropDownViewResource(R.layout.spinner_row); DropDownView 定义了 DropDownView 的外观,SimpleCursorAdapter 构造函数中定义的布局定义了(关闭的)微调器对象本身的项目的布局(不是它的下拉视图!)。

因此,很高兴为 DropDownView 提供不同的布局,这与 SimpleCursorAdapter 中定义的布局完全相同,因此推送给它的值可以设置为正确的相应字段,除了我android:layout_height="?android:attr/listPreferredItemHeight"用于下拉视图布局的 textview 的差异和 android:layout_height="wrap_content" 用于微调器布局的文本视图!

于 2011-11-07T17:32:07.343 回答