Android Display a random Item from List without showing the ListView

1.3k views Asked by At

I am practicing on this project called GetTours i got from github, now I want to display random Item from the ListView without seeing the ListView. not only show random item from ListView, but also print the details of the items, the item contains Name, address and lat long value for the map marker. is that even possible?

or maybe, Teach me how to create a list that contains a name, address and lat long value and how to display it. I am also planning of randomizing it every time i shake the device. I had an app that do shake an random with a single data array, now I want to randomize a place with Name, address and a Map marker in the said location per shake. please help!

1

There are 1 answers

4
lcw_gg On BEST ANSWER

A ListView is not needed here, save your items in a List :

ArrayList<Item> items = new ArrayList<Item>;
items.add(item1);
items.add(item2);
//...

Then select a random number with the maximum as the length of your list :

final int random = Random.nextInt(items.size());

And then, do what you need to do with the data :

Item randomItem = items.get(random);
nameTextView.setText(randomItem.getName());
priceTextView.setText("" + randomItem.getPrice());
//...