使用 Android 检索数据库信息并在 ListView 中显示
我正在开发一个 Android 应用程序,它允许用户查看我居住的城市中的餐馆。我将每个餐馆的信息(名称、地址、电话、营业时间、类别、网站)存储在 SQLite 数据库中。
我想做的是创建一个 SortByAlpha 活动,该活动将按名称按字母降序列出餐厅。
我知道我应该使用游标来执行此操作,但我找不到半像样的教程,我找到的所有“教程”都是一堆带有最少解释的代码。我该怎么做/在哪里可以找到好的教程?
I am working on an Android app that will allow the user to see restaurants in the city I live in. I am storing each restaurants information (name, address, telephone, hours, category, website) in an SQLite database.
What I am trying to do is to create a SortByAlpha activity that will list the restaurants by name in alphetically-descending order.
I understand that I should be using a Cursor to do this but I can't find a half decent tutorial, all of the "tutorials" I find are a bunch of code with minimal explanation. How can I do this / Where can I find a good tutorial?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 SimpleCursorAdapter,它在 Cursor 和 Adapter 之间架起桥梁。以下是如何使用它的示例: http://developer.android.com/指南/topics/ui/binding.html
Use SimpleCursorAdapter, which bridges between Cursor and Adapter. Here is an example how to use it: http://developer.android.com/guide/topics/ui/binding.html
我建议创建一个“餐厅”类,其中包含您将列出的所有字段。然后进行 SQL 调用并指定“ORDER BY Name”。然后创建一个可以输入到您的自定义列表适配器的 ArrayList!另外,如果它们以某种方式出现故障,只需为 Restaurant 类实现“Comparable”接口并根据“Name”进行比较,然后您可以调用 Collections.sort(restaurantlist);按字母顺序对它们进行排序。我个人认为 ORDER BY 是更简单的方法!
I would advise creating a "Restaurant" class that contains all the fields you will be listing. Then make your SQL call and specify "ORDER BY Name". Then create an ArrayList that can be fed to your custom list adapter! Also, if they were to somehow get out of order, just implement the "Comparable" interface for the Restaurant class and compare based on "Name" and then you can call Collections.sort(restaurantlist); to sort them in alphabetical order. I personally think ORDER BY is the easier way to go!