0

I am working on an Android project with a SQLite database and have a column in the database consisting of a boolean value (which store 1s and 0s) of whether an individual in the database is important.

What I am trying to do is output into a textview listview a "!" if an individual is important, and " " if they are not important using a cursor and cursor adaptor. I can get the "1" and "0"s to appear in the listview, but my question is how/where do I convert these to "!" and " "?

The query I am currently using is

return database.query("people", new String[] {"_id", "important"}, null, null, null, null, "important" + " DESC, " + "name"  + " ASC");
brianna_S
  • 13
  • 1
  • 2

2 Answers2

0

If it is a custom ListView which I am pretty sure it is, then you'll have to do that in your CustomAdapter for the ListView, which takes care of populating the ListView with the data you pass to it.Check this out. Doing it is pretty simple, just loop through your Cursor and do if else statement.

Community
  • 1
  • 1
Daniel Conde Marin
  • 7,588
  • 4
  • 35
  • 44
0

The easiest way is to do this in the database query. The query function is too inflexible for that, so you have to use rawQuery instead:

db.rawQuery("SELECT _id, " +
                   "CASE WHEN important THEN '!' ELSE ' ' END AS important " +
            "FROM people " +
            "ORDER BY important DESC, name ASC", null);
CL.
  • 173,858
  • 17
  • 217
  • 259
  • Hi thanks for logic, but the Listviews are still showing the 0s and 1s This is what I am using: `return database.rawQuery("SELECT _id, important, " + "CASE WHEN important THEN important = '*' ELSE ' ' END AS impMarker " + "FROM people " + "ORDER BY important DESC, people ASC", null);` I have tried alternating 'important=', '==' but no success. Any ideas? – brianna_S May 11 '13 at 11:36
  • Why did you change the query string? If you need a result column named `important`, change the `AS`. – CL. May 11 '13 at 12:05
  • Do you mean the first bit when I used `SELECT _id, important, `? When I did not put important in this (as the query you gave me), the program crashed on startup because in my other code I have `String[] from = new String[] { "important" };` to use for my CursorAdaptor – brianna_S May 11 '13 at 12:20
  • You did no mention the `CursorAdapter` in the question. Anyway, the column names must match; you could either have used `impMarker` in the `from` array, or renamed the `CASE` expression to `important` (as it's now). – CL. May 11 '13 at 12:45