0

I'm not sure how to approach this. I just leaning Android and wanted to try having a listview where you select an item from a list and then you are presented with a random item from another list. I'm not sure where to start or what would be a good direction to go with this. I currently have a listview that you click on to another listview for each item on the first list, but it feels clunky. Any ideas for a way to show one random item from a list?

public void onCreate(Bundle savedInstancesState) {
    super.onCreate(savedInstancesState);
    String[] fname = {"Bubbles", "Goldie",
            "Dog", "Fins", "Dory"};
    setListAdapter(new ArrayAdapter<String>(this,
            R.layout.fish, R.id.fishname, fname));

}
lioness
  • 13
  • 5
  • Have you tried using a random number generator? After selecting an Item, you could fire an event that will retrieve all IDs of the item then randomly select one from the list of IDs with the random number generator that are within the range of the list. – Burning Crystals May 06 '15 at 00:02
  • I'm not sure I understand your question. If you post the code you have so far, it might be easier to understand what you're trying to do. Thank you in advance. – Mikael Ohlson May 06 '15 at 00:03
  • I'm not sure how to get a random number generator to work with a list of items. I have looked at the Android Developer Reference for Random and I'm not sure how to make it work with text items. – lioness May 06 '15 at 00:16

1 Answers1

0

With reference to How do I generate random integers within a specific range in Java?, something like this perhaps:

public void onCreate(Bundle savedInstancesState) {
    super.onCreate(savedInstancesState);
    String[] fname = {"Bubbles", "Goldie",
            "Dog", "Fins", "Dory"};

    Random rand = new Random();
    int randomNum = rand.nextInt(((fname.length - 1) - 0) + 1) + 0;

    setListAdapter(new ArrayAdapter<String>(this,
        R.layout.fish, R.id.fishname, fname[randomNum]));

}
Community
  • 1
  • 1
terencey
  • 3,282
  • 4
  • 32
  • 40