Aki Bei Bei Aki Bei Bei - 1 year ago 35
Android Question

Custom listview issue with checkbox

I'm trying to create a listview with checkboxes. so this is the issue whenever I click a checkbox and scroll it down or up, it automatically checks other checkbox in the list that i didn't click. i already search and read other thread here with the same issue but none of them solved my problem.

this is what I have done so far.

public View getView(final int position, View convertView, ViewGroup parent) {


ViewHolder viewHolder = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_songs, null, true);

viewHolder = new ViewHolder();

viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);
viewHolder.tvSongName = (TextView) convertView.findViewById(R.id.tvSongName);
viewHolder.tvSongArtist = (TextView) convertView.findViewById(R.id.tvArtistName);

convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}


viewHolder.tvSongName.setText(_songs.get(position).getSongname());
viewHolder.tvSongArtist.setText(_songs.get(position).getArtistname());
viewHolder.checkBox.setTag(R.integer.btnplusview, convertView);

viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
_songs.get(getPosition).setSelected(buttonView.isChecked());


if (_songs.get(getPosition).isSelected()) {
_songs.get(getPosition).setSelected(false);
Toast.makeText(getApplicationContext(), "position " + position + " " + _songs.get(position).getSongUrl(),
Toast.LENGTH_LONG).show();
Log.e("URL", _songs.get(position).getSongUrl() + " ");
} else {
_songs.get(getPosition).setSelected(true);

}


}


});
convertView.setTag(R.id.tvSongName, viewHolder.tvSongName);
convertView.setTag(R.id.tvArtistName, viewHolder.tvSongArtist);
convertView.setTag(R.id.checkBox, viewHolder.checkBox);

viewHolder.checkBox.setTag(position);

viewHolder.checkBox.setChecked(_songs.get(position).isSelected());

return convertView;
}


what should I do to solve this problem?

Thanks in Advance!

Answer Source

When you scroll, setOnCheckedChangeListener is called. So to handle that in get view you need to do the below -

viewHolder.checkBox.setOnCheckedChangeListener(null);
viewHolder.checkBox.setChecked(_songs.get(position).isSelected());

You first need to set onchecked null so that on scroll when view is getting populated and you are assigning value to checkbox views, you don't call the onCheckedListener.

And then you need to call OnCheckedChangeListener

viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int getPosition = (Integer) buttonView.getTag(); 
              //remove this line from the code. The below one
                _songs.get(getPosition).setSelected(buttonView.isChecked()); 


                if (_songs.get(getPosition).isSelected()) {
                    _songs.get(getPosition).setSelected(false);
                    Toast.makeText(getApplicationContext(), "position " + position + " " + _songs.get(position).getSongUrl(),
                            Toast.LENGTH_LONG).show();
                    Log.e("URL", _songs.get(position).getSongUrl() + " ");
                } else {
                    _songs.get(getPosition).setSelected(true);

                }


            }


        });
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download