Here I have Adapter class for Listview with Checkbox in Android. Many devloper face scroll problem wile using check box in listview. When we scroll the listview the checked items will be unchecked. To overcome this issue we need to use set tag for the check box. Herewith I have attached the Adapter class.
import java.util.ArrayList;
import com.move.R;
import com.move.model.ListItems;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
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.CompoundButton.OnCheckedChangeListener;
public class ListItemsAdapter extends BaseAdapter {
private Activity activity;
public ArrayList<ListItems> data;
private static LayoutInflater inflater = null;
public Resources res;
ViewHolder holder;
ListItems tempValues;
public ListItemsAdapter(Activity a, ArrayList<ListItems> d,
Resources resLocal) {
activity = a;
data = d;
res = resLocal;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
if (data.size() <= 0)
return 1;
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public static class ViewHolder {
public CheckBox listItemCheckBox;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
holder = new ViewHolder();
if (convertView == null) {
vi = inflater.inflate(R.layout.my_bits_list_items, null);
holder.listItemCheckBox = (CheckBox) vi
.findViewById(R.id.listItemChkBox);
holder.listItemCheckBox
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean checked) {
int getPosition = (Integer) buttonView.getTag();
data.get(getPosition).setSelected(
buttonView.isChecked());
}
});
vi.setTag(holder);
vi.setTag(R.id.listItemChkBox, holder.listItemCheckBox);
} else
holder = (ViewHolder) vi.getTag();
if (data.size() <= 0) {
// holder.text.setText("No Data");
} else {
tempValues = null;
tempValues = (ListItems) data.get(position);
holder.listItemCheckBox.setTag(position);
holder.listItemCheckBox.setText(tempValues.getFromAddress());
holder.listItemCheckBox.setChecked(tempValues.isSelected());
}
return vi;
}
}
Now you can use the listview without issue. Here vi.setTag(R.id.listItemChkBox, holder.listItemCheckBox) This is the important line.
Happy coding..
import java.util.ArrayList;
import com.move.R;
import com.move.model.ListItems;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
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.CompoundButton.OnCheckedChangeListener;
public class ListItemsAdapter extends BaseAdapter {
private Activity activity;
public ArrayList<ListItems> data;
private static LayoutInflater inflater = null;
public Resources res;
ViewHolder holder;
ListItems tempValues;
public ListItemsAdapter(Activity a, ArrayList<ListItems> d,
Resources resLocal) {
activity = a;
data = d;
res = resLocal;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
if (data.size() <= 0)
return 1;
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public static class ViewHolder {
public CheckBox listItemCheckBox;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
holder = new ViewHolder();
if (convertView == null) {
vi = inflater.inflate(R.layout.my_bits_list_items, null);
holder.listItemCheckBox = (CheckBox) vi
.findViewById(R.id.listItemChkBox);
holder.listItemCheckBox
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean checked) {
int getPosition = (Integer) buttonView.getTag();
data.get(getPosition).setSelected(
buttonView.isChecked());
}
});
vi.setTag(holder);
vi.setTag(R.id.listItemChkBox, holder.listItemCheckBox);
} else
holder = (ViewHolder) vi.getTag();
if (data.size() <= 0) {
// holder.text.setText("No Data");
} else {
tempValues = null;
tempValues = (ListItems) data.get(position);
holder.listItemCheckBox.setTag(position);
holder.listItemCheckBox.setText(tempValues.getFromAddress());
holder.listItemCheckBox.setChecked(tempValues.isSelected());
}
return vi;
}
}
Now you can use the listview without issue. Here vi.setTag(R.id.listItemChkBox, holder.listItemCheckBox) This is the important line.
Happy coding..