从 SQLite 数据库 Android 填充 Spinner [英] Populating Spinner From SQLite Database Android

查看:47
本文介绍了从 SQLite 数据库 Android 填充 Spinner的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个将由 SQLite 表填充的动态下拉列表.我有一个 Cursor 对象,我可以从中提取我需要的数据.我已经能够使用以下代码将值加载到下拉列表中:

I'm attempting to make a dynamic drop down that will be filled by a SQLite table. I have a Cursor object which I can pull the data I need from. I've been able to accomplish loading the values into the drop down with the code below:

Spinner s = (Spinner) findViewById(R.id.spinner);
    ArrayAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    s.setAdapter(adapter);

    try{
        Cursor cursor = getAccounts();
        int accountnameIndex = cursor.getColumnIndexOrThrow(ACCOUNT_NAME);
        if(cursor.moveToFirst()){
            do{
                adapter.add(cursor.getString(accountnameIndex));
            } while(cursor.moveToNext());
        }
    } finally {
        MintLink.close();
    }

我的问题是我需要从下拉列表中选择一个还包含所选项目的 RowID.我需要能够选择一个项目并在后端访问该项目的值.例如,想想 HTML 中的下拉菜单.每个下拉选择都有自己的隐藏值被拉出.我需要为我隐藏这个值,让我知道他们选择了哪个 ID.

My problem is that I need the a selection from the drop down to also contain the RowID of the item selected. I need to be able to select one item and have access to the value of that item in the back end. For example, think of a drop down in HTML. Each drop down selection has it's own hidden value that is pulled. I need this value to be hidden for me to allow me to know which ID they choose.

推荐答案

尝试使用 SimpleCursorAdapter 而不是手动将所有数据复制到 ArrayAdapter.

Try using a SimpleCursorAdapter instead of copying all the data by hand into an ArrayAdapter.

这篇关于从 SQLite 数据库 Android 填充 Spinner的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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