Android的:如何绑定微调,以自定义对象名单? [英] Android: How to bind spinner to custom object list?

查看:162
本文介绍了Android的:如何绑定微调,以自定义对象名单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用户界面,必须有一个喷丝其中包含一些名称(名称是可见的)和每个名字具有其自己的ID(ID是不等于显示序列)。当用户从列表中选择的名称的变量currentID必须改变。

In the user interface there has to be a spinner which contains some names (the names are visible) and each name has its own ID (the IDs are not equal to display sequence). When the user selects the name from the list the variable currentID has to be changed.

该应用程序包含的ArrayList

The application contains the ArrayList

如果用户是一个对象ID和名称:

Where User is an object with ID and name:

public class User{
        public int ID;
        public String name;
    }

我不知道的是如何创建一个微调,其显示的用户名和绑定微调物品,所以当微调项目被选中的ID列表/改变变量currentID设置为适当的值。

What I don't know is how to create a spinner which displays the list of user's names and bind spinner items to IDs so when the spinner item is selected/changed the variable currentID is set to appropriate value.

我会AP preciate如果有人能证明所描述的问题的解决方案,或提供解决问题的任何链接有用的。

I would appreciate if anyone could show the solution of the described problem or provide any link useful to solve the problem.

谢谢!

推荐答案

您可以看这个答案。您也可以使用自定义适配器,但下面的解决方案是对简单的场合。

You can look at this answer. You can also go with a custom adapter, but the solution below is fine for simple cases.

下面是一个重职:

所以,如果你来到这里,因为你想拥有这两个标签和值的微调 - 在这里就是我做的:

So if you came here because you want to have both labels and values in the Spinner - here's how I did it:

  1. 只需创建微调通常的方式
  2. 定义2个相同大小的数组在 array.xml 文件 - 一个阵列的标签,一个阵列值
  3. 设置你的微调安卓项=@阵列/标签
  4. 在您需要的值,这样做(不,你不必链吧):

  1. Just create your Spinner the usual way
  2. Define 2 equal size arrays in your array.xml file -- one array for labels, one array for values
  3. Set your Spinner with android:entries="@array/labels"
  4. When you need a value, do something like this (no, you don't have to chain it):

  String selectedVal = getResources().getStringArray(R.array.values)[spinner.getSelectedItemPosition()];

这篇关于Android的:如何绑定微调,以自定义对象名单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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