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

发布于 2024-12-14 06:21:30 字数 3326 浏览 1 评论 0原文

我得到了一个 Spinner 元素,我使用 SimpleCursorAdapter 使用来自 Cursor 的数据填充该元素。此外,我还使用 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

        }

        });

“问题”

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

囍孤女 2024-12-21 06:21:30

你应该做的是实现 OnItemSelectedListener。在侦听器中,每当选择一个项目时,都会将该项目保存到某种变量中,您可以在旋转器关闭后访问该变量。

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.

眼波传意 2024-12-21 06:21:30

我们开始:

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

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

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文