I am newbie to android and have implemented a listView with checkbox and textViews, I am facing a weird issue of selection.
When I am checking any checkbox, all the other checkbox are automatically checked and it is changed after scrolling randomly.
Can anybody help me what changes should I make to fix this in my code?
ServiceAdapter
package one.tusk.stush.adapters;
import android.content.Context;
import android.content.Intent;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import com.company.stush.R;
import java.util.ArrayList;
import java.util.HashMap;
import one.tusk.stush.other.Const;
public class ServiceAdapter extends BaseAdapter {
public ArrayList<HashMap<String, String>> contArray;
SparseBooleanArray mArray;
private Context mContext;
String resnID, reson;
Intent i;
public ServiceAdapter(Context paramContext, ArrayList<HashMap<String, String>> contList) {
this.mContext = paramContext;
this.contArray = contList;
mArray = new SparseBooleanArray(contList.size());
}
public int getCount() {
return this.contArray.size();
}
public Object getItem(int paramInt) {
return Integer.valueOf(paramInt);
}
public long getItemId(int paramInt) {
return paramInt;
}
public View getView(final int paramInt, View paramView,
ViewGroup paramViewGroup) {
LayoutInflater localLayoutInflater = (LayoutInflater) this.mContext
.getSystemService("layout_inflater");
Viewholder localViewholder = null;
Const.selectedIDs.clear();
if (paramView == null) {
paramView = localLayoutInflater.inflate(
R.layout.raw_single_contact, paramViewGroup, false);
localViewholder = new Viewholder();
localViewholder.tv_name = ((TextView) paramView
.findViewById(R.id.tv_name));
localViewholder.chk_id = ((CheckBox) paramView
.findViewById(R.id.chk_id));
paramView.setTag(localViewholder);
} else {
localViewholder = (Viewholder) paramView.getTag();
localViewholder.chk_id.setOnCheckedChangeListener(null);
localViewholder.chk_id.setChecked(mArray.get(paramInt));
}
localViewholder.tv_name.setText(contArray.get(paramInt).get("serviceText"));
localViewholder.chk_id.setChecked(mArray.get(paramInt));
//localViewholder.chk_id.setChecked(false);
//localViewholder.chk_id .setTag(paramInt);
localViewholder.chk_id.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
try {
if (isChecked) {
Const.selectedIDs.add(contArray.get(paramInt));
Const.serviceArrayList.get(paramInt).put("flag", "1");
} else {
Const.selectedIDs.remove(contArray.get(paramInt));
Const.serviceArrayList.get(paramInt).put("flag", "0");
}
}catch (Exception e){
e.printStackTrace();
}
}
});
return paramView;
}
static class Viewholder {
TextView tv_name;
CheckBox chk_id;
}
}