我在json文件中儲存了圖像名,例如:"Image1.png"。我使 JSONParser 獲得這個圖像名,但是不能把圖像顯示在 ImageView 中。
ImageView imageView = (ImageView)findViewById(R.id.imageViewer);
for (int i = 0; i < posts.length(); i++) {
JSONObject c = posts.getJSONObject(i);
String varImage = c.getString(TAG_IMAGE);
int resId = getResources().getIdentifier(varImage, "drawable" , getPackageName());
imageView.setImageResource(resId);
//code to add data in hash map
}
使用代碼:
ImageView imageView = (ImageView)findViewById(R.id.imageViewer);
for (int i = 0; i < posts.length(); i++) {
JSONObject c = posts.getJSONObject(i);
String varImage = c.getString(TAG_IMAGE).substring(0, c.getString(TAG_IMAGE).lastIndexOf('.');
int resId = getResources().getIdentifier(varImage, "drawable" , getPackageName());
imageView.setImageResource(resId);
//code to add data in hash map
}