Question:
I'm creating a quiz app, the link is https://github.com/luisbalmant/QuickQuiz-Science .
I'm trying to use getString(R.string.nome)
to migrate strings from java class and then be able to use 2 languages.
In other places I managed to pull the strings and it's working showing the texts when I run the app on my cell phone, but the moment I try to use the string in the questions the app just doesn't open anymore.
public class Questions {
public String mQuestions[] = {
// Funciona
"Pergunta número 1 xxxxxxx",
// NÃO FUNCIONA
getString(R.string.Q1_function_insulin),
};
==/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=//=/=/=/=/=/=/= /=
Update:
I made the following changes and it didn't work:
My MainActivity.java:
public class MainActivity extends AppCompatActivity {
Button answer1, answer2, answer3;
TextView score, question;
private Questions mQuestions;
private String mAnswer;
private int mScore = 0;
private int mQuestionsLength;
Random r;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mQuestions = new Questions(this);
mQuestionsLength = mQuestions.mQuestions.length;
r = new Random();
My other Questions.java file
import android.content.Context;
public class Questions extends MainActivity {
Context context;
public Questions(Context context)
{
this.context = context;
}
public String mQuestions[] = {
"Pergunta número 1 xxxxxxx",
context.getString(R.string.Q1_function_insulin),
"Outra Pergunta número 2",
};
Answer:
Try it this way:
public class Questions {
private Context context;
private Integer[] mQuestions = new Integer[]{R.string.Q1_function_insulin, R.string.Q2_any_name};
public Questions(Context context) {
this.context = context;
}
public String get(int q_number) {
String question = null;
try {
question = context.getString(mQuestions[q_number - 1]);
} catch (IndexOutOfBoundsException e) {
Log.e("Question", "Invalid question number: " + q_number);
}
return question;
}
}
Em OnCreate declare a classe Questions:
Questions mQuestions = new Questions(this);
Get the questions:
String question1 = mQuestions.get(1); //obtém a questão 1 e assim por diante...