基于ROWID的微调setSelection [英] setSelection on Spinner based on rowId

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

问题描述

我有一个微调查看多数民众赞成通过SimpleCursorAdapter填充。

I have a Spinner View that's populated through a SimpleCursorAdapter.

根据选择我需要保存在 ROWID 在输入数据库(位置将无法工作,因为东西可以添加和删除从微调数据库)。

Based on the selection I need to save the rowid in the entry database (position won't work because things can be added and deleted from the Spinner Database).

这我可以用 spinner.getAdapter()getItemId(POS)做的。 。但是,当我编辑的条目,我需要做微调位置选择与此ROWID(目前)。

This I can do by using spinner.getAdapter().getItemId(pos);. But When I edit an entry I need to make the Spinner position selected that is associated with this rowid (currently).

spinner.setSelection(位置); 将无法工作,因为我有rowid的,我需要一种方法来发现的当前位置根据在数据库中的rowid在当前微调器的项目

spinner.setSelection(position); won't work because I have the rowid, I need a way to find the current position of the item in the current spinner based on the rowid in the database.

推荐答案

如果你想设置的选择一个微调多数民众赞成由支持的 CursorAdapter的,你可以通过在光标中的所有项目环,并查找你想要的(假设你的表的主键被命名为一个_id):

If you want to set the selection of a Spinner thats backed by a CursorAdapter, you can loop through all the items in the Cursor and look for the one you want (assuming that the primary key in your table is named "_id"):

Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setAdapter(new SimpleCursorAdapter(...));

for (int i = 0; i < spinner.getCount(); i++) {
    Cursor value = (Cursor) spinner.getItemAtPosition(i);
    long id = value.getLong(value.getColumnIndex("_id"));
    if (id == rowid) {
        spinner.setSelection(i);
    }
}

如果你想获得所选项目的ROWID,你可以做类似的事情:

If you want to get the rowid of a selected item, you can do something similar:

Cursor cursor = (Cursor) spinner.getSelectedItem();
long rowid = cursor.getLong(cursor.getColumnIndex("_id"));

有可能是一个更快的方式做到这一点,但总是为我工作。

There might be a quicker way to do it, but that's always worked for me.

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

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