填充微调从SQLite数据库的Andr​​oid [英] Populating Spinner From SQLite Database Android

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

问题描述

我试图做一个动态下拉将由一个SQLite表填写。我有一个游标对象,我可以拉我需要的数据。我已经能够做到把值装载入的下拉低于code:

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数据库的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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