Search code examples
androidlistviewandroid-radiogroupandroid-radiobutton

Add different Images to my Adapter and show them in ListView


I am new to android..and my question is:

I am making one android Application in which I have one RadioGroup with two Radiobutton btnA and btnB along with some other Parameters. if btnA is Checked than value in database is 1 and if btnB is selected then Value in Database is 0. I am retrieving Data from database while showing My Listview.

Now My Question is I want to display Listview with listItem like :

imgA if Value From Database is 1 .

imgB if Value from Database is 0.

How to do it???

I tried this

private Integer[] Images = {R.drawable.imgA,R.drawable.imgB};
Cursor cur = dop.getData();
if(cur!= null && cur.getCount()>0)
{
  if(cur.moveToFirst()){
     do {Integer btnType= cur.getInt(cur.getColumnIndex(databaseName.TableName.ColumnName));
                    if(btnType== 1){ImageId = Images[0];}
                    else if(btnType== 0){ImageId= Images[1];}}
          //other Params
  }while (cur.moveToNext());

}
Adapter myAdp = new Adapter(Activity.this,ImageId,para);
myList.setAdapter(myAdp);

My Adapter is Like

    public class Adapter extends BaseAdapter {

    public Context context;
    public ArrayList<String>Param1;
    public int ImageId;

    public Adapter(Context context,int ImageId,ArrayList<String>Param1)
    {
        this.context = context;
        this.ImageId = ImageId;
        this.Param1= Param1;

    }
    public int getCount(){return param1.size();}
    public Object getItem(int Position){return null;}
    public long getItemId(int Position){return 0;}
    public class viewHolder{
        TextView tvParam1;
        ImageView imgType;

    }
    @Override
    public View getView(int Position,View Child,ViewGroup Parent)
    {
        viewHolder vHolder;
        LayoutInflater inflator;
        if(Child == null)
        {
            inflator = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            Child = inflator.inflate(R.layout.list_row,null);
            vHolder = new viewHolder();
            vHolder.tvparam1 = (TextView)Child.findViewById(R.id.txtParam1);
            vHolder.imgType = (ImageView)Child.findViewById(R.id.imgType);
            Child.setTag(vHolder);
        }
        else {vHolder = (viewHolder)Child.getTag();}
        vHolder.tvParam1.setText(Param1.get(Position));                 
        vHolder.imgType.setImageResource(ImageId);
        return Child;
    }

}

my Problem is I am getting same image for all list items. but I want ImgA for btnA and imgB for btnB. How to resolve this???


Solution

  • I got solution for this issue

    what i done is: I took Integer Arraylist for storing my Images

    In my Main Activity:

    public int[] Images = {R.drawable.imgA,R.drawable.imgB};
    public ArrayList<Integer>ImageId = new ArrayList<Integer>();
    int i = 0;
    
    if(cur.moveToFirst()){
          if(btnType == 1)
                    {
                        ImageId.add(Images[0]);
                    }
                    else if(btnType == 0)
                    {
                        ImageId.add(Images[1]);
                    }   
           } while(cur.moveToNext());
    

    also in myAdapter: I jst changed Integer Array to Integer Arraylist for Image

    this solve my Problem