从SD卡(未在列表中的项目设定量)填充与图像的列表视图 [英] Populating a listview with images from the SD card (not a set amount of items in list)

查看:95
本文介绍了从SD卡(未在列表中的项目设定量)填充与图像的列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我试图让喜欢搭载Android提供的一个联系人列表。当填充与项目的列表视图,用SimpleCursorAdapter就可以轻松搞定所有的名字出现在每一个项目的R.id.textview:

Basically I'm trying to make a contact list like the one provided by Android. When populating the listview with items, using a SimpleCursorAdapter you can easily get all the names to appear in the R.id.textview of each item:

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);
    }

类似的东西。我已经搜查,发现样品code为获取图像,从网上,或显示图像中的项目的一组数字(比如你​​知道你有5个项目让您得到5匹配的图像)。但我真的不知道,我想首先从我的SD卡获取图像,并在适当的项目显示它们。这些图像是根据联系人的ID命名的,所以我必须调用正确的图像的方式。

Something like that. I've searched and found sample code for both getting images from online, or displaying a set number of images in the items (for instance you know you have 5 items so you get the 5 matching images). But I really don't know where I'd begin on getting images from my SD card, and displaying them in the proper item. The images are named according to the id of the contact, so I do have the means to call the proper image.

在正确的方向上推会更AP preciated,谢谢!

A push in the right direction would be much appreciated, thank you!

编辑:@Jeff Gilfelt给予了极大的答案,但我继续说话太快,说我可以找出我自己休息...哈哈时候。我在XML的Andr​​oid这样做的接触声明的默认图像。当我实施新的适配器,它像COM presses的项目到什么,我想,是因为它找到了一个空位图@该位置。所以,我做了以下内容:

@Jeff Gilfelt gave a great answer, but I went ahead and spoke too soon when saying I could figure out the rest on my own... haha. I have a default image declared in the xml for the contacts like Android does. When I implement the new Adapter, it like compresses the items into nothing, I figure because it finds an empty bitmap @ that location. So I did the following:

@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);
    }
}

但是,这并没有帮助。任何想法?

But this doesn't help either. Any ideas?

EDIT2:想通了上述问题。只要是这样的:

Figured out the above problem. Simply go like this:

    else {
        Resources res = mContext.getResources();
        Drawable drawable = res.getDrawable(R.drawable.default);
        v.setImageDrawable(drawable);
    }

希望这可以帮助别人!请记住这个解决方案,您将需要添加一个上下文成员变种,并在构造函数中的行 mContext =背景

推荐答案

创建SimpleCursorAdaptor的子类,覆盖setViewImage方法,使得它构造的路径,从你给它的ID在SD卡上相应的文件,然后使用BitmapFactory.de codeFILE创建您使用您绑定到图像视图一个位图:

Create a subclass of SimpleCursorAdaptor and override the setViewImage method such that it constructs the path to the appropriate file on the SD card from the id you feed it, then use BitmapFactory.decodeFile to create a Bitmap you use for the image view you are binding to:

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);

    }

}

然后从游标和您的ImageView的视图id的添加联系人ID /从你传递给适配卡的阵列。例如:

Then add your contact id from the cursor and the view id of your ImageView to the to/from arrays that you pass to the adapter. Example:

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);
}

这篇关于从SD卡(未在列表中的项目设定量)填充与图像的列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆