对于微调项方法单击监听器与SimpleCursorAdapter和ViewBinder [英] Method for Spinner item click listener with SimpleCursorAdapter and ViewBinder

查看:261
本文介绍了对于微调项方法单击监听器与SimpleCursorAdapter和ViewBinder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我,我用填充数据从光标 A 微调元素的 SimpleCursorAdapter 。另外我使用 setViewBinder 微调的自定义行布局。一切顺利精细,微调获取数据和微调项中的自定义布局。

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>

和这里的 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);
}

添加 OnItemSelectedListener 想在这个职位建议下开始实施类似下面,但不解决问题。另外我不知道如何使用 setOnItemSelectedListener 可以帮助我得到我需要以后的数据字段:

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

        }

        });

推荐答案

在这里我们去:

其neccessary设置 adapter.setDropDownViewResource(R.layout.spinner_row);
该DropDownView定义DropDownView并在SimpleCursorAdapter构造定义的布局的外观定义(公开)喷丝对象本身的项的布局(未其下拉视图!)。

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!).

所以,其最好有用于这exacly像在SimpleCursorAdapter定义,因此值推到它可以被设置为相应的字段,除了与我使用<$ C $差右边的一项所述的DropDownView不同的布局C>的android:layout_height =机器人:ATTR /列表preferredItemHeight的dropdownview布局和android的TextView的:layout_height =WRAP_CONTENT为微调布局的TextView

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!

这篇关于对于微调项方法单击监听器与SimpleCursorAdapter和ViewBinder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆