0

I'm using source code from the link here

From the above source, I try to select the checked item and delete from the list.

Add delete button in layout:

  <Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Delete" />

set choice mode to multichoice :

adapter = new MyAdapter(this,getModel());
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(this);
    del_btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            DeleteSelecteditems();
        }
    });

and here is the DeleteSelecteditems method:

DeleteSelecteditems(){
int len = listView.getCount();
SparseBooleanArray checked = listView.getCheckedItemPositions();

Log.d("DeleteSelecteditems","no of checked item:"+checked.size());
    for (int i = 0; i < checked.size(); i++){
        //item position in adapter
        int pos= checked.keyAt(i);
        Log.d("DeleteSelecteditems","pos: "+pos);
        boolean valueat = checked.valueAt(i);
        boolean get = checked.get(i);
        Log.d("DeleteSelecteditems","get: "+get+" valueat:"+valueat);
        if (checked.valueAt(i)) {

            Model item = adapter.getItem(pos);//list.get(i);

            adapter.remove(item);
            adapter.notifyDataSetChanged();

        }

}

getview()

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder viewHolder = null;
    if (convertView == null) {
        LayoutInflater inflator = context.getLayoutInflater();
        convertView = inflator.inflate(R.layout.row, null);
        viewHolder = new ViewHolder();
        viewHolder.text = (TextView) convertView.findViewById(R.id.label);
        viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.check);
        viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int getPosition = (Integer) buttonView.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
            }
        });
        convertView.setTag(viewHolder);
        convertView.setTag(R.id.label, viewHolder.text);
        convertView.setTag(R.id.check, viewHolder.checkbox);
        } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }
    viewHolder.checkbox.setTag(position); // This line is important.

    viewHolder.text.setText(list.get(position).getName());
    viewHolder.checkbox.setChecked(list.get(position).isSelected());

    return convertView;
}
}

the checked array has always 13 items and all values are false. why?

Daniel Pratt
  • 12,007
  • 2
  • 44
  • 61
user755
  • 2,521
  • 3
  • 18
  • 29
  • 1
    try this `SparseBooleanArray checked = listView.getCheckedItemPositions(); for (int i = 0; i < listview.getCount(); i++){ if(checked.get(i)==true) { // do something } }`. also check this http://stackoverflow.com/questions/18162931/android-get-selected-item-using-checkbox-in-listview-when-i-click-a-button/18164177#18164177 – Raghunandan Aug 20 '13 at 07:31
  • I see no change in the behaviour.. still same – user755 Aug 20 '13 at 07:37
  • show your `getView`. you might doing it wrong there. check this http://stackoverflow.com/questions/17608002/pass-checkbox-data-to-next-activity/17608123#17608123. my guess your getview might not be right. – Raghunandan Aug 20 '13 at 07:38
  • please follow the link in the query to get the complete code. – user755 Aug 20 '13 at 07:41
  • pls post what you have tried here by editing the post – Raghunandan Aug 20 '13 at 07:42
  • updated my changes to the code used from the link. – user755 Aug 20 '13 at 07:47
  • See: http://stackoverflow.com/questions/3996938/why-is-listview-getcheckeditempositions-not-returning-correct-values – Marek Sebera Aug 20 '13 at 08:21
  • @Raghunandan: I tried your answer, have this issue: when last list item is selected the statement fails "if(adapter.mCheckStates.get(i)==true)" you need to check the keyat() and valueat() as given in my answer. – user755 Aug 20 '13 at 09:16

2 Answers2

0
SparseBooleanArray sarray = mMyAdapter.getarray();
    int size =sarray .size();
for(int i=0;i<size;i++)
     {
     int pos= sarray.keyAt(i);
     boolean valueat = sarray.valueAt(i);
     boolean get = sarray.get(i);
    if(valueat )
    {
        Model itemadpter= mMyAdapter.getItem(pos);

    }
}
user755
  • 2,521
  • 3
  • 18
  • 29
  • i don't know the links worked for the op. so i don't think my solution is wrong its just that you din't do it right. all you need is to get the data so in the if block `if(checked.get(i)==true) {Model itemadpter= mMyAdapter.getItem(i);}` not the pos. or use the arraylist with which you populate your listview – Raghunandan Aug 20 '13 at 09:24
  • I tried your answer implementing the SparseBooleanarray in the customAdapter. its works fine but not when the item selected after scrolling. The array give the correct info in KeyAt() and ValueAt() but get() method is not reliable. you could jus download the source code the link and give a try. – user755 Aug 20 '13 at 09:29
  • also check this http://stackoverflow.com/questions/18162931/android-get-selected-item-using-checkbox-in-listview-when-i-click-a-button/18164177#18164177. working code absolutely works fine. its your code i don't want to try. i stick to my stand that my solutions works – Raghunandan Aug 20 '13 at 09:29
  • in fact i picked the solution from Romain Guy who is a google engineer https://groups.google.com/forum/?fromgroups#!topic/android-developers/No0LrgJ6q2M. so you are saying his(http://stackoverflow.com/users/298575/romain-guy) solution is wrong? – Raghunandan Aug 20 '13 at 09:31
  • The solution is working depending on the methods invoked on customAdapter and arraylist. I tried your approach on adapter liek this which didnt worked. if(sarray.get(i)==true) { Model itemadpter= mMyAdapter.getItem(i); Log.d("test",itemadpter.getName()+" selected:"+itemadpter.isSelected()); } – user755 Aug 20 '13 at 09:38
  • if you don't do it right it's not that solution does not work you don't know how to modify it. good luck. – Raghunandan Aug 20 '13 at 09:39
  • based on this I used keyAt and valueAt methods on Adapter which is working as expected. – user755 Aug 20 '13 at 09:39
  • as far as i know i don't think you need that at all. any way its your code. happy coding – Raghunandan Aug 20 '13 at 09:40
0

Use:

SparseBooleanArray arraySparse = myGridView.getcheckeditempositions();

for(int i=0;i<arraySparse.size();i++){
//Shows values boolean of keys 
     Log.d("MI_APP",""+arraySparce.valueAt(i));
//Shows keys id or position of your element in the GridView
     Log.d("MI_APP",""+arraySparce.keyAt(i));
}

Regards!