如何将ImageView与ListView相结合

时间:2012-11-21 21:43:03

标签: android

我将我的数据库连接到ListView,如下所示:

public void update_list(String NN) {
       c = db.rawQuery(NN, null);
       startManagingCursor(c);
       String[] from = new String[]{"_id","Fname","Lname","Phone","Car","CarNum" };
       int[] to = new int[]{  R.id._id,R.id.Fname,R.id.Lname,R.id.Phone,R.id.Car,R.id.CarNum };
       SimpleCursorAdapter notes = new SimpleCursorAdapter (this, R.layout.my_list, c, from, to);
       setListAdapter(notes);
       setListAdapter(new SimpleCursorAdapter(this, R.layout.my_list, c, from,to) {

       @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = super.getView(position, convertView, parent);
        }
    });
    }

现在我需要将ImageView连接到图片

我有字段PicNum,其中包含指向图片的链接

我知道像这样将图片加载到ImageView:

 MyPic = (ImageView) findViewById(R.id.MyPic);
        try
        {
        File imgFile = new File("/sdcard/MyPic/Unzip/" +MyParam.zPicNum+ ".jpeg");
        if(imgFile.exists()){
            Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
             MyPic.setImageBitmap(myBitmap);
            }
        else
        {
             MyPic.setImageBitmap(null);
        }
      }
      catch (Exception e) {
           MyPic.setImageBitmap(null);
    } 

如何将此ImageView组合到我的ListView?

5 个答案:

答案 0 :(得分:3)

尝试将图像插入列表视图:

MySimpleAdapter notes;

    public class MySimpleAdapter extends SimpleCursorAdapter{

            public MySimpleAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
                super(context, layout, c, from, to);
            }

            @Override
            public void setViewImage(ImageView v, String zPicNum) {
                try{
                    String pathName = Environment.getExternalStorageDirectory().getPath() + "MyPic/Unzip/" +zPicNum+ ".jpeg";
                    File path = new File(pathName);
                    if(path.exists()){
                        BitmapFactory.Options options = new BitmapFactory.Options();
                        Bitmap bm = BitmapFactory.decodeFile(pathName, options);
                        v.setImageBitmap(bm);
                    }
                    else{
                        v.setImageResource(R.drawable.defaultpic);
                    }
                }
                catch (Exception e)
                {
                   Toast.makeText(getActivity(), "error in finding images", Toast.LENGTH_LONG).show();
                }
            }
        }

答案 1 :(得分:2)

PicNumR.id.MyPic中添加R.layout.my_list字段和from(它应该是to的一部分)。 String[] from = new String[]{"_id","Fname","Lname","Phone","Car","CarNum","PicNum" }; int[] to = new int[]{R.id._id,R.id.Fname,R.id.Lname,R.id.Phone,R.id.Car,R.id.CarNum, R.id.MyPic}; 数组:

getView()

然后在返回之前使用R.id.MyPic方法填充@Override public View getView(int position, View convertView, ViewGroup parent) { if(convertView.getId()==R.id.MyPic){ /*Your code to load the Picture to convertView*/ return convertView; }else return super.getView(position, convertView, parent); } }); 的图片:

ViewBinder

另一种选择是使用View并检查绑定的R.id.MyPic是否为{{1}},然后再将图片加载到其中。

答案 2 :(得分:2)

SimpleCursorAdapter因某种原因被弃用了。原因是,它将冻结整个应用程序,直到它下载当前视口中可见的所有图像。

答案 3 :(得分:1)

PicNum中有什么?如果它是图像的URL,则必须先下载该文件并在显示之前对其进行解码,因此您不能单独使用SimpleCursorAdapter。如果需要,您可以将SimpleCursorAdapter子类化,并使用SimpleCursorAdapter.ViewBinder接口对其进行扩展。这将允许您对大多数列具有默认的bindView行为,但随后下载并解码PicNum的图像。

答案 4 :(得分:1)

SimpleCursor Adapter很容易实现,下面是一个,它将帮助您学习简单的游标适配器Click Here