hi i am trying to implement a long list in which user can check the items in list view.when he scroll the list item then i store the checked items in sparseboolean array but when i try to check the items on clicking a button that which items is checked. it shows strange behaviour.when i check first item and click button then it gave right result but when i start checking the items from bottom of list. then it shows strange behaviour.there are 14 items indexed from 0-13.when i check 13th element it shows no toast.when i click 12th element it shows no toast .when i click 12 th element it shows no toast first toast arise when i click on 8th element.please help me in figuring out where i am wrong and help me in fixing the bug.when i click on 7th element three toast come 7,8,9 when i click on 6th element 5toast arise 6,7,8,9,10.please help me in fixing this bug.i want to show the the number in toast which are selected. here is my code
/**
* This example shows how to create a list of checkboxes.
*/
public class CheckboxActivity extends ListActivity implements
AdapterView.OnItemClickListener {
static SparseBooleanArray mCheckStates;
private CheckBoxAdapter mCheckBoxAdapter;
List<Boolean> check;
Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mCheckBoxAdapter = new CheckBoxAdapter(this,
R.layout.list_item_checkbox, GENRES);
setListAdapter(mCheckBoxAdapter);
check=new ArrayList<Boolean>();
final ListView listView = getListView();
listView.setItemsCanFocus(false);
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(this);
btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
for(int i=0;i<mCheckStates.size()+1;i++)
if(mCheckStates.get(i)==true)
Toast.makeText(CheckboxActivity.this,i+" " ,Toast.LENGTH_SHORT).show();
}
});
}
public void onItemClick(AdapterView parent, View view, int
position, long id) {
mCheckBoxAdapter.toggle(position);
}
private static class CheckBoxAdapter extends ArrayAdapter<String>
implements CompoundButton.OnCheckedChangeListener {
public CheckBoxAdapter(Context context, int resource, String[]
objects) {
super(context, resource, objects);
mCheckStates = new SparseBooleanArray(objects.length);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final CheckBox view = (CheckBox) super.getView(position,
convertView, parent);
view.setTag(position);
view.setChecked(mCheckStates.get(position, false));
view.setOnCheckedChangeListener(this);
return view;
}
public boolean isChecked(int position) {
return mCheckStates.get(position, false);
}
public void setChecked(int position, boolean isChecked) {
mCheckStates.put(position, isChecked);
notifyDataSetChanged();
}
public void toggle(int position) {
setChecked(position, !isChecked(position));
}
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
mCheckStates.put((Integer) buttonView.getTag(), isChecked);
}
}
private static final String[] GENRES = new String[] {
"Action", "Adventure", "Animation", "Children", "Comedy",
"Documentary", "Drama",
"Foreign", "History", "Independent", "Romance", "Sci-Fi",
"Television", "Thriller"
};
}