1

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???

Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
Pranita
  • 803
  • 7
  • 16
  • 1
    Where are you getting the zero/one value? you should get the values from Database and keep it into your data source (`ArrayList`) before setting the adapter to list view. – Ashish Tiwari May 02 '16 at 11:45

2 Answers2

2

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

Pranita
  • 803
  • 7
  • 16
1

Take an array of ImageId and save the id in that array in specific positions.

int i = 0;

if(cur.moveToFirst()){
    do {Integer btnType= cur.getInt(cur.getColumnIndex(databaseName.TableName.ColumnName));
        if(btnType== 1){ImageId[i] = Images[0];}
        else if(btnType== 0){ImageId[i] = Images[1];}}

        i++;
} while(cur.moveToNext());

Now inside your adapter load the images like this

vHolder.imgType.setImageResource(ImageId[Position]);

You've logical error in your code.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98