创建适配器来填充微调使用对象 [英] create adapter to fill Spinner with objects

查看:140
本文介绍了创建适配器来填充微调使用对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个微调 Android应用程序,并希望与我自己的对象动态填充它。对象不存在为列表< T>

I have an Android application with a Spinner and want to fill it dynamically with my own objects. The objects do exist already as a List<T>.

对象是类型类别

public class Category implements Serializable {
    private Long id;
    private String name;

    // constructors
    // getter & setter
    // hashCode, equals
    // toString
}

我知道,我必须写一个适配器。我怎么做?我试图找到一些例子......没有运气。请指点。

I know that I have to write a Adapter. How do I do that? I've tried to find some examples... no luck. Please advice.

推荐答案

这是我5美分。我有一个类似的问题。我与SimpleCursorAdapter它实现接口SpinnerAdapter工作,但到了只有等到SDK版本11(Android 3.0的)。我打算我的应用程序与8 SDK(Android 2.2的)和最多的工作,所以只好用另一个,还是我自己来代替SimpleCursorAdapter。真正的挑战者是,我还使用自定义XML布局微调,并在其中表现出的光标即光标适配器多个领域。因此,这里是大量的调查研究后,我的解决方案,以及信息并不容易获得。

Here is my 5 cents. I had a similar problem. I was working with SimpleCursorAdapter which implements interface SpinnerAdapter, but arrived only until SDK version 11 (Android 3.0). I intended my app to work with SDK 8 (Android 2.2) and up, so I had to replace SimpleCursorAdapter with another, or my own. The real challenger was that I also used a custom XML layout for spinner and in it showed several fields from cursor i.e. cursor adapter. So here is my solution after a lot of research, and the info wasn't easy to come by.

下面是在一个名为spin_layout.xml微调使用的布局文件:

Here is the layout file used in spinner named spin_layout.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:orientation="horizontal" >
<TextView 
    android:id="@+id/field1"
    android:textColor="#000"
    android:gravity="center"
    android:layout_width="40dp"
    android:layout_height="wrap_content"
    android:textSize="24sp" />
<TextView 
    android:id="@+id/field2"
    android:textColor="#000"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:textSize="24sp" />
</LinearLayout>

这是实现SpinnerAdapter和扩展(使用作为一个小帮手)BaseAdapter适配器。这是最初使用的游标与含有微调活动转化成列表,并在构造函数中传递,共同提高。

And here is the adapter implementing SpinnerAdapter and extending (using as a little helper) BaseAdapter. The Cursor that was used originally was transformed into List and passed in constructor, together with activity containing the spinner.

public class MyCursorAdapter extends BaseAdapter implements SpinnerAdapter{
    private Activity activity;
    private List<BusLines> list_bsl; 

    public MyCursorAdapter(Activity activity, List<BusLines> list_bsl){
        this.activity = activity;
        this.list_bsl = list_bsl;
    }

    public int getCount() {
        return list_bsl.size();
    }

    public Object getItem(int position) {
        return list_bsl.get(position);
    }

    public long getItemId(int position) {
        return list_bsl.get(position).getId();
    }

    public View getView(int position, View convertView, ViewGroup parent) {

    View spinView;
    if( convertView == null ){
        LayoutInflater inflater = activity.getLayoutInflater();
        spinView = inflater.inflate(R.layout.spin_layout, null);
    } else {
         spinView = convertView;
    }
    TextView t1 = (TextView) spinView.findViewById(R.id.field1);
    TextView t2 = (TextView) spinView.findViewById(R.id.field2);
    t1.setText(String.valueOf(list_bsl.get(position).getLine_Num()));
    t2.setText(list_bsl.get(position).getName());
    return spinView;
    }
}

不像其他的解决方案,你发现在网络上,方法的 getItemId 建立与 ID 从数据库字段,就像SimpleCursorAdapter链接。该ID是通过 onItemSelected (适配器视图为arg0,ARG1查看,INT位置,长的标识)的OnItemSelectedListener对于 spinner.setOnItemSelectedListener 的说法。方法 getView 充气的 spin_layout.xml ,标识包含在布局中的两种观点并赋予它们的值(如字符串!)。

Unlike other solutions you find on the web, method getItemId establishes link with id field from database, just like SimpleCursorAdapter. That id is the argument passed in onItemSelected(AdapterView arg0, View arg1, int position, long id) in OnItemSelectedListener for spinner.setOnItemSelectedListener. Method getView inflated spin_layout.xml, identifies the two views contained in layout and assigns them values (as String!).

这篇关于创建适配器来填充微调使用对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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