如何使用Android中的SimpleAdapter在Listview中动态设置Listview中的图像?

时间:2014-04-24 07:47:51

标签: android image android-listview simpleadapter

我使用SimpleAdapter生成了Listview。我想为ListView的每一行设置不同的图片。我已完成以下编码,但我的图像仅在第一行设置。如何为剩余行设置图片。请一步一步解释我。

我的代码如下:

通讯录扩展片段

 ArrayList<HashMap<String, String>>val=new ArrayList<HashMap<String,String>>();
 val=db.getTaskSent(name);
  ListAdapter k=new SimpleAdapter(getActivity(),val,R.layout.senttaskdata,new String[]{"rname","heading","desc","id","path","receiver","sender"},new int[]{R.id.textView1,R.id.textView2,R.id.textView3,R.id.hide1,R.id.hide2,R.id.hide3,R.id.hide4})
  {

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final View v = super.getView(position, convertView, parent);
        TextView picpath=(TextView)v.findViewById(R.id.hide2);
        ImageView picture=(ImageView)v.findViewById(R.id.imageView1);
        String p=picpath.getText().toString();
         File f = new File(p);
        //ImageView mImgView1 = (ImageView)findViewById(R.id.imageView2);
        Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath());
        picture.setImageBitmap(bmp);

        return super.getView(position, convertView, parent);
    }

  };
  sent.setAdapter(k);

1 个答案:

答案 0 :(得分:1)

ArrayList<HashMap<String, String>>val=new ArrayList<HashMap<String,String>>();
 val=db.getTaskSent(name);
  ListAdapter k=new SimpleAdapter(getActivity(),val,R.layout.senttaskdata,new String[]{"rname","heading","desc","id","path","receiver","sender"},new int[]{R.id.textView1,R.id.textView2,R.id.textView3,R.id.hide1,R.id.hide2,R.id.hide3,R.id.hide4})
  {

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final View v = super.getView(position, convertView, parent);
        TextView picpath=(TextView)v.findViewById(R.id.hide2);
        ImageView picture=(ImageView)v.findViewById(R.id.imageView1);
        String p=picpath.getText().toString();
         File f = new File(p);
        //ImageView mImgView1 = (ImageView)findViewById(R.id.imageView2);
        Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath());
        picture.setImageBitmap(bmp);

        return v;
    }

  };
  sent.setAdapter(k);