我一直在尋找一段時間,找不到解決方案,我的問題。我試圖根據用戶登錄設置imageView。主要問題是,如何將R.drawable.stock
重命名爲R.drawable.user1
,其中imageView的名稱因用戶的名稱而異。我試圖將其設置爲像String temp="R.drawable."+userNameStore;
這樣的字符串,但沒有運氣。根據情況設置ImageView
下面是我在做什麼:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile);
SharedPreferences app_preferences =
PreferenceManager.getDefaultSharedPreferences(this);
String userNameStore =
app_preferences.getString("userNameStore", null);
String temp="R.drawable."+userNameStore;
TextView textName=(TextView) findViewById(R.id.username);
textName.setText("Username: "+ userNameStore);
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageResource(temp);
}
謝謝,這工作完美 – user1074039