创建适配器以用对象填充 Spinner

发布于 2024-11-17 10:20:15 字数 1566 浏览 6 评论 0原文

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

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

发布评论

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

评论(3

与他有关 2024-11-24 10:20:15

这是我的 5 美分。我有类似的问题。我正在使用 SimpleCursorAdapter,它实现了 SpinnerAdapter 接口,但直到 SDK 版本 11 (Android 3.0) 才出现。我希望我的应用程序能够与 SDK 8 (Android 2.2) 及更高版本一起使用,因此我必须将 SimpleCursorAdapter 替换为另一个或我自己的。真正的挑战是我还为微调器使用了自定义 XML 布局,并在其中显示了光标(即光标适配器)中的多个字段。这是我经过大量研究后的解决方案,而这些信息并不容易获得。

这是 Spinner 中使用的名为 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 的适配器。最初使用的 Cursor 被转换为 List 并与包含微调器的 Activity 一起传入构造函数。

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 是在 spinner.setOnItemSelectedListener 的 OnItemSelectedListener 中的 onItemSelected(AdapterView arg0, View arg1, int position, long id) 中传递的参数。方法getView扩展了spin_layout.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.

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>

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;
    }
}

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

忘年祭陌 2024-11-24 10:20:15

这是一个简单示例。不要被“光标”名称所迷惑,它只是使用一个列表。这个想法很简单:从 BaseAdapter 扩展并实现任何缺少的方法(它是一个抽象类);并且不要忘记重写 getView() 方法以提供 Category 的“视觉”表示。

This is a simple example. Don't be fooled by the "cursor" name, it's just using a List. The idea is simple: extend from BaseAdapter and implement any missing methods (it's an abstract class); and don't forget to override the getView() method to provide the "visual" representation of your Category.

迷爱 2024-11-24 10:20:15

我不确定这是否有帮助,但 Android SDK 有一个很好的小示例,说明从数组向 Spinner 提供数据。将列表转换为数组很简单,所以也许这会为您指明正确的方向。

http://developer.android.com /resources/samples/Spinner/src/com/android/example/spinner/SpinnerActivity.html

祝你好运。

I am not sure if this helps, but the Android SDK has a nice little example of supplying data to a Spinner from an array. Converting a List to an array is trivial, so maybe this will point you in the right direction.

http://developer.android.com/resources/samples/Spinner/src/com/android/example/spinner/SpinnerActivity.html

Good luck.

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