0

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" 
    }; 
} 
mathlearner
  • 7,509
  • 31
  • 126
  • 189
  • HUH?! Please consider breaking down your paragraph into chunks. It is hard to understand that 5 line sentence which describes your flow of checking items. – Jack Oct 18 '11 at 15:03
  • @Jack hi i think now i explain correctly my problem please help. – mathlearner Oct 18 '11 at 15:13
  • did you ever figure this out? I just noticed that in your for loop for btn onClickListener, you are going from i=1, to mCheckStates.size() + 1, which I think should just be mCheckStates.size() – Jack Nov 22 '11 at 14:16

2 Answers2

2

What is this method doing?

public void onCheckedChanged(CompoundButton buttonView, 
                            boolean isChecked) { 
    mCheckStates.put((Integer) buttonView.getTag(), isChecked); 
}

Do your list rows have a button in them?

EDIT: Actually - see this question as I think it will help.

Community
  • 1
  • 1
Jack
  • 9,156
  • 4
  • 50
  • 75
  • view.setOnCheckedChangeListener(this) in this it implements the functions of OnCheckedChangListener of the CheckBox.no list row have no button button is outside list view when it is clicked it shows the items which are checked. – mathlearner Oct 18 '11 at 16:43
0

Just tried to answer the resembling question Here.

Secondly, this strange behavior is because ListView reuses the view sp you have to maintain the state of the check box and use that state in getView of you Adapter.

Community
  • 1
  • 1
Khawar
  • 5,197
  • 4
  • 28
  • 52