Issue
I working in a Quiz Question.
I have on "Questions.java"
package com.example.luisbalmant.quickquiz_science;
import android.widget.TextView;
/**
* Created by LuisBalmant on 15/07/2017.
*/
public class Questions {
public String mQuestions[] = {
"My question here",
};
}
I'm trying use string language of "strings.xml" on "My question here".
Eg:
<string name="Q1_function_insulin">What is the function of insulin?</string>
I'm trying this:
getString(R.string.Q1_function_insulin),
Can someone help me please?
Solution
You need a Context object in order to perform getString(). Thus, you can refactor your class this way:
public class Questions {
private static final int QUESTIONS[] = {
R.string.text1,
R.string.text2
};
private Context context;
public Questions(Context context) {
this.context = context;
}
public String getString(int index) {
return context.getString(QUESTIONS[index]);
}
}
And then, from your activity:
Questions questions = new Questions(MainActivity.this);
questions.getString(0);
Answered By - azizbekian
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.