使用 SimpleCursorAdapter 时从联系人 ID 检索并显示联系人姓名
我使用 sqlite 数据库存储手机联系人信息。 我存储的字段是:_id, contact_id, body
,其中_id
是行的id,contact_id
是电话联系人的id,body
只是一个简单的文本,它是我存储的实际信息。
我正在使用 SimpleCursorAdapter 将数据映射到视图,如下所示:
Cursor cursor = database.fetchInformation(); // fetch info from DB
String[] from = new String[] { CONTACT_ID, BODY };
int[] to = new int[] { R.id.contact, R.id.body };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
getListView().setAdapter(adapter);
我真正想要的是获取与此 CONTACT_ID 关联的联系人姓名,并将其显示在视图中。
我似乎无法找到一个简单的解决方案。我尝试实现自己的 SimpleCursorAdapter 并重写 setViewText(TextView, String) 但它不起作用。此外,对于如此简单的任务来说,它似乎过于复杂。
有什么帮助吗?谢谢。
I am storing information on the phone's contacts using a sqlite database.
The fields I am storing are: _id, contact_id, body
where _id
is the row's id, contact_id
is the phone contact's id and body
is just a simple text which is the actual information I am storing.
I'm using a SimpleCursorAdapter to map the data to the view, like so:
Cursor cursor = database.fetchInformation(); // fetch info from DB
String[] from = new String[] { CONTACT_ID, BODY };
int[] to = new int[] { R.id.contact, R.id.body };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
getListView().setAdapter(adapter);
What I would actually want is to get the contact name that is associated with this CONTACT_ID, and have that be shown in the view.
I can't seem to find a simple solution. I've tried implementing my own SimpleCursorAdapter and rewriting setViewText(TextView, String) but it didn't work. Also, it just seems overly complicated for such a simple task.
Any help? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里实际上有两个选择:
1)您可以创建一个 MatrixCursor,其中包含您希望绑定到 ListView 的所有信息。虽然做到这一点并不是很困难,但它可能不是最有效地利用内存,因为您需要获取光标中每个联系人的姓名。
2)您可以从 CursorAdapter 子类化。实现适当的 newView 和 bindView 方法非常简单。在bindView 内部,您只需查询手机的联系人内容提供程序即可获取当前绑定的联系人的姓名。
You really have two options here:
1) You could create a MatrixCursor that contains all the information you wish to bind to your ListView. While its not very difficult to do this, its probably not the most efficient use of memory because you would need to get the name of every contact in your cursor.
2) You could subclass from CursorAdapter. Its pretty simple to implement the appropriate newView and bindView methods. Inside of bindView you would simply query the phone's contact content provider to get the name of the contact currently being bound.
试试这个
try this