简体   繁体   English

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

[英]How to set an Image in the Listview dynamically in the Listview using SimpleAdapter in Android?

I have generated a Listview using a SimpleAdapter . 我已经使用SimpleAdapter生成了Listview。 I want to set different pictures for each row of the ListView. 我想为ListView的每一行设置不同的图片。 I have done the following coding, but my image sets only on the first row. 我已经完成了以下编码,但是我的图像仅在第一行设置。 how to set pictures for the remaining row. 如何为剩余的行设置图片。 Please explain me step by step. 请一步一步给我解释。

My codes are as below: 我的代码如下:

Contacts extends Fragment 接触延伸片段

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM