3

Problem:

When i click on the 2nd checkbox item in the listview then automatically 10th item is checked. I can not understand what's happen?

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.TextView;

public class ItemAdapter extends ArrayAdapter<MyItem> {
private int resId;
Context context;
private ArrayList<MyItem> itemList;

public ItemAdapter(Context context, int textViewResourceId,
        List<MyItem> objects) {
    super(context, textViewResourceId, objects);
    this.context = context;
    this.resId = textViewResourceId;
    this.itemList = new ArrayList<MyItem>();
    this.itemList.addAll(objects);
}

private class ViewHolder {
    public boolean needInflate;
    public TextView txtItemName;
    public CheckBox chkItem;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    MyItem cell = (MyItem) getItem(position);
    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.listitem, null);
        holder = new ViewHolder();
        holder.txtItemName = (TextView) convertView
                .findViewById(R.id.tvItemName);
        holder.chkItem = (CheckBox) convertView.findViewById(R.id.chkItem);
        holder.chkItem
                .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView,
                            boolean isChecked) {
                        //Log.i("Pos", "" + position);
                        //cell.setSelected(buttonView.isChecked());
                    }
                });
        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.txtItemName.setText(cell.getName());
    return convertView;
  }
}

"MyItem" is my pojo Class.

OnCreate Code:

 lvItemName = (ListView) findViewById(R.id.lvItemName);
    List<MyItem> myItemsList = new ArrayList<MyItem>();

    for (int i = 0; i < items.length; i++) {
        MyItem item = new MyItem(items[i], false);
        myItemsList.add(item);
    }

    ItemAdapter adapter = new ItemAdapter(this, R.layout.listitem,
            myItemsList);
    lvItemName.setAdapter(adapter);
    lvItemName.setOnItemClickListener(this);

"items" is my String Array.

Thanks in Advance.

Dhruv
  • 1,862
  • 3
  • 20
  • 38
  • http://stackoverflow.com/questions/18162931/android-get-selected-item-using-checkbox-in-listview-when-i-click-a-button/18164177#18164177. check this if it helps – Raghunandan Sep 09 '13 at 09:53
  • Perhaps this helps: http://stackoverflow.com/questions/1362602/selecting-multiple-items-in-listview – Alexfr8 Sep 09 '13 at 09:55
  • I have problem with checkbox means when i checked 2nd checkbox item then automatically 10th item is checked. Can you please tell me what's wrong in my code? – Dhruv Sep 09 '13 at 10:01
  • 1
    @lawrance:you are not setting tag on checkeditem postion. I have suggested a link in my answer which fixes your issue. – user755 Sep 09 '13 at 10:03
  • yes you are right @user2439755. I solved my issue by adding "holder.chkItem.setTag(position);" Thanks. – Dhruv Sep 09 '13 at 10:16
  • 1
    @Lawrence check out this answer, it may help: http://stackoverflow.com/a/20171191/919216 – Chanakya Vadla Nov 24 '13 at 04:56

3 Answers3

1

I hope you can avoid this problem by

adding a boolean array like

boolean[] checkboxState=new boolean['your array size here']; //global decleration
holder.checkBox.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

                if (((CheckBox) v).isChecked()) {
                    checkboxState[position] = true;


                } else
                    checkboxState[position] = false;

This works for me and i hope this will help you..

Ansar
  • 364
  • 3
  • 16
0

set tag on checkeditem postion.

Check out here complete working example.

user755
  • 2,521
  • 3
  • 18
  • 29
0

In your listview getView method, make holder.checkbox to seTag(position).

Then use that position using getTag() method, you will get selected position there and your problem will solved. Read about setTag(value) and getTag() method.

Pratik Dasa
  • 7,439
  • 4
  • 30
  • 44