Search code examples
androidandroid-layoutlistviewandroid-listviewandroid-arrayadapter

ListView Holder with checkbox


I'm developing an app with a custom layout and an arrayAdapter. My custom layout is composed by 1 ImageView, 2 textViews and 1 checkbox.

Booth the image files and text are stored on string.xml in values folder.

enter image description here

So my problem is I cant get the correct position(index) when I click on checkbox. I just want that a Toast message appears with the position on the checkbox.

e.g. if I click on meme3 checkbox appears 2

Lets look at some code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Resources res = getResources();
    titles = res.getStringArray(R.array.titles);
    descriptions = res.getStringArray(R.array.descriptions);
    list = (ListView) findViewById(R.id.listView1);

    MyAdapter adapter = new MyAdapter(this, titles, images, descriptions);
    list.setAdapter(adapter);
}

This is my ArrayAdapter

class MyAdapter extends ArrayAdapter<String> implements OnCheckedChangeListener {
    Context context;
    int[] images;
    String[] titlesArray, descrptionArray;

MyAdapter(Context context, String[] titles, int[] images, String[] description) {
    super(context, R.layout.single_row, R.id.textView1, titles);
    this.context = context;
    this.images = images;
    this.titlesArray = titles;
    this.descrptionArray = description;
}

class MyViewHolder {
    ImageView myImage;
    TextView myTitle;
    TextView myDescription;
    CheckBox box;

    MyViewHolder(View v) {
        myImage = (ImageView) v.findViewById(R.id.imageView1);
        myTitle = (TextView) v.findViewById(R.id.textView1);
        myDescription = (TextView) v.findViewById(R.id.textView2);
        box = (CheckBox) v.findViewById(R.id.checkBox1);
    }
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    MyViewHolder holder = null;
    if (row == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.single_row, parent, false);
        holder = new MyViewHolder(row);
        row.setTag(holder);
    } else {
        holder = (MyViewHolder) row.getTag();
    }
    holder.myImage.setImageResource(images[position]);
    holder.myTitle.setText(titlesArray[position]);
    holder.myDescription.setText(descrptionArray[position]);
    holder.box.setOnCheckedChangeListener(this);
    return row;
}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if(isChecked){
        int position = (Integer)buttonView.getTag();
        Toast.makeText(getContext(), ""+position, Toast.LENGTH_SHORT).show();
    }
}
}

My list have 20 items. Now if i check one single checkbox the app automatically select other 2 other checkbox! Please Help!


Solution

  • You can set the position as the tag on your checkbox.

    holder.box.setTag(position);
    

    And retrieve in your listener with:

    int position = (Integer)buttonView.getTag();