2

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!

Community
  • 1
  • 1
Julian A.
  • 10,928
  • 16
  • 67
  • 107

1 Answers1

1

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

pawelzieba
  • 16,082
  • 3
  • 46
  • 72