开发者

How can I get the position of a row in a dataset in order to set the selected item in an Android Spinner?

开发者 https://www.devze.com 2023-02-11 04:48 出处:网络
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 co

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?

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

Thanks much!


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

CREATE VIEW my_view AS
  SELECT _id, field FROM my_table

Second step:

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

Then simply:

SELECT * FROM my_view2

Results:

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

To get position by id:

SELECT * FROM my_view2 WHERE _id=11

Results:

row_id | _id | field

5   11  EQQwPKksIj

Hope that help

0

精彩评论

暂无评论...
验证码 换一张
取 消