I want to use an intent string extra to dynamically determine what String array in an XML file to use.
Java code:
Intent myIntent = getIntent();
String stringID = myIntent.getStringExtra("stringID");//pull string id
String[] allStrings = getResources().getStringArray(stringID);
The XML:
<string-array name="set1">
<item>item1</item>
<item>item2</item>
<item>item3</item>
</string-array>
The last line in the java code doesn't work because it wants something like r.array.set1, but I want to choose this dynamically instead. How can I accomplish this? Would it be easier to use the ID of the string array somehow?
Actually r.array.set1 is a reference to an Integer. So you should pass it as an Integer, not a String. So:
Intent myIntent = getIntent();
int intID = myIntent.getIntExtra("stringID", r.array.set1);//The second parameter is the default value if nothing is specified
String[] allStrings = getResources().getStringArray(intID);
And in the other activity pass it as an Integer:
intent.putExtra("stringID", r.array.set1);
Related
I have a problem with adding values to list which is in SecondActivity. In MainActivity I set text in EditText boxes and send to second class. For the first time values are adding, but when I back to previous activity and one more time set text and send it, values in the list are replaced, not added. Someone know what is the source of this problem?
try this in first activity:
String[] array = {"Hi", "there", "yeah"};
Intent goIntent = new Intent(this, NewAppActivity.class);
/*
* put extra with "array" as a key and the String[] with your values as the value to pass
* */
goIntent.putExtra("array", array);
startActivity(goIntent);
and in second activity:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String[] array = extras.getStringArray("array");
}
I'm currently developing android quiz app and I implemented the "reset" button that resets the questions and randomize them, but I'm having trouble with calling the random Strings from .xml file in MainActivity.java.
I have all of my questions listed in the Strings.xml file like this, in order to make them easy to translate:
<string name="q1">The reactor at the site of the Chernobyl nuclear disaster is now in which country?</string>
<string name="a1">A. Slovakia</string>
<string name="b1">B. Ukraine</string>
<string name="c1">C. Hungary</string>
<string name="d1">D. Russia</string>
<string name="q1answer">B</string>
All of the questions are listed in that file like q1, q2, q3, and the same goes for ABCD answers: a1, b1, c1, d1; a2, b2, c2, d2, and so on.
There are many questions, but button chooses only 5 of them and displays them on the screen. The problem is that I just can't access the Strings if I want to use an integer generated by randomizer to find them in .xml:
for (int i = 1; i <= 5; i++) {
// I managed to get the identifier of the TextView with the for loop like that (TextViews for questios are listed q1q, q2q, q3q, q4q, q5q):
// I would like to do the same thing down there with Strings.
TextView question = (TextView) findViewById(getResources().getIdentifier("q" + i + "q", "id", this.getPackageName()));
// randomQuestion is the number of the question in random number generator from different method.
randomQuestion = randomizeNumbers();
// And here I'm stuck, this will show "q1-randomNumber" instead of the real question,
// because it does not see it as an ID. I tried various different solutions, but nothing works.
question.setText("q" + randomQuestion);
// I left the most silly approach to show what I mean.
}
What can I do to make the computer distinguish the name of the String? So it displays the real question instead of "q1" "q6" "q17"?
Thank you for help in advance!
You can access strings from the string.xml file by using the getIdentifier() utility.
private String getStringByName(String name) {
int resId = getResources().getIdentifier(name, "string", getPackageName());
return getString(resId);
}
It would be a lot easier if you also tried another approach entirely and create a Problem class which would look something like :
public class Problem(){
int ID;
String question;
String answer1; String answer2; //..and so on
String answerCorrect;
public class Problem(int ID, String question, ...){
this.question= question;
...
}
}
And than, create and ArrayList = new ArrayList<>(); and populate it with your questions. Than, you would have the ability to access them by their ID / position in array, search them by name and so on. In the future, you can use this model to request the list of problems from a server too.
question.setText("q" + randomQuestion);
In here, your parameter is inferred as a string since you pass "q", so it will display "q-randomNumber" as expected. I think what you want is to pass and actual ID to setText. To do this, you can do the same approach you did to find the question textview ID using "string" instead of "id".
In the MainActivity or in a function :
Resources res = getResources();
myString = res.getStringArray(R.array.YOURXMLFILE);
String q = myString[rgenerator.nextInt(myString.length)];
// WE GET THE TEXTVIEW
tv = (TextView) findViewById(R.id.textView15);
tv.setText(q);
// WE SET THE TEXTVIEW WITH A RANDOM QUESTION
And declare :
private String[] myString;
private static final Random rgenerator = new Random();
private TextView tv;
I am newcomer to programming and I am attempting to create an Android app using Android Studio. I've tried searching but my findings do not appear to be what I am looking for, because they seem to be overly complex. What I've written below is just an example.
I want to be able to return a string from string.xml when user types "whale". The string in this case is information about the whale.
This is my java file, animal is already a string entered from a form.
TextView textview = new TextView(this);
String animalType = "water_" + animal; // This become water_whale if user typed whale
String animalInfo = getString(R.string.animalType); // This doesn't work
textView.setText(animalInfo);
This is my string.xml
<string name="water_fish">Fish is a small bla...</string>
<string name="water_whale">A whale is an enourmous blabla...</string>
<string name="land_giraffe">Africa.</string>
I have probably tunneled on this particular way and I have probably miss something obvious or is there another way to do this?
R.string.anyIdentifer represents an integer value. You can't add your own identifier with it, just the way you can't call any non existent property on any class. If you want to access any resource dynamically with it's name then there is a different approach for that.
Use this
TextView textview = new TextView(this);
String animalType = "water_" + animal;
int animalTypeId = getResources().getIdentifier(animalType, "string", getActivity().getPackageName())
String animalInfo = getResources().getString(animalTypeId);
textView.setText(animalInfo);
String string=getResources().getString(R.string.water_whale);
you can't use getString() method directly.
The value in string.xml file...
<string name="bangla_history_2ndpoint">SOME VALUE </string>
From this activity i am trying to pass value to another activity ... by using putextra
Intent ptwo=new Intent("com.powergroupbd.victoryday.of.bangladesh.HISTORYDESCRIPTION");
ptwo.putExtra("header", R.string.bangla_history_2ndpoint);
startActivity(ptwo);
Then get the value in this activity ...
But it is not getting the value from the string.xml file...
text_point = getIntent().getStringExtra("header");
Toast.makeText(getApplicationContext(), text_point, Toast.LENGTH_LONG).show();
But it is toasting blank ....
Please give a solution...
That's because you're trying to retrieve a String, but what you're passing in as extra is actually the resource identifier of it, an int. Either put an actual string as extra, or retrieve an int on the receiving end to fix this.
// put:
ptwo.putExtra("header", R.string.bangla_history_2ndpoint);
// get:
int extraResourceId = getIntent().getIntExtra("header");
text_point = getString(extraResourceId);
Or:
// put:
ptwo.putExtra("header", getString(R.string.bangla_history_2ndpoint));
// get:
text_point = getIntent().getStringExtra("header");
I'm trying to pass 2 variables through a couple of Android Activities. One of them keeps turning up as null on the last page:
The first Activity:
Intent intent= new Intent(RoundOptionActivity.this, MoveOptionActivity.class);
intent.putExtra("numRounds", "5");
startActivity(intent);
The second Activity:
Bundle extras = getIntent().getExtras();
if(extras !=null) {
numRounds = Integer.parseInt(extras.getString("numRounds"));
}
.........
Intent intent = new Intent(MoveOptionActivity.this, MoveActivity.class);
intent.putExtra("numRounds", numRounds);
intent.putExtra("playerChoice", playerChoice);
startActivity(intent);
(Note that at this point I printed numRounds in LogCat and it was set to the right number, and not null)
The Third Activity:
Bundle extras = getIntent().getExtras();
if(extras !=null) {
playerChoice = Integer.parseInt(extras.getString("playerChoice"));
numRounds = Integer.parseInt(extras.getString("numRounds"));
}
At this point, the application crashes at the line where I try to parse numRounds to an integer, with a NumberFormatException complaining that it can't parse a null value. There's never a problem with playerChoice, only numRounds. I've tried handling numRounds the exact same way as playerChoice, but nothing seems to work. What's going on? D:
You have to use extras.getInt("numRounds");
because in second Activity you added to putExtra int value:
numRounds = Integer.parseInt(extras.getString("numRounds"));
use
numRounds = extras.getInt("numRounds");
intead of
numRounds = Integer.parseInt(extras.getString("numRounds"));
because you are passing numRounds as Integer in intent.putExtra("numRounds", numRounds); from second Activity
or pass as if you want to receive as String:
Intent intent = new Intent(MoveOptionActivity.this, MoveActivity.class);
intent.putExtra("numRounds", numRounds+"");
intent.putExtra("playerChoice", playerChoice);
startActivity(intent);
As far as i think in your second activity you are setting numRounds a integer value in putExtra() i.e the integer variable numRounds thats why it causing problem. either get the numRounds in third activity as directly like extras.getInt("numRounds") or send value as String in second activity i.eintent.putExtra("numRounds", numRounds+"");