【问题标题】:Populating a listview with images from the SD card (not a set amount of items in list)使用 SD 卡中的图像填充列表视图(不是列表中的一组项目)
【发布时间】:2012-07-25 04:23:11
【问题描述】:

基本上,我正在尝试制作一个类似于 Android 提供的联系人列表。当使用项目填充列表视图时,使用 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

【问题讨论】:

    标签: android image listview


    【解决方案1】:

    创建 SimpleCursorAdaptor 的子类并覆盖 setViewImage 方法,以便它根据您提供的 id 构造 SD 卡上相应文件的路径,然后使用 BitmapFactory.decodeFile 创建您用于图像视图的位图绑定到:

    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 添加到传递给适配器的 to/from 数组中。示例:

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

    【讨论】:

    • 太棒了。非常感谢,这非常有效。如果联系人没有个人资料图片,我将需要添加一些行以使图像默认为我在 xml 中将 ImageView 设置为的图像(如果联系人没有个人资料图片,现在整个项目被压缩成一个小条有一个图像),但我绝对可以自己弄清楚。非常感谢!
    • 说得太早了,如果你看到这个看看我的问题,我添加了一个跟进。我尝试修复适配器以仍然显示默认图像失败了,有什么想法吗?
    • 不确定我是否理解问题所在。在您的其他情况下,如果您使用默认占位符资源 ID 调用 v.setImageResource,是否可以解决?
    【解决方案2】:

    阅读本教程http://developer.android.com/guide/topics/data/data-storage.html

    还有这个帖子http://www.brighthub.com/mobile/google-android/articles/64048.aspx

    有一个二维数组列表,第一个元素包含联系人姓名,第二个元素包含图像。将图像读入位图后,将位图保存在联系人数组列表中。有一个包含 textView 和 imageView 作为其行元素的自定义 arrayAdapter,在数组适配器的 getView() 中设置联系人名称和图像。参考这篇文章自定义列表视图http://www.androidpeople.com/android-custom-listview-tutorial-example/

    这应该可以帮助您继续前进。

    【讨论】:

      【解决方案3】:

      您应该阅读 Android 开发指南中的 external storage 章节。通过调用 getExternalFilesDir(),您将获得 SD 卡的文件路径。

      作为一种策略,如果您导航到包含图像的文件夹并仅传递联系人的 ID 以查找文件,则可以将其加载到数据结构中。

      然后您可以创建一个自定义适配器,传入包含您的图像的数据结构。这将导致您的 ListView 仅包含您的联系人的图像。

      这只是为了给你一个良好的开端!

      【讨论】:

        【解决方案4】:

        您不需要创建SimpleCursorAdapter 的子类,只需创建一个ViewBinder 来处理每一行的图像设置。

        ViewBinder.setViewValue 将为您行中的所有 Views 调用(在您的布局 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);
        

        【讨论】:

          猜你喜欢
          • 2014-09-03
          • 2017-08-10
          • 2019-10-01
          • 1970-01-01
          • 2011-01-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多