0

I am using an Adapter:

final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, android.R.id.text1);

and I want to shuffle the contents of it, however I discovered that Collections.shuffle(adapter); does not work. Is there another method to do this? While keeping the format of adapter i.e. not changing it to a List

Ryan Sayles
  • 3,389
  • 11
  • 56
  • 79
  • are you calling notifydatasetchanged() after shuffling on your adapter ? – Aalok Sharma Mar 08 '13 at 18:12
  • If you don't want to use a List you may have to write a shuffle method yourself. [Random shuffling of an array in Android?](http://stackoverflow.com/questions/1519736/random-shuffling-of-an-array-in-android) – Sam Mar 08 '13 at 18:24
  • @AalokSharma adapter is not a supported type for `Collections.shuffle()` – Ryan Sayles Mar 08 '13 at 18:48
  • okay @rsayles3 My comment meant that are u calling notifydatasetchanged() on your adapter after calling shuffle ... is it understandable now , and have you even read the title of your question "Shuffling an Adapter" ?? – Aalok Sharma Mar 09 '13 at 16:18

1 Answers1

2

Of course Collections.shuffle(adapter) doesn't work..shuffle takes a java.util.List... The Java Collections API knows nothing about the Android API...

You need to shuffle the underlying List and then then tell the adapter that the data has changed..something like:

Collections.shuffle(myList);
adapter.notifyDataSetChanged();
LuxuryMode
  • 33,401
  • 34
  • 117
  • 188
  • Does this work with a primitive array as specified? – Sam Mar 08 '13 at 18:20
  • I don't have an underlying list, the adapter is populated manually by a user entering in String via a text input and that adapter is set in a `ListView` – Ryan Sayles Mar 08 '13 at 18:47
  • @rsayles3 Then you should collect the user input in a collection and base your adapter on that. The adapter should only be the V in the MVC pattern. – Simon Mar 08 '13 at 18:58