0

Note: This isn't the common problem of scrolling causing the checkboxes to lose their way.

I have a listview adapter with a relativelayout (containing a textview and a checkbox) as the listview row, and I want to make it so when a user clicks on the listview row... then the checkbox will toggle.

I've got it working right now almost, but what's happening is the checkbox doesn't show me that it has toggled until I scroll the row off the screen and then come back. So it seems like it's not refreshing properly. My guess is I need to explicitly do something else with the checkbox listener. Here's my code:

         ViewHolder holder = null;
        MainContactsListOnOff entry = listMaincontactslistOnOff.get(position);
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = inflater.inflate(R.layout.contact_rowonoff, null);

        holder = new ViewHolder();
        holder.code = (TextView) convertView.findViewById(R.id.textView1onoff);
        holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1onoff);
        holder.img = (ImageView) convertView.findViewById(R.id.imageView1onoff);
        convertView.setTag(holder);       

        } else {
            holder = (ViewHolder) convertView.getTag();
        }
     holder.code.setText(entry.getName().toString());
  if(entry.getOnoff()==1){
      holder.img.setImageResource(R.drawable.green);
  } else {
      holder.img.setImageResource(R.drawable.red);
  }
     convertView.setId(entry.getDevID());
     holder.name.setTag(entry);
     holder.name.setChecked(entry.getChecked());

     convertView.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                RelativeLayout rl = (RelativeLayout) arg0;
                CheckBox chkBx = (CheckBox) rl.getChildAt(1);
                MainContactsListOnOff mclOO = (MainContactsListOnOff)   chkBx.getTag();
                if(chkBx.isChecked()){
                    mclOO.setChecked(false);
                } else {
                    mclOO.setChecked(true);
                }

            }

        });
      return convertView;

2 Answers2

2

You just forgot a line there!

if(chkBx.isChecked()){
                mclOO.setChecked(false);
            } else {
                mclOO.setChecked(true);
            }
chkBox.setChecked(!chkBx.isChecked()); //<-----Add this line to update the box state! :)

You only modified the tag but not the checkbox. Which is why when it reloads it will be right.

(Bounce: You can simplify the above to two lines:

chkBox.setChecked(!chkBx.isChecked()); 
mclOO.setChecked(chkBox.isChecked());

)

Edison
  • 5,961
  • 4
  • 23
  • 39
0

I implemented similar functionality - where I had a checkbox in a row of listView. So, there are three places that deal with this thing:

The xml file: Mark the checkbox as "focusable" in the row.xml (in my case this is the file name).

<CheckBox
    android:id="@+id/on_off"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:focusable="false" />

The Activity that will receive check box clicks: Make the activity implement OnCheckedChangeListener. Then, you will need to implement its onCheckedChanged() method.

@Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        // here you receive clicks
            // you can perform your desired action

    }

Now, the question is how to associate the checkbox with the listener (which is the listview acitivity). Well, wherever you bind your xml checkbox to the CheckBox in java, set its

In my case, I have a custom View class representing a row of listview. I pass the activity to the constructor of that class, which is then used in its constructor as CheckedChangedListener to the activity that implements OnCheckedChangeListener.

public class ListRow extends RelativeLayout  {

    // other elements
    ...
    private CheckBox OnOff;

    public ListRow(Context context, ...) {
        super(context);


         LayoutInflater inflater = 
                 (LayoutInflater)     context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         inflater.inflate(R.layout.list_row, this, true);
         ...

         OnOff = (CheckBox) findViewById(R.id.on_off);
        OnOff.setOnCheckedChangeListener((OnCheckedChangeListener) context);

    }
}
Nazar Merza
  • 3,365
  • 1
  • 19
  • 19