SimpleCursorAdapter 中的图像
我正在尝试将 SimpleCursorAdapter
与 ViewBinder
结合使用,从数据库获取图像并将其放入我的 ListView
项视图中。这是我的代码:
private void setUpViews() {
mNewsView = (ListView) findViewById(R.id.news_list);
Cursor cursor = getNews();
SimpleCursorAdapter curAdapter = new SimpleCursorAdapter(
getApplicationContext(), R.layout.cursor_item, cursor,
new String[] { "title", "content", "image" },
new int[] { R.id.cursor_title, R.id.cursor_content,
R.id.news_image });
ViewBinder viewBinder = new ViewBinder() {
public boolean setViewValue(View view, Cursor cursor,
int columnIndex) {
ImageView image = (ImageView) view;
byte[] byteArr = cursor.getBlob(columnIndex);
image.setImageBitmap(BitmapFactory.decodeByteArray(byteArr, 0, byteArr.length));
return true;
}
};
ImageView image = (ImageView) findViewById(R.id.news_image);
viewBinder.setViewValue(image, cursor, cursor.getColumnIndex("image"));
curAdapter.setViewBinder(viewBinder);
mNewsView.setAdapter(curAdapter);
}
我得到:
android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 60
在执行 byte[] byteArr = curve.getBlob(columnIndex);
时。有谁知道我做错了什么?
I'm trying to use a SimpleCursorAdapter
with a ViewBinder
to get an image from the database and put it into my ListView
item view. Here is my code:
private void setUpViews() {
mNewsView = (ListView) findViewById(R.id.news_list);
Cursor cursor = getNews();
SimpleCursorAdapter curAdapter = new SimpleCursorAdapter(
getApplicationContext(), R.layout.cursor_item, cursor,
new String[] { "title", "content", "image" },
new int[] { R.id.cursor_title, R.id.cursor_content,
R.id.news_image });
ViewBinder viewBinder = new ViewBinder() {
public boolean setViewValue(View view, Cursor cursor,
int columnIndex) {
ImageView image = (ImageView) view;
byte[] byteArr = cursor.getBlob(columnIndex);
image.setImageBitmap(BitmapFactory.decodeByteArray(byteArr, 0, byteArr.length));
return true;
}
};
ImageView image = (ImageView) findViewById(R.id.news_image);
viewBinder.setViewValue(image, cursor, cursor.getColumnIndex("image"));
curAdapter.setViewBinder(viewBinder);
mNewsView.setAdapter(curAdapter);
}
I am getting:
android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 60
while executing byte[] byteArr = cursor.getBlob(columnIndex);
. Does anyone have an idea what am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我扩展了 SimpleCursorAdapter,虽然我没有使用 ViewBinder,但这里是我的代码,用于在列表视图的 sqlite 数据库中使用存储为 blob 的图像。这是改编自我在此处阅读的一篇文章。
我的行布局文件是:
row_layout_two_line.xml
调用代码
ImageCursorAdapter.java
}
这是最终的样子
I extended SimpleCursorAdapter, and while I did not use a ViewBinder here is my code for using an image stored as a blob in an sqlite database in a listview. This was adapted from an article I read here.
My layout file for a row is:
row_layout_two_line.xml
The calling code
ImageCursorAdapter.java
}
Here is what it looks like in the end
我认为
cursor.moveToFirst()
尚未被调用,因此光标抛出android.database.CursorIndexOutOfBoundsException。
在使用
cursor
之前,您应该始终通过调用cursor.moveToFirst()来检查光标是否为空。这也会将光标定位在第一个位置。I think the
cursor.moveToFirst()
has not been called so the cursor is throwingandroid.database.CursorIndexOutOfBoundsException.
Before using a
cursor
you should always check is the cursor is empty or not by callingcursor.moveToFirst()
. This will also position the cursor at the first position.使用 ListView 和 SimpleCusrorAdapter 以及联系人照片和过滤/搜索的联系人列表
我一直在寻找一种更简单的解决方案,我的最终解决方案非常接近 Daniel 在这里提到的解决方案,所以我想我应该在这里分享我的解决方案。我正在使用 Fragment 将设备联系人显示为带有照片的姓名列表。结果与丹尼尔的结果非常相似,但仅显示姓名。一旦理解了代码,就可以很容易地显示更多信息。
就我而言,我使用 PHOTO_URI 从 ContactsContract 获取姓名和图片,因此我不必像 Daniel 那样扩展
SimpleCursorAdapter
。我的示例还包括当用户在
SearchView
中键入时过滤联系人列表以查找联系人我有一个名为
FragmentContacts
的片段和两个布局文件,第一个是主布局frag_contacts.xml
和每个联系人行的第二个list_row_contact
。frag_contacts.xml
list_row_contact.xml
FragmentContacts.java
Contact List using ListView and SimpleCusrorAdapter with Contact Photos and Filter/Search
I had been looking for a simpler solution and my final solution is quite closer to the one Daniel mentioned here so I thought I should share mine here. I am using Fragment to show Device Contacts as a list of names with their pictures. Result is pretty similar to that of Daniel's but is showing only names. More information can be shown very easily once you understand the code.
In my case I was fetching names and pictures from ContactsContract using PHOTO_URI so I didn't have to extend
SimpleCursorAdapter
as Daniel had to.My example also includes filtering the list of contacts as user types in
SearchView
to find a contactI have a Fragment called
FragmentContacts
and two Layout files, first the main Layoutfrag_contacts.xml
and second for each contact rowlist_row_contact
.frag_contacts.xml
list_row_contact.xml
FragmentContacts.java