Sunday, May 16, 2010

[android-developers] Re: How to get selected items in a ListView (multi selection check boxes)? - URGENT PLZ

You are not using SparseBooleanArray correctly.

selectedContacts.size() returns the number of mapped (key, value)
pairs.
In for loop you assume that keys are in the range 0, size - 1; and
they are not.

In SparseBooleanArray keys and indexes are two different things.

You will obtain proper key with SparseBooleanArray.keyAt(int index)


Viktor.


On May 11, 12:34 am, dillipk <codersnet2...@gmail.com> wrote:
> Hello,
>  How do I get the selected items in a ListView which contains a multi
> selelected check boxes.? The following code doesn't work correctly...
> getContacts() is being called in a Button click().
>
> private String getContacts(){
>
>                 SparseBooleanArray  selectedContacts =
> listView.getCheckedItemPositions();
>                 StringBuffer sb = new StringBuffer();
>                 int size = selectedContacts .size();
>
>         for (int sbaKey = 0; sbaKey < size; sbaKey++)
>         {
>             if (selectedContacts .get(sbaKey, false))
>             {
>                 //Log.d(TAG, "Selected Categories : " +
> listView.getItemAtPosition(sbaKey).toString());
>                 sb.append(listView.getItemAtPosition(sbaKey).toString());
>                 if(sbaKey <=  size - 2 ){
>                         sb.append(',');
>                 }
>             }
>         }
>
>         Log.d(TAG, "Selected Contacts : " + sb.toString());
>         return sb.toString();
>
> }
>
> Creation:
>
>  @Override
>     public void onCreate(Bundle savedInstanceState) {
>         super.onCreate(savedInstanceState);
>         this.setTitle("Preferences");
>         setListAdapter(new ArrayAdapter<String>(this,
>                 android.R.layout.simple_list_item_multiple_choice,
> CONTACTS));
>
>         listView = getListView();
>
>         listView.setItemsCanFocus(false);
>         listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
>
>     }
>
> Thank you for your help in advance..
>
> -DK
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscribe@googlegroups.com
> For more options, visit this group athttp://groups.google.com/group/android-developers?hl=en

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment