How can I make ListView with SimpleCursorAdapter skip "" text?
I have a ListView which contains a text field that is populated usin开发者_运维技巧g a SimpleCursorAdapter. In my database, the field is allowed to be null (a desired effect), however, if the value is null, rather then having the TextView be blank (as populated by the SimpleCursorAdapter), I want it to contain some default value, say "-------". How can I acheive this effect?
It can either be done by the way I run my SQLite query, or by some method telling the textView to ignore "", and show its android:text value or something like that. Whichever method is recommended, I can't figure out how to do it. Any help is appreciated.
I would use ifnull
in your SQL SELECT statement as per this answer. Something like:
SELECT IFNULL(title, "------") FROM books;
or if you need the column name:
SELECT IFNULL(title, "------") AS title FROM books;
I don't think there is any special property that you can set on the TextView to show a specific value if setText("")
is called. There is android:hint but I'm sure that goes out the window when setText()
is called, even if it is blank, and the views are recycled so that probably wouldn't work anyway.
So you should either return the "-----" in the sql query when the value is null or use a SimpleCursorAdapter.ViewBinder to set the "-----" when the value is blank. Building it into the sql query should be easiest.
精彩评论