简体   繁体   English

使用SimpleCursorAdapter和ViewBinder的Spinner项目单击侦听器的方法

[英]Method for Spinner item click listener with SimpleCursorAdapter and ViewBinder

I got a Spinner element which I populate with data from a Cursor using a SimpleCursorAdapter . 我得到了一个Spinner元素,使用SimpleCursorAdapter填充了Cursor中的SimpleCursorAdapter Also I'm using setViewBinder for a custom row layout of the Spinner . 另外,我setViewBinder用于Spinner的自定义行布局。 Everything works out fine, the Spinner gets the data and the Spinner items use the custom layout. 一切正常, Spinner获取数据, Spinner项目使用自定义布局。

But clicking the items from the Spinner drop down view doesn't do anything . 但是从Spinner下拉视图单击项目并没有任何作用 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. 我不知道该怎么办,因此将从列表中选择的项目传递到Spinner逻辑并按应有的方式运行。 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 : 这是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 : 这是我将数据添加到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. 像下面这篇文章中建议的那样添加OnItemSelectedListener实现方式如下,但是并不能解决问题。 Also I'm not sure how the setOnItemSelectedListener could help me to get the data fields I need later on: 另外,我不确定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

        }

        });

问题

What you should do is implement an OnItemSelectedListener . 您应该执行的是实现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. 在侦听器中,一旦选择了某个项目,就将该项目保存到某种类型的变量中,您可以在微调器关闭后访问该变量。

here we go: 开始了:

its neccessary to set adapter.setDropDownViewResource(R.layout.spinner_row); 需要设置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!). DropDownView定义了DropDownView的外观,而SimpleCursorAdapter构造函数中定义的布局定义了(闭合)微调器对象本身(而不是其下拉视图!)的各项的布局。

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! 因此,很高兴为DropDownView具有不同的布局,这完全类似于SimpleCursorAdapter中定义的布局,因此可以将推送到它的值设置为正确的对应字段,区别在于我使用android:layout_height="?android:attr/listPreferredItemHeight"用于dropdownview布局的textview,而android:layout_height =” wrap_content“用于微调器布局的textview!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM