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.
Related
Currently I have added the dynamic TextInputEditText fields to the LinearLayout where all the dynamically added fields stores(only holds dynamic EditText fields only).
However, when I read each EditText field and fetch it's data, the value of last text field replace all the other values in the array.
Example:
Adds 3 dynamic fields, with the corresponding values of "AA","BB","CC". When i read the array, it shows like this,
Output: "CC,CC,CC"
Code:
private void fetchCertificates(){
ArrayList<String> certs = new ArrayList<>();
for(int i =0;i<linearLayout.getChildCount();i++){
View certificateView = linearLayout.getChildAt(i);
TextInputEditText newCerts = findViewById(R.id.new_certs);
String name = newCerts.getText().toString();
certs.add(name);
}
String certList = android.text.TextUtils.join(",", certs);
Log.i("Certificates",certs);
}
Objective:
Read dynamically added TextInputEditText, and store the values in an array.
References: page-1 (This did not work)
I was able to resolve it in following way, thanks for the questioning hellboy and blackapps, it made me think bit differently.
private void fetchCertificates(){
ArrayList<String> certs = new ArrayList<>();
for(int i =0;i<linearLayout.getChildCount();i++){
View certificateView = linearLayout.getChildAt(i);
TextInputEditText newCerts = certificateView.findViewById(R.id.new_certs);
String name = newCerts.getText().toString();
certs.add(name);
}
String certList = android.text.TextUtils.join(",", certs);
Log.i("Certificates",certs);
}
The problem in your code is at these lines,
View certificateView = linearLayout.getChildAt(i);
TextInputEditText newCerts = findViewById(R.id.new_certs);
Assuming that the LinearLayout(parent) carries only TextInputEditText(children), when you iterate through LinearLayout you will get TextInputEditText only, so your code should be like,
View certificateView = linearLayout.getChildAt(i);
TextInputEditText newCerts = (TextInputEditText) certificateView;
or
TextInputEditText newCerts = (TextInputEditText) linearLayout.getChildAt(i);
In your code since you directly used findViewById you always got the last to the latest view(child) with this id(remember if there are multiple views with the same id, then the latest view defined, both in Activity or layout, will be fetched) that's the reason you get CC, CC, CC.
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
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.
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?
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.