Hello I have 2000 Questions each with 3 possible answers in my Strings.xml. I want that a random question is displayed in the textview with is answers.
<string name="Frage1">Was versteht man unter defensivem Fahren?</string>
<string name="ersteAntwort1">Nicht auf dem eigenen Recht bestehen</string>
<string name="zweiteAntwort1">Mit Fehlern anderer rechnen</string>
<string name="dritteAntwort1">Vorsorglich an jeder Kreuzung anhalten</string>
<string name="Frage2">Was kann zu Auffahrunfällen führen?</string>
<string name="ersteAntwort2">Unerwartet starkes Bremsen</string>
<string name="zweiteAntwort2">Unaufmerksamkeit</string>
<string name="dritteAntwort2">Zu dichtes Auffahren</string>
<string name="Frage3">Sie fahren innerorts hinter einem Fahrzeug mit ortsfremdem Kennzeichen. Was könnte geschehen?</string>
<string name="ersteAntwort3">- bremst unerwartet</string>
<string name="zweiteAntwort3">- betätigt den Blinker vor dem Abbiegen zu spät</string>
<string name="dritteAntwort3">- hält unerwartet an, um nach dem Weg zu fragen</string>
The names of the Strings are always the same only the number of the question changes so i want to add a random number to the first part of the string name
public void neueFrage (View view) {
Button buttonTipp = (Button)findViewById(R.id.button1);
final TextView tw = (TextView)findViewById(R.id.textView1);
final CheckBox Chk1 = (CheckBox)findViewById(R.id.checkBox1);
final CheckBox Chk2 = (CheckBox)findViewById(R.id.checkBox2);
final CheckBox Chk3 = (CheckBox)findViewById(R.id.checkBox3);
buttonTipp.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
int random = (int) (Math.random() *3 );
String zahl = String.valueOf(random);
String question = "Frage"+zahl;
String firstAnswer ="ersteAntwort"+zahl;
String secondAnswer = "zweiteAntwort"+zahl;
String thirdAnswer = "dritteAntwort"+zahl;
tw.setText(R.string.question);
Chk1.setText(R.string.firstAnswer);
Chk1.setText(R.string.secondAnswer);
Chk1.setText(R.string.thirdAnswer);
}});
}
First of all, if you can, write the data in an appropriate format. XML would be a good choice, you could create your own XML format and read it. An example XML format could be as follows, Android does include some excellent parsers as well.
<questions_list>
<question name="Question 1?">
<answer name="Answer 1"/>
<answer name="Answer 2">
<correct/>
</answer>
<answer name="Answer 3"/>
</question>
</question_list>
If that doesn't work, maybe a String Array could work, like this:
<string-array name="question1">
<item>Question 1</item>
<item>Answer 1</item>
...
</string-array>
A third option would be to put the data into a SQLite database, probably with 5 columns, question, 3 answers, and the correct answer. There's a lot out there on how to do this, the easiest way is to use a language like Python to populate the database, then use something like SQLiteAssetHelper to put the database in to your program.
I think re-formatting will save you considerable work. XML, a database, or something else, but I'm sure a better format would help you considerably.
As it stands, you would have to reference not only the questions directly in the code, but also the answers. The tool simply isn't meant to be used the way you're using it.
Related
I try to make a simple app and I used two strings in strings.xml:
<string name="question">is the capital of:</string>
<string name="riga">Riga</string>
And I want to add them to one text view.
I want the output to be (Riga is the capital of: ).
How can I do this?
This is my layout:
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/riga" + "#string/question"
/>
You cannot do this in XML. You could do it in Java, however. Assuming you're inside an Activity:
String riga = getString(R.string.riga);
String question = getString(R.string.question);
TextView tv = findViewById(/* your id here */);
tv.setText(riga + question);
Even better would be to use format arguments in one of your strings:
<string name="question">%1$s is the capital of:</string>
And then do this in your activity:
String riga = getString(R.string.riga);
String fullQuestion = getString(R.string.question, riga);
TextView tv = findViewById(/* your id here */);
tv.setText(fullQuestion);
I am showing Random text in the two textview's on button click. Now I have to show the explaination of the text showed in textview1 in textview2.But I am not able to get it. Help will be appreciated.
I put the text in string.xml
<string name="one">1</string>
<string name="two">2</string>
<string name="three">3</string>
<string name="four">4</string>
<string name="five">5</string>
<string name="six">6</string>
<string name="seven">7</string>
<string name="eight">8</string>
<string name="nine">9</string>
<string name="ten">10</string>
<string name="one_explaination">This is number one</string>
<string name="two_explaination">This is number two</string>
<string name="three_explaination">This is number three</string>
<string name="four_explaination">This is number four</string>
<string name="five_explaination">This is number five</string>
<string name="six_explaination">This is number six</string>
<string name="seven_explaination">This is number seven</string>
<string name="eight_explaination">This is number eight</string>
<string name="nine_explaination">This is number nine</string>
<string name="ten_explaination">This is number ten</string>
My code in MainActivity:
Random number,number_explaination;
int [] array_number,array_number_explaination;
int textview_number,textview_number_explaination;
TextView textView1,textView2;
number = new Random();
array_number = new int[] {R.string.one,R.string.two,R.string.three,R.string.four,R.string.five,R.string.six,R.string.seven,
R.string.eight,R.string.nine,R.string.ten};
textview_number = number.nextInt(array_number.length - 1);
textView1.setText(array_number[textview_number]);
number_explaination = new Random();
array_number_explaination = new int[] {R.string.one_explaination,R.string.two_explaination,R.string.three_explaination,
R.string.four_explaination,R.string.five_explaination,R.string.six_explaination,R.string.seven_explaination,
R.string.eight_explaination,R.string.nine_explaination,R.string.ten_explaination};
textview_number_explaination = number_explaination.nextInt(array_number_explaination.length - 1);
textView2.setText(array_number_explaination[textview_number_explaination]);
What I want here if I get Random R.string.two in my textview1 and then I will get R.string.two_explaination and so on.How can I achive this. Sorry for my bad english.
try this
Random number,number_explaination;
int [] array_number,array_number_explaination;
int textview_number,textview_number_explaination;
TextView textView1,textView2;
number = new Random();
array_number = new int[] {R.string.one,R.string.two,R.string.three,R.string.four,R.string.five,R.string.six,R.string.seven,
R.string.eight,R.string.nine,R.string.ten};
array_number_explaination = new int[] {R.string.one_explaination,R.string.two_explaination,R.string.three_explaination,
R.string.four_explaination,R.string.five_explaination,R.string.six_explaination,R.string.seven_explaination,
R.string.eight_explaination,R.string.nine_explaination,R.string.ten_explaination};
textview_number = number.nextInt(array_number.length - 1);
textView1.setText(getResources().getString(array_number[textview_number]));
textView2.setText(getResources().getString(array_number_explaination[textview_number]));
You do not need to generate another random number for explanation text
Try this:
textView2.setText(getString(array_number_explaination[textview_number_explaination]));
getString(int) should be used to get a string in your string resource file.
you need to use string array in the strings file :
<string-array
name="string_array_name">
<item
>text_string</item>
</string-array>
and then make an array in your code with reference of :
R.array.string_array_name
see http://developer.android.com/guide/topics/resources/string-resource.html for reference.
after you got the two arrays in your code you can run loops and do your desired work
I have a problem with language encoding... what i try is to incorporate a random motivational string into my app which contains German unicode letters... as far as I know, Java uses Unicode-16, but the respective letters dont show up at all when I start the app. I'm using Android Studio and the app is tested on a real device.
public class Start extends ActionBarActivity {
//This is a string array containing quotes
String[] motivational = {"Unser größter Ruhm ist nicht, niemals zu fallen, sondern jedes Mal wieder aufzustehen.\nRalph Waldo Emerson"};
public int randInt() {
Random rand = new Random();
return rand.nextInt((motivational.length) + 1);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
//Takes a random quote from the list and sets it as a TextViews content
TextView motivator = (TextView) findViewById(R.id.motivator);
motivator.setText(motivational[randInt()]);
}
//rest of the class
Add your text to strings.xml and then use Android's getResources.getString() method to get the text
<resources>
<string name="motivational">Unser größter Ruhm ist nicht, niemals zu fallen, sondern jedes Mal wieder aufzustehen.\nRalph Waldo Emerson</string>
</resources>
Then in your java file, use
String motivational = getResources().getString(R.string.motivational);
TextView motivator = (TextView) findViewById(R.id.motivator);
motivator.setText(motivational);
Retrieving the value from strings.xml is done using
getResources().getString(R.string.motivational)
where R.string.motivational is the unique string identifier.
Isn't this the output you are looking for?
I'm writing an app in which I want to display a string, describing an object in my model, to the user. I've started thinking about localization and I'm a bit stumped as to the best approach to deal with the difference in grammar between languages.
Imagine if in my model I have a verb an object and a quantity (pseudocode):
int _quantity = 6;
String _object = "#string/object";
String _verb = "#string/verb";
This should render in English as _verb + _quantity + _object (i.e. "eat 6 eggs", whereas in German it should render as _quantity + _object + _verb (i.e. "6 eggs eat" (but, obviously, using German word tokens rather than English :) ))
Is there a standard approach to this type of problem in Android?
Thanks.
OK, I've come up with a solution which, based on what I know, is the best I can find. If anyone has a better solution, please post!
1) In strings.xml, define any bits of text needed (for me, these equated to the localised names for enumeration members and for the string used to build natural language strings (more on this in a bit))
Example:
<resources>
...
<string name="verb_ate">ate</string>
<string name="verb_threw">threw</string>
...
<string name="modelobject_naturallanguagedescription">I {0} {1} {2}</string>
...
</resources>
2) In any enumerations, decorate them with the resource ids of the strings they are to use for display
Example:
public enum VerbType
Ate(R.string.verb_ate),
Threw(R.string.verb_threw);
private final int _stringID;
private VerbType(int stringID) {
_stringID = stringID;
}
public int getStringID() {
return _stringID;
}
}
3) Make a helper function which will take the model object and the context and will piece together the required natural language string (using java.text.MessageFormat), plus a helper function for each enumeration:
Example (assume that ModelObject has _verb, _quantity, _object and relevant getters):
public static String getVerb(VerbType v, Context c) {
return c.getResources().getString(v.getStringID());
}
public static String getNaturalLanguageString(ModelObject o, Context c) {
MessageFormat mf = new MessageFormat(c.getResources().getString(R.string.modelobject_naturallanguagedescription);
return mf.format(new Object[] {getVerb(o.getVerb(),c), o.getQuantity(), o.getObject()});
}
So, having done all of this, it's easy enough to define another strings file for a different language (German for example (I don't know German, so the language won't be correct)):
<resources>
...
<string name="verb_ate">gegessen</string>
<string name="verb_threw">gethrown</string>
...
<string name="modelobject_naturallanguagedescription">Ich haben {1} {2} {0}</string>
...
</resources>
So, in an english locale getNaturalLanguageString might return:
I ate 6 eggs
whereas in a German locale it might return:
Ich haben 6 eggs gegessen
I'd like to format an array of strings just like android used to format strings:
Usually we do:
strings.xml
<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>
In some java code:
Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);
I'm looking for something like:
in some arbitrary xml:
<string-array name="employee">
<item>name: %1$s</item>
<item>post: %2$s</item>
</string-array>
in some java code:
Resources res = getResources();
String[] employee = ArrayString.format(res.getStringArray(R.string.employee), name, post);
Is there an elegant way to do that?
EDIT:
The next pieces of code is a workaround and I'm posting it just to help #Sufian, who asked for it in a comment. It's not a real answer once my question is about format the string array's content and the bellow code is formatting each string separately.
In some misc.xml:
<string-array
name="string_array">
<item>1st position: %1$d</item>
<item>2nd position: %1$d</item>
</string-array>
Then, in java code:
res = getResources();
String[] sa = res.getStringArray(R.array.string_array);
for (int i = 0; i < sa.length; i++ ) {
text += String.format(sa[i], i);
}
Just use:
String text = String.format(res.getStringArray(R.array.myStringArray)[index], param1, param2);
getQuantityString may solve your problem.
Look at quantity strings in http://developer.android.com/guide/topics/resources/string-resource.html
Here's the specific API doc:
http://developer.android.com/reference/android/content/res/Resources.html#getQuantityString(int,%20int,%20java.lang.Object...)