我有一個國家(字符串) - (來自web服務)以及相應的可繪製的列表是他們的標誌 - (在drawables文件夾中)如何基於文件名編程設置Drawable
Drawables被標記爲「cape_verde」或「canada」,但當網絡服務的名稱下降時,它們會以「佛得角」或「加拿大」的形式出現。
當我打電話的web服務我做了小寫轉換和替換某些字符如下面的代碼所示:
String lowerCaseName = countryNameInArray.countryName.toLowerCase();
String replacedCountryName = lowerCaseName.replaceAll(" ", "_");
我的想法是,我可以將某些設置如何
R.drawable.replacedCountryName
但顯然它不能採取字符串作爲它的要求int。
編輯:
public View getCustomView(int position, View convertView, ViewGroup parent) {
countries = res.getStringArray(R.array.countries);
/********** Inflate spinner_rows.xml file for each row (Defined below) ************/
View row = inflater.inflate(R.layout.country_drop_down_rows, parent, false);
/***** Get each Model object from Arraylist ********/
tempValues = null;
tempValues = (countryDropDownItem) data.get(position);
TextView label = (TextView) row.findViewById(R.id.spinner_country_textview);
ImageView country = (ImageView) row.findViewById(R.id.spinner_county_flag_imageview);
if (position == 0) {
// Default selected Spinner item
label.setText("South Africa"); //get the country text
country.setImageResource(R.drawable.south_africa);
} else {
String lowerCaseName = countries.toString().toLowerCase();
String replacedCountryName = lowerCaseName.replaceAll(" ", "_");
// Set values for spinner each row
label.setText(tempValues.countryName); //get the country text
country.setImageResource(res.getIdentifier(replacedCountryName, "drawable", "com.app.app"));
}
return row;
}
有沒有人有一個想法,我怎麼去解決這個?
感謝
感謝磨存儲,我已經嘗試過,但由於某種原因沒有設置圖像。我已經添加了我使用的問題,因爲它是在我的應用程序中...它用於微調,它不設置圖像,但名稱工作正常 – x10sion
好吧,非常感謝 – Androider