我怎样才能以设定在Android微调中所选项目的行数据集中的位置? [英] How can I get the position of a row in a dataset in order to set the selected item in an Android Spinner?

查看:97
本文介绍了我怎样才能以设定在Android微调中所选项目的行数据集中的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用从数据库中包含数据的光标填充一个微调。看来,这是不可能的设置使用行的ID列的值微调所选项目。并且设置所选项目的唯一方法是通过使用数据集内的行的位置的。它是否正确?如果是这样,有确定比使用光标通过迭代数据行的位置的更有效的方法?

I have a Spinner that's populated using a cursor containing data from a database. It appears that it's impossible to set the selected item in the spinner using the value of the ID column of a row. And that the only way to set the selected item is by using the position of the row within the dataset. Is this correct? If so, is there a more efficient way of determining the row's position than by iterating through the dataset using the cursor?

低效率(在我看来)的方式概述<一href=\"http://stackoverflow.com/questions/2559611/setselection-on-spinner-based-on-rowid\">here.

The inefficient (to my mind) way is outlined here.

谢谢了!

推荐答案

第一步,为您的数据集创建视图,联接等。

First step, create view for your data set, with joins etc.:

CREATE VIEW my_view AS
  SELECT _id, field FROM my_table

第二步:

CREATE VIEW my_view2 AS
  SELECT count(*) AS row_id, q1.*
  FROM my_view AS q1
  LEFT JOIN my_view AS q2
  WHERE q1._id >= q2._id
  GROUP BY q1._id

然后简单:

SELECT * FROM my_view2

结果:

row_id | _id | field

1   4   XbMCmUBFwb
2   6   Te JMejSaK
3   8   dDGMMiuRuh
4   10  phALAbnq c
5   11  EQQwPKksIj
6   12  PAt tbDnf
7   13  f zUSuhvM
8   14  TIMBgAGhkT
9   15  OOcnjKLLER

要通过id来获取位置:

To get position by id:

SELECT * FROM my_view2 WHERE _id=11

结果:

row_id | _id | field

5   11  EQQwPKksIj

希望这种帮助

这篇关于我怎样才能以设定在Android微调中所选项目的行数据集中的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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