简体   繁体   中英

Android Studio - How to create a back button on randomly selected strings?

I have an array of strings that I am displaying in my activity using a random number every time a button called Next is clicked. How do I create another button called Back , that simply displays the previously or last generated strings in the order they were randomly generated?. So that instead of clicking next to generate another string, I can click Back at any time to view all the previous strings one by one and then click Next again to continue the random selection.

Create a array list of String like

ArrayList<String> arraylist= new ArrayList<String>();

after that using add function add the strings that you are displaying in your activity using a random number every time a button called Next is clicked.

 arraylist.add("your_randomly_generated_string");

now your randomly generated string will stored in arraylist one by one This will store the string now how to retrieve? following steps may help you not sure ... While adding the randomly generated string into arraylist initialize the int variable with zero and increment its value after each adding...

public int indexvalue=0;

on your Onclick of NEXT button add two lines

arraylist.add("your_randomly_generated_string");
indexvalue++;

And now on the Onclick of BACK Button Use indexvalue to retrieve...

String data = arraylist.get(indexvalue);
indexvalue--;

here Decremented the indexvalue to retrieve the previous string.. hope you'll understand what im trying to say little bit of lengthy explanation ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM