如何使用 SimpleCursorAdapter 使 ListView 跳过“”文本?

发布于 2024-10-28 09:10:52 字数 284 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

好倦 2024-11-04 09:10:52

我将按照 这个答案。类似于:

SELECT IFNULL(title, "------") FROM books;

或者如果您需要列名称:

SELECT IFNULL(title, "------") AS title FROM books;

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;
聆听风音 2024-11-04 09:10:52

我不认为您可以在 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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文