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

StackOverflow https://stackoverflow.com/questions/23262905

Вопрос

I have generated a Listview using a SimpleAdapter. I want to set different pictures for each row of the 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 с атрибуция
Не связан с StackOverflow
scroll top