I know that this question has been asked over and over again but still I've not been able to find a helpful suggestion. Check box is getting unchecked when I scroll the List view. I am using Array List to store id of a selected item in the list view when particular item is unchecked it is removed from List view.
public class ManagePracticeLogAdapter extends BaseAdapter
{
Context context;
LayoutInflater inflater;
MenuItem menu,addlog;
List<Integer> SelectedBox;
ArrayList<HashMap<String, String>> data;
HashMap<String, String> resultp = new HashMap<String, String>();
public ManagePracticeLogAdapter(
Context context,
ArrayList<HashMap<String, String>> arraylist,
MenuItem mymenu,
MenuItem myaddlog)
{
this.context = context;
data = arraylist;
menu=mymenu;
addlog=myaddlog;
}
@Override
public int getCount()
{
return data.size();
}
@Override
public Object getItem(int position)
{
return null;
}
@Override
public long getItemId(int position)
{
return 0;
}
// Method to display data of Produce log Activity in list view
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
TextView datetime;
TextView totminutes;
TextView skills;
TextView weather;
final CheckBox chkdelete;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.logitem1, parent, false);
// Get the position
resultp = data.get(position);
// Locate the TextViews in listview_item.xml
datetime = (TextView) itemView.findViewById(R.id.id_datetime);
totminutes = (TextView) itemView.findViewById(R.id.totminutes);
skills= (TextView) itemView.findViewById(R.id.id_skills);
weather=(TextView)itemView.findViewById(R.id.id_weather);
chkdelete=(CheckBox)itemView.findViewById(R.id.id_chkDelete);
// Capture position and set results to the TextViews
datetime.setText(resultp.get("Skill_practice"));
totminutes.setText(resultp.get("Day_minutes")+" min");
skills.setText(resultp.get("Night_minutes"));
weather.setText(resultp.get("Prac_Date"));
String fontPath = "fonts/Roboto-Light.ttf";
Typeface tf = Typeface.createFromAsset(context.getAssets(), fontPath);
datetime.setTypeface(tf);
totminutes.setTypeface(tf);
skills.setTypeface(tf);
weather.setTypeface(tf);
SelectedBox = new ArrayList<Integer>();
chkdelete.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if(SelectedBox.size()-1==0)
{
menu.setVisible(false);
addlog.setVisible(true);
}else
{
addlog.setVisible(false);
}
if(isChecked)
{
SelectedBox.add(buttonView.getId());
menu.setVisible(true);
addlog.setVisible(false);
}else if(!isChecked)
{
SelectedBox.remove(SelectedBox.indexOf(buttonView.getId()));
}
}
});
menu.setOnMenuItemClickListener(new OnMenuItemClickListener()
{
@Override
public boolean onMenuItemClick(MenuItem item)
{
// TODO Auto-generated method stub
if(!SelectedBox.isEmpty())
{
Toast.makeText(context, "Menu option 4 added!",
Toast.LENGTH_LONG).show();
}
return false;
}
});
itemView.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
Intent intent = new Intent(context,LogEdit.class);
intent.putExtra("s11","Update Practice");
context.startActivity(intent);
}
});
return itemView;
}
}