Android 开发 - ListView 绑定到我的数据库

发布于 2024-12-26 00:08:49 字数 507 浏览 0 评论 0原文

我是 Android 开发新手,而且我已经很长时间没有做过 Java 了(很好的开始..)。

我不想做任何花哨的事情 - 然而,我只是想通过 SimpleCursorAdaptor 将 Cursor 绑定到 ListView。

我已经让我的主类扩展了“Activity”,因此大多数示例源代码并不真正适用。他们似乎想扩展 ListActivity。

在第一个应用程序屏幕上,我有一个列表视图,需要显示数据库中的数据。我有一个数据库助手,它会很高兴地返回一个包含数据库值的游标。所以真正的问题是 - 在不扩展 ListActivity 类的情况下 - 我到底该怎么做?

这个问题没有代码,因为它只是世界上最简单的事情,我确信知道自己在做什么的人的一些建议就足够了。另请注意,我尝试在项目中创建一个扩展 ListActivity 的新类并调用它,但我仍然不知道绑定如何、何时或在何处发生。它不允许我在列表视图中使用“setListAdapter()” - 它只允许 setAdapter() 导致崩溃。

任何帮助将不胜感激。

I'm new to Android development and it's been a long time since I've done any Java either (great start..).

I'm not looking to do anything fancy - yet, I'm just looking to bind a Cursor to the ListView - through a SimpleCursorAdaptor.

I've already got my main class extending "Activity" so most of the sample source code doesn't really apply. They seem to want to extend ListActivity instead.

Right on the first app screen, I've got a listview that needs to show the data in my database. I've got a DB helper that will happily return a cursor with the database values in it. So the real question is - without extending the ListActivity class - how on earth do I do it?

There's no code in this question as it's just the simplest thing in the world and I'm sure some advice from someone who knows what they're doing would be sufficient. Note also that I've tried creating a new class in my project that does extend ListActivity and called that but I still don't know how or when or where the binding takes place. It doesn't allow me to use "setListAdapter()" to my listview - it only allows setAdapter() which crashes the thing.

Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

梦行七里 2025-01-02 00:08:49

您可以使用 Acitivity 扩展该类,在布局 xml 中使用 id @+id/blahblah 定义 ListView,然后在 Activity 类中找到 ListView id。使用设置适配器

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.row, cursor, columns, to);
listView.setAdapter(adapter);

You can extend the class with Acitivity that define ListView in the layout xml with id @+id/blahblah, then find the ListView id in the Activity class. Set the Adapter using

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.row, cursor, columns, to);
listView.setAdapter(adapter);
趁年轻赶紧闹 2025-01-02 00:08:49

如果你扩展 Activity 它将是

ListView listView = (ListView)findViewById(...);
listView.setAdapter(adapter);

如果你扩展 ListActivity 它将是

setListAdapter(adapter);

If you extend Activity it will be

ListView listView = (ListView)findViewById(...);
listView.setAdapter(adapter);

If you extended ListActivity the it will be

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