Reputation: 42854
What I have: I have a spinner, foe which I have set the adapter, the adapter displayed in coed below has a checkbox
and a textview
What is happening:: When I check the checkbox in one row and scroll below, some item in the below adapter is checked automatically. also when i scroll back up the adapter i checked also remained checked
What I am trying to do:: How can I preserve the proper selection of checkbox in the adapter all teh time. when i scroll up and down
AdptCategories.java
public class AdptCategories extends BaseAdapter {
ArrayList<HashMap<String, String>> categorySpinnerData;
Context context;
//ArrayList<ListObject> objects;
public AdptCategories(ArrayList<HashMap<String, String>> _categorySpinnerData, Context _context) {
super();
context=_context;
categorySpinnerData=_categorySpinnerData;
}
@Override
public int getCount() {
return categorySpinnerData.size();
}
@Override
public Object getItem(int position) {
return categorySpinnerData.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
HashMap<String, String> mapData=categorySpinnerData.get(position);
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.adpt_categories_spinner, null);
}
TextView txtCategoryNameId = (TextView) convertView.findViewById(R.id.txtCategoryNameId);
CheckBox categoryChkBxId = (CheckBox)convertView.findViewById(R.id.categoryChkBxId);
if(position==0){
txtCategoryNameId.setText(mapData.get("name"));
categoryChkBxId.setVisibility(View.INVISIBLE);
}else{
txtCategoryNameId.setText(mapData.get("name"));
txtCategoryNameId.setTag(mapData.get("id"));
categoryChkBxId.setVisibility(View.VISIBLE);
}
return convertView;
}
}
public class AdptCategories extends BaseAdapter {
ArrayList<HashMap<String, String>> categorySpinnerData;
Context context;
private ArrayList<Boolean> checked = new ArrayList<Boolean>();
//ArrayList<ListObject> objects;
public AdptCategories(ArrayList<HashMap<String, String>> _categorySpinnerData, Context _context) {
super();
context=_context;
categorySpinnerData=_categorySpinnerData;
}
@Override
public int getCount() {
return categorySpinnerData.size();
}
@Override
public Object getItem(int position) {
return categorySpinnerData.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
static class ViewHolderItem {
TextView txtCategoryNameId;
static CheckBox categoryChkBxId;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolderItem viewHolder;
HashMap<String, String> mapData=categorySpinnerData.get(position);
for (int i = 0; i < this.getCount(); i++)
{
checked.add(i, false);
}
ViewHolderItem.categoryChkBxId.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton checkbox,
boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked)
{
checked.set(position,true);
}
else
{
checked.set(position,false);
}
}
});
ViewHolderItem.categoryChkBxId.setChecked(checked.get(position));
if(convertView==null){
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.adpt_categories_spinner, null);
// well set up the ViewHolder
viewHolder = new ViewHolderItem();
viewHolder.txtCategoryNameId = (TextView) convertView.findViewById(R.id.txtCategoryNameId);
viewHolder.categoryChkBxId = (CheckBox)convertView.findViewById(R.id.categoryChkBxId);
// store the holder with the view.
convertView.setTag(viewHolder);
}else{
// we've just avoided calling findViewById() on resource every time
// just use the viewHolder
viewHolder = (ViewHolderItem) convertView.getTag();
}
if(position==0){
viewHolder.txtCategoryNameId.setText(mapData.get("name"));
viewHolder.categoryChkBxId.setVisibility(View.INVISIBLE);
}else{
viewHolder.txtCategoryNameId.setText(mapData.get("name"));
viewHolder.txtCategoryNameId.setTag(mapData.get("id"));
viewHolder.categoryChkBxId.setVisibility(View.VISIBLE);
}
return convertView;
}
}
Upvotes: 0
Views: 1548
Reputation: 2326
this is due to the recycling of views in spinner,what you can do is declare an boolean array private ArrayList<Boolean> checked = new ArrayList<Boolean>();
and initialize in your adapter constructor
for (int i = 0; i < this.getCount(); i++)
{
checked.add(i, false);
}
and now your get view method use it like this:-
your_checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton checkbox,
boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked)
{
checked.set(position,true);
}
else
{
checked.set(position,false);
}
}
});
your_checkbox.setChecked(your_data.get(position));
and please use viewholder, its a good practice.
Upvotes: 2