Randomly displaying strings - - android

I have a set of strings labeled st1, st2, st3, etc...until st9. I want to have a method that randomly shows one of those strings based off a randomly generated number...could someone please show me how to do this?
Thanks!

You'll want to place them in an array and access the member of the array that your generated random number tells you to.
Android generally means java so:
import java.util.Random
Random rand = new Random();
String[] myarray = new String[]{st1, st2, st3, st4, st5, st6, st7, st8, st9};
int myrand = rand.nextInt(8);
System.out.println(myarray[myrand]);
Forgive me any minor syntax errors, it's been a while since I've programmed in Java.

put your strings into an array, pick a random integer between zero and the length of the string array, and use the number to index into the array.

If you have a set of strings defined in strings.xml then it will look more like this:
import java.util.Random
Random rand = new Random();
int[] myarray = new int[]{
R.strings.my_string_1,
R.strings.my_string_2,
R.strings.my_string_3,
R.strings.my_string_4
};
int myrand = rand.nextInt(myarray.length-1);
System.out.println(getText(myarray[myrand]));
This assumes that you're doing this in an Activity where the getText method is available.

Related

Pulling individual strings from string arrays at random

Im trying to pull individual strings from a string array at random, and display it on screen(in android studio). But i cant seem to find a solution anywhere.
Its a simple string array, and i need to pull one at a click of a button. My string array is pretty standard and set up like this:
<string-array name="string_array1">
<item>Sentence 1</item>
<item>Sentence 2</item>
</string-array>
You can user java String array or ArrayList of String in Activity like:
Java String Array Example in Activity :
1)define an Array
String string_array1[]={"Sentence 1","Sentence 2"};
Get value from the array :
Sting zeroIndexValue=string_array1[0];
Sting oneIndexValue=string_array1[1];
2) ArrayList Example:
define ArrayList of String:
ArrayList<String> string_List=new ArrayList<String>();
Add value to List:
string_List.add("Sentence 1");
string_List.add("Sentence 2");
Get value from List:
string_List.get(0);
string_List.get(1);
fetch array from string file
String[] string_array1 = getResources().getStringArray(R.array.string_array1);
Now generate random value and fetch it from array.
Random rand = new Random()
int Low = 0;
int High = string_array1.length-1;
int value = rand.nextInt(High-Low) + Low; //It will generate random number in the given range only.
String printed_value = string_array1[value];

Android setter of String array

now i've got simple setter and getter of string array. I want to use setter to put some retrevied json info + same text to array. When i use belowe code:
met.setPlacepic(new String[]{"http://dfsdfsdfsf/" + json.getString("source")});
it looks like setter put only one string to array, despite there is many more data.
Declaration is simple
public String[] placepic
and the setter is also simple:
public void setPlacepic(String[] placepic) {
this.placepic = placepic;
}
Anybody knows reason of this?
If the number of strings is fixed (you know exactly how many element you would have in the array), then you could use String Arrays:
String[] placepic = new String[20]; //20 strings
//Then, in your loop:
placepic[i] = yourData;
If you do NOT know how many strings in your data, You should use List:
List<String> placepicList= new ArrayList<String>();
//Then, in your loop:
placepicList.add(yourData);
//Then after the loop, you get the array
String[] placepic = placepicList.toArray(new String[placepicList.size()]);

String usage as TextView ID

I would like to generate a random string(with some rules), than use it as a textview id. For example I would like to use settext with this string.
Purpose: I should select a textview randomly, than set its text to another.
Actually, there are different kinds of way to achieve this purpose. For instance you could have an array of texts that can be selected randomly.
String[] strArr = { "text1", "text2", "text3" };
Random rand = new Random();
int selected = rand.nextInt(3);
textView.setText(strArr[selected]);
If you MUST get the string from other textviews then you can create an array of IDs instead of an array of text. Then use the Random object to get an ID and then something like:
TextView textToGetString = (TextView) findViewById(idArray[selected]);
String newText = textToGetString.getText();
Your thought process seems a little complicated, but there could be a simpler solution. Ids are really only used by Android as placeholders for an integer. Instead of randomly generating an id's placeholder, you could populate an integer array with all the ids you want to use and then randomly select one from that array. Implementation could be as follows in your activity:
Random rand = new Random();
int[] myTextViews = new int[]{R.id.textView1, R.id.textView2, R.id.textView3}
int length = myTextViews.length;
TextView tV = (TextView)findViewById(myTextViews[rand.nextInt() % length]);
tV.setText("Whatever Text You Want");
I hope this helps! Good luck

Randomly select a string from strings.xml in Android

I need to randomly select a string defined within strings.xml file in android.
For example my strings.xml is :
<resources>
<string name="str1">Content comes here1</string>
<string name="str2">Content comes here2</string>
<string name="str3">Content comes here3</string>
</resources>
Can I randomly select one of these strings in my Activity?
Create an array contains all of your resource names you want to select:
String[] strs = new String[] {"str1", "str2", "str3"};
Get a random index:
int randomIndex = new Random().nextInt(3);
Get your random string from resource:
int resId = getResources().getIdentifier(strs[randomIndex ], "string", your_package_name);
String randomString = getString(resId);
The best way is you declare you Strings as an Array, then get it like this:
String[] arrayOfStrings = context.getResources().getStringArray(R.array.your_string_array);
String randomString = arrayOfStrings[new Random().nextInt(arrayOfStrings.length)];
Then you can use it as you like.
You would probable rather make it an array of strings (and then that is easier to select at random one of the array). Else, you can put the ids of your strings in an array and randomly select one of the items in the array.

android best way to write and read lots of arrays?

I've been looking at this http://blog.javia.org/assembly-java/ and in particular this bit #6: Don’t use String. Use char array instead.
Does anyone have an example of storing and retrieving data in this way?
For example I have arrays like this
int[] myInts = new int[256];
int[] myotherInts = new int[256];
byte[] mybytes = new byte[someLength];
Essentially:
String text = "To be or not to be";
char[] textArray = new char[3];
text.getChars(9, 12, textArray, 0);
So all of your strings are stored in a single array, and then extracted separately using getChars().

Categories

Resources