String usage as TextView ID - android

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

Related

Attaching random string from an array to a button

I am trying to make a random string from an array in my strings.xml file to appear on a random button in a linear layout. I inflate the button, choose a random string from my file, and them attach the random string to the button, then repeat for three buttons. everything works fine, I can see that it is attaching random strings every time, but the problem is that the button displays the name that I would use to refer to the string, not the strings actual value. For example if I have a string with a name of: string and value of: "Hello World", it just displays "string" as my button text.
private void loadButtons()
{
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int row = 0; row < guessRows; row++)
{
random = new Random();
Resources res = getResources();
String[] truthString = res.getStringArray(R.array.truthArray);
String truth = truthString[random.nextInt(truthString.length)];
Button newGuessButton = (Button) inflater.inflate(R.layout.guess_button, null);
newGuessButton.setText(truth);
buttonLayout.addView(newGuessButton);
}
Are you sure you're setting the string array correctly? Individual strings in an array shouldn't have name tags, the only name should be the array name. See the docs, and make sure you're declaring it properly.

Store multiple dynamic EditText value

I have a code adding multiple EditText. How can i store them into a array. This number 10 is just example, the number may bigger than that. How can i store it after click a button
for(int i=0; i<10; i++) {
String store[] = new String[10];
EditText addAnsMCQ = new EditText(this);
AnswerRG.addView(addAnsMCQ, 1);
addAnsMCQ.setWidth(200);
addAnsMCQ.setId(1000);
}
In your example the store variable isn't actually being used, did you intend do use it for storing the EditTexts?
Instead of using an array of String, just use an array of EditText and store a reference to them:
EditText store[] = new EditText[10];
for(int i=0; i<10; i++) {
EditText addAnsMCQ = new EditText(this);
AnswerRG.addView(addAnsMCQ, 1);
addAnsMCQ.setWidth(200);
addAnsMCQ.setId(1000);
store[i] = addAnsMCQ; //store a reference in the array to the EditText created
}
Then outside of the for loop, you can access the reference to each EditText, e.g.
store[0].setWidth(300);
You need to keep/get a reference to each of your EditText's then you can look up its value with .getText().toString() which you can store in whatever manner you like.
However if as you say
This number 10 is just example, the number may bigger than that.
If the number is going to be larger you should be using an Adapter and a ListView or something to hold your View objects. That will make it easier to get everything on to the screen. And will give you the benefit of view recycling.

How to Use String as a ResourceID in Android

I've several TextViews or another component, doesn't matter. And the views have iteration ids like: textView1, textView2, textView3 etc.
Simply I want to iterate ids by using pre-string values.
Psuedo example:
String pre_value = "textView";
for(int i = 0; i < size; i++) {
String usable_resource_id = pre_value + Integer.toString(pre_value);
// So how to use these id like R.id.textView1
// Cast or something similar
}
Any suggestions?
You can use Resources#getIdentifier() to get the identifier from a string.
But if you are going to iterate over them, wouldn't it be easier to keep the ids in an array or a list?

Calling a Resource by a string?

Here's the setup. I have a spinner, and each item in the spinner is associated with their own StringArray. I want to streamline the process of loading the StringArray when an item is selected in the spinner without using a bunch of if statements for each item.
The StringArray has the same name as the spinner item's text
Drawn out it would look like this:
String cat = parent.getItemAtPosition(pos).toString(); //Selected Spinner item (Category)
...
String catStringArray = "R.array." + cat;
listdata = getResources().getStringArray(catArray); //Get the StringArray
is there a way to do this correctly?
--Edit--
#EboMike
Your answer sent me on a hunt and ran into this which I'm now using:
Class res = R.array.class;
Field field = res.getField(selectedCategory);
int saId = field.getInt(null);
String[] myList = getResources().getStringArray(saId);
That's not a great approach. It's slow. It'd be better to have an internal integer array with all the R.string IDs or something similar.
If you really insist on using a string-based approach, use Resources.getIdentifier(). It's technically not a big deal if you only do it once.

Randomly displaying strings -

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.

Categories

Resources