如何使用 SimpleCursorAdapter 使 ListView 跳过“”文本?
我有一个 ListView,其中包含一个使用 SimpleCursorAdapter 填充的文本字段。在我的数据库中,该字段允许为空(期望的效果),但是,如果该值为空,而不是让 TextView 为空(由 SimpleCursorAdapter 填充),我希望它包含一些默认值,例如“--------”。我怎样才能达到这个效果?
它可以通过我运行 SQLite 查询的方式来完成,或者通过某种方法告诉 textView 忽略“”,并显示其 android:text 值或类似的东西。无论推荐哪种方法,我都不知道该怎么做。任何帮助表示赞赏。
I have a ListView which contains a text field that is populated using 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将按照 这个答案。类似于:
或者如果您需要列名称:
I would use
ifnull
in your SQL SELECT statement as per this answer. Something like:or if you need the column name:
我不认为您可以在 TextView 上设置任何特殊属性来在调用
setText("")
时显示特定值。有 android:hint 但我确信当调用setText()
时它会消失,即使它是空白的,并且视图会被回收,因此可能无论如何都无法工作。因此,当值为 null 时,您应该在 sql 查询中返回“-----”,或者使用 SimpleCursorAdapter.ViewBinder 当值为空时设置“-----”。将其构建到 sql 查询中应该是最简单的。
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 whensetText()
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.