Issue
I've created a method to get a String from strings.xml
with SharedPreferences
and display it in a TextView
. There is no error report but the TextView
shows nothing. Is there a problem with SharedPreferences
? The Strings and TextView
are correct.
public void setQuestion() {
TextView Question = (TextView) findViewById(R.id.question);
if (question == 0) {
SharedPreferences sharedPreferences =
getSharedPreferences("strings", Context.MODE_PRIVATE);
String myquestion = sharedPreferences.getString("AppQuestion1", "");
Question.setText(myquestion);
}
}
Solution
The strings.xml
and SharedPreferences
are different things.
If your AppQuestion1
is defined in the strings.xml
like below:
<string name="AppQuestion1">Question1: ...</string>
You can get the String by calling getString
method on your Resources
object.
String myquestion = getResources().getString(R.string.AppQuestion1);
And there is no need to use SharedPreferences
.
Answered By - hata
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.