nagoya0
nagoya0

Reputation: 2828

About the efficient query for displaying on ListView

I'm a beginner of SQL. I would like to display sqlite data on Android listview. The table has the following structure.

| _id | name | data | ...
---------------------
|   0 |   A  |  abc |
|   1 |   B  |  def |
|   2 |   C  |  ghi | ...
|   3 |   D  |  jkl |
|   4 |   E  |  mno |

So, when the user inputs [C, B, D], I want to display name and data column in user's order. For example,

ListView

---------------------
     C     ghi
---------------------
     B     def
---------------------
     D     jkl
---------------------

I'm torn between using ArrayAdapter and CursorAdapter now.

Should I do SELECT 3 times, store values in array, and use ArrayAdapter? Or, can fulfill my demands with SELECT once?

Thanks in advance.

Upvotes: 1

Views: 91

Answers (2)

st0le
st0le

Reputation: 33545

It'll be a bit complicated but it can be done in a single query.

SELECT * FROM yourTable
ORDER BY 
  CASE name
    WHEN 'C' THEN 0
    WHEN 'B' THEN 1
    WHEN 'D' THEN 2
  END

You'll have to construct the query according to the user input ofcourse . Look more here

Upvotes: 1

waqaslam
waqaslam

Reputation: 68187

You can fulfill your demands with SELECT query. Your query would look like:

String query = "SELECT * FROM yourTable WHERE name IN ('C', 'B', 'D');"

Upvotes: 0

Related Questions