基本上我正在尝试创建一个类似Android提供的联系人列表。使用项目填充listview时,使用SimpleCursorAdapter可以轻松地将所有名称显示在每个项目的R.id.textview中:
private void fillData() {
mCursor = mDbAdapter.fetchAllContacts();
startManagingCursor(mCursor);
String[] from = new String[] {DBAdapter.KEY_NAME};
int[] to = new int[] {R.id.contact_name};
SimpleCursorAdapter contacts = new SimpleCursorAdapter(this, R.layout.contact_view, mCursor, from, to);
this.setListAdapter(contacts);
}
这样的事情。我搜索并找到了从在线获取图像或在项目中显示一定数量图像的示例代码(例如,您知道您有5个项目,因此您可以获得5个匹配的图像)。但我真的不知道从哪里开始从我的SD卡获取图像,并将它们显示在正确的项目中。图像根据联系人的ID命名,因此我可以调用正确的图像。
非常感谢正确的方向,谢谢!
编辑:@Jeff Gilfelt给出了一个很好的答案,但是当我说我可以自己解决剩下的问题时,我继续说得太早......哈哈。我在xml中为Android这样的联系人声明了一个默认图像。当我实现新的适配器时,它就像把项目压缩成什么,我想因为它找到了一个空位图@该位置。所以我做了以下事情:@Override
public void setViewImage(ImageView v, String id) {
File root = Environment.getExternalStorageDirectory();
File path = new File(root, "path/images/thumbs/"+id+".jpg");
if(path.exists()) {
Bitmap bitmap = BitmapStatic.getThumb(id);
v.setImageBitmap(bitmap);
}
else {
super.setViewImage(v, id);
}
}
但这也无济于事。有什么想法吗?
EDIT2:找出上述问题。简单地说就是这样:
else {
Resources res = mContext.getResources();
Drawable drawable = res.getDrawable(R.drawable.default);
v.setImageDrawable(drawable);
}
希望这有助于他人!请记住,对于此解决方案,您必须在构造函数中添加Context成员var和行mContext = context
。
答案 0 :(得分:2)
创建SimpleCursorAdaptor的子类并覆盖setViewImage方法,使其从您提供的ID构建SD卡上相应文件的路径,然后使用BitmapFactory.decodeFile创建用于图像视图的Bitmap绑定到:
public class MySimpleCursorAdapter extends SimpleCursorAdapter {
public MySimpleCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
}
@Override
public void setViewImage(ImageView v, String id) {
String path = "/path/to/sd/card/" + id + ".png";
Bitmap b = BitmapFactory.decodeFile(path);
v.setImageBitmap(b);
}
}
然后将光标中的联系人ID和ImageView的视图ID添加到传递给适配器的数组中。例如:
private void fillData() {
mCursor = mDbAdapter.fetchAllContacts();
startManagingCursor(mCursor);
String[] from = new String[] {DBAdapter.KEY_NAME, DBAdapter.ID};
int[] to = new int[] {R.id.contact_name, R.id.contact_image};
MySimpleCursorAdapter contacts =
new MySimpleCursorAdapter(this, R.layout.contact_view, mCursor, from, to);
this.setListAdapter(contacts);
}
答案 1 :(得分:0)
阅读本教程http://developer.android.com/guide/topics/data/data-storage.html
和这篇文章http://www.brighthub.com/mobile/google-android/articles/64048.aspx
有一个二维arraylist,第一个元素包含联系人姓名,第二个元素包含图像。在将图像读入位图后,将位图保存在联系人arraylist中。有一个包含textView和imageView作为其行元素的自定义arrayAdapter,在数组适配器的getView()中设置联系人名称和图像。请参阅此帖子以获取自定义列表视图http://www.androidpeople.com/android-custom-listview-tutorial-example/
这可以帮助你开始。
答案 2 :(得分:0)
您应该阅读有关Android开发指南的external storage章节。通过调用getExternalFilesDir(),您将获得SD卡的文件路径。
作为一种策略,如果您导航到包含图像的文件夹并只传入联系人的ID以找到该文件,则可以将其加载到数据结构中。
然后,您可以创建传递包含图像的数据结构的自定义适配器。这将导致ListView仅包含联系人的图像。
这只是为了让你先行一步!
答案 3 :(得分:0)
您不需要创建SimpleCursorAdapter
的子类,只需创建一个ViewBinder
来处理每行的图像设置。
ViewBinder.setViewValue
调用 View
(在布局xml中指定),因此请确保对任何不是ImageView实例的视图返回false。
ViewBinder viewBinder = new ViewBinder(){
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (view instanceof ImageView){
ImageView imageView = (ImageView) view;
String imageFilename = cursor.getString(columnIndex);
imageView.setImageURI(imageFilename);
return true;
}
return false;
}
};
cursorAdapter.setViewBinder(viewBinder);