Issue
I have a model and it has a specific country flag:
public int getCountryFlag() {
return countryFlag;
}
public void setCountryFlag(int countryFlag) {
this.countryFlag = countryFlag;
}
I am getting Data from an API. The API has a value country code and this could be like: "country":"de","created_at":"2018-08-08 09:17:43","updated_at":"2018-08-08 09:17:43"
I have the flags named as the country code:
de
se
us
....
How can I get the exact resourceID of the image to relate it to the model? I am using RecyclerView.
I tried something like:
int resID = getResources().getIdentifier(object.getString("country"), "drawable", getPackageName());
model.setCountryFlag(R.drawable.sy);
Though that didn't work!
Solution
If you're sure about the format of the api
string then do the following:
String country = api.trim().split(",")[0].split(":")[1].replaceAll("\"", "");
or
String country = api.trim().substring(11, 13);
and then:
int resID = getResources().getIdentifier(country, "drawable", getPackageName());
model.setCountryFlag(resID);
Answered By - user8959091
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.