Reputation: 85
I'm making a quiz game and for each level there is a textview for the question and a text field for you to type your answer. There's going to be a lot of levels, so instead of having tons and tons of Java classes and XML files I was wondering if there is a quicker solution to this?
I was thinking of having one Java class that populates the same TextView with different strings for different levels. Is this possible? I think I may have to use a database?
The code that I have at the moment is something like this (XML):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="fill_horizontal"
android:layout_margin="25dp"
android:orientation="vertical" >
<TextView
android:id="@+id/tutorialWelcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="@string/tutorial_step_four_a"
android:textSize="@dimen/text_size"
android:textStyle="bold" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/enterButton"
android:hint="@string/answerhere"
android:inputType="text"
android:textSize="@dimen/text_size" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tutorialWelcome"
android:layout_centerHorizontal="true"
android:text="@string/tutorial_step_four_b"
android:textSize="@dimen/text_size" />
<ImageButton
android:id="@+id/enterButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:contentDescription="@string/nextbutton"
android:src="@drawable/ic_action_send_now"
style="?android:attr/borderlessButtonStyle"/>
Any help would be grateful!
Upvotes: 2
Views: 1298
Reputation: 8598
There are few options:
store your questions in strings.xml, then use getString(R.string.id_of_question_you_want) to fetch the question and later display it
store the questions in SQLite database
store the questions in a file, and you will need to write functions to read the file
EDIT:
You can also use string array in xml, and then based on which questin is currently being displayed, populate from the array, eg:
have your quesitons.xml where you define your array of questions:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="questions_array">
<item>Question 1 content</item>
<item>Question 2 content</item>
<item>Question 3 content</item>
<item>Question 4 content</item>
</string-array>
</resources>
in your activity, you can get these questions in an array:
Resources res = getResources();
String[] questions = res.getStringArray(R.array.questions_array);
then, keep track of which question should be displayed now, and get the string from array in your activity that populates the layout:
int questionNumber = 5;
textViewQuestion.setText(questions[questionNumber]);
Just remember to check for boundaries, otherwise you'll get an exception if your questionNumber is higher than the number of questions in an array. Remember that arrays in Java start from index 0.
and probably some other...
Check the docs for info about storing data.
Upvotes: 1