Android的绑定微调上课 [英] Android Bind Spinner to Class

查看:78
本文介绍了Android的绑定微调上课的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在与微调控件一些麻烦。鉴于以下code:

I'm having some trouble with the Spinner widget. Given the following code:

    ArrayList<Person> people= new ArrayList<Person>();

    Person = null;
    for(int i = 0; i!= 10; i++) {
        p = new Person();
        s.setID(i);
        s.setName("Name " + i);
        people.add(s);
    }

我用下面的code将其绑定到一个微调:

I'm using the following code to bind it to a Spinner:

 Spinner spinner1 = (Spinner) findViewById  (R.id.spinner);       
 ArrayAdapter<Person> adapter = new ArrayAdapter<Person>(this, android.R.layout.simple_spinner_item, people);                           
 spinner1.setAdapter(adapter);  

我想是的值(ID)被隐藏,但通过选择何时,显示的名称。任何帮助是AP preciated。

What I would like is for the value (id) to be hidden but passed when selected, and the name to appear. Any help is appreciated.

感谢。

推荐答案

如果我理解你的问题正确,你会喜欢这个名字出现在微调下拉列表视图。你会当一个项目被选中想回到的ID。

If I understand your question correctly you would like the name to appear in the spinner dropdown view. When an item is selected you would like to return the id.

有2种方式可以处理这个。

There are 2 ways you can approach this.

最简单的方法是实现一个toString()返回在Person对象的名称。一个ArrayAdapter将结合您的对象的文本值时,下拉视图返回此值。 然后在你的活动,你可以设置onItemClickListener的微调,并呼吁adapter.getItemAtPosition(位置)来获取Person对象。从该人的对象,你可以得到你的ID。

The simplest way is to implement a toString() that returns the name in your Person object. The ArrayAdapter will return this value when binding the text values of your object to the dropdown view. Then in your activity you can set the onItemClickListener for the Spinner and call on the adapter.getItemAtPosition(position) to retrieve the Person object. From that person object you can get your id.

第二种方法是延长一个ArrayAdapter和实施getView和getDropDownView和的getItem方法。该getView负责创建你看,当您单击微调行。该getDropDownView负责创建您的微调看风景。而为getItem方法将返回对象或在您的案件中的id在那个指定的位置。你应该Person元素的名称绑定到你的getView和getDropDownView方法一个TextView。当你创建自定义ArrayAdapter您应该设置onItemClicKListener的微调和处理它,就像我上面提到的。

The second approach is to extend the ArrayAdapter and implement the getView and getDropDownView and getItem methods. The getView is responsible for creating the rows you see when you click the spinner. The getDropDownView is responsible for creating the view you see in the spinner. And the getItem method will return the object or in your case the id at that specified position. You should bind the name of the Person element to a textview in your getView and getDropDownView methods. After you've created your custom ArrayAdapter you should set the onItemClicKListener for the Spinner and handle it just as I have mentioned above.

第一种方法很简单,但第二种方法是远远优越,将产生更大的控制你的适配器,尤其是当你开始开发的东西复杂得多。

The first approach is simple but the second approach is far superior and will yield much greater control over your adapter, especially when you begin to develop something much more complex.

希望有所帮助。

这篇关于Android的绑定微调上课的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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