Issue
Hi i have some data in values that structured in this way every time i generate random number and i want to show values of that number from values what is the solution
<string name="info47">hello world</string>
<string name="info48">hello dolly</string>
int ok=47;
infotextView.setText(getString(R.string.info{ok}));
Solution
Try this code:
If in activity like MainActivity you can use this.
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int ok=47;
String name="info"+ok;
TextView infotextView= (TextView) findViewById(R.id.textView);
infotextView.setText(getStringByName(this,name));
}
public static String getStringByName(Context context, String name) {
Resources res = context.getResources();
return res.getString(res.getIdentifier(name, "string", context.getPackageName()));
}
Answered By - Ahmad Aghazadeh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.