How to get strings from array randomly without repeating the string? - android

I'm making a quiz app I have string array and I want to load them on button click, strings should be loaded randomly into Text View. Strings should not be repeated.
Thanks..

Because it's your quiz, I'm gonna give you hints not the whole soltion
Random rnd = new Random;
while(array.size()>0){
// 1. use rng to get a index between 0 to current array.size()
// 2. remove the string by array.remove(index) so that it won't duplicated.
// 3. setText(string)
}
Try this, you can add comments if you need more details.

One way to do it is to convert the array to a list and shuffling it.
List myList = Arrays.asList(yourArray);
Collections.shuffle(myList);
After that you can just iterate over the list.

Related

Android ArrayList populating unnecessary elements

I am currently creating an app that fetches 2 Strings randomly from an arraylist.
This is what I did:
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("someString");
// continue arrayList.add() for another thousand times for different strings.
// I am adding the Strings manually as these are specific questions in a quiz app
Random random = new Random();
myTextView.setText(arrayList.get(random.nextInt(arrayList.size());
myTextView2.setText(arrayList.get(random.nextInt(arrayList.size());
The Question:
At any point in time, I only need 2 Strings to be displayed to the user. But I am populating a thousand element arrayList, and fetching only 2 Strings from there. Is there a better way to do this? For example, only populating 2 Strings at runtime, without populating the unnecessary Strings?
Or maybe it does not matter at all?
Thank you very much in advance!

How would I save an Integer array list to shared preferences

I've been trying to save an integer array list in shared preferences for a couple of days now but cannot figure out how to do it.
here's the array list i'm trying to save
////list that contains checked routes///
ArrayList<Integer> checkedRoutePosition = new ArrayList<>();
and here's how the array obtains the values within it
int listViewItemPosition = ((Activity) getContext()).getIntent().getIntExtra("listViewItemPosition",0);
checkedRoutePosition.add(listViewItemPosition);
In another activity after you click the back button a new intent is started that takes me to this activity. The intent passes in the listViewItemPosition value that I need to save to the array.
The above lines of code are in my getView method of my custom adapter for a listview. After saving them I want to compare them to position in my getView. Where the values are equal I want to set a certain image. Is this code the right way to do that?
for(int i=0; i<checkedRoutePosition.size(); i++)
if(position == checkedRoutePosition.get(i)) {
checkImageView.setImageResource(checkImageResourceId);
}
Thanks for any help!
You can't write any arrays or arraylists to shared preferences. The closest you can do is write the integers to a comma separated string, write the string, and parse it when you need to read it. This is only appropriate if the size of the array is relatively small. If its large, you need to move away from SharedPreferences to another form of storage such as a file or database.

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 ...

Gerator Image Random

I have is how to generate random images without repeating , as an example driving in an array 15 images and 7 images to send imageview but that are not repeated .
int[] img={ R.drawable.ima1,R.drawable.ima2,R.drawable.ima3,R.drawable.ima4,R.drawable.ima5,R.drawable.ima6,R.drawable.ima7,R.drawable.ima8,R.drawable.ima9,R.drawable.ima10,R.drawable.ima11,R.drawable.ima12,R.drawable.ima13,R.drawable.ima14,R.drawable.ima15};
int [] game= new int[7];
Random numerRan = new Random();
for (int i=0;i<game.length ;i++)
{
int num= numerRan.nextInt(17);
int x= img[num];
game[i]=img[num];
}
img1.setImageResource(juego[0]);
img2.setImageResource(juego[1]);
img3.setImageResource(juego[2]);
img4.setImageResource(juego[3]);
img5.setImageResource(juego[4]);
img6.setImageResource(juego[5]);
img7.setImageResource(juego[6]);
img8.setImageResource(juego[7]);
When I press the button control images to the score rando the position that was generated . The problem I have is that images are repeated.
I am using 2 arrangements the first is to save all the images and the second keep the random images that will assign imageview .
Instead of using an int[] array, use a List<Integer> instead, such as an ArrayList<Integer>.
That way, you'll be able to use Collections#shuffle(), which will have the desired effect. It'll randomly permute your list, in a non-repeating way. All you'll have to do later is to iterate on that List.
For more info, please refer to the Collections documentation.
You can also convert that array to a List, like the following:
List<Integer> myList = Arrays.asList(img);
For more info about that, take a look at this question.

Display random word from list

I am a beginner and I am making a an android app. My goal is to have the app randomly return a specific word from a list of word when a button is pressed. How should I go about doing this? Also, I am using eclipse and do not have much experience at all.
Since you already mentioned in your question that you want to generate a word from a list of words, I assume that you already have the list prepared.
Use the following to generate a random number and then display the word corresponding to that index.
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(100); // maximum value 100; you can give this as the maximum index in your list of words
Note : do not use Math.random (it produces doubles, not integers)
import java.util.Random;
EDIT :
Assume that you have the list of words in a String array which contains 30 words.
String wordList[] = new String[30];
// enter the words in the wordList (if you have a String, use a Tokenizer to get the individual words and store them in wordList)
int randomInt = randomGenerator.nextInt(30);
String wordToDisplay = wordList[randomInt];
Then, you can display wordToDisplay in your TextView by using mTextView.setText(wordToDisplay);

Categories

Resources