我有一个本地json,其中包含一些显示在列表视图中的静态数据。
每个对象都应该有一个图像/徽标,也会显示在列表视图中。该图像将位于本地和可绘制文件夹中。
数据/列表视图与此http://wptrafficanalyzer.in/blog/android-itemclicklistener-for-a-listview-with-images-and-text/
类似在本教程中,所有数据和图像都存储在数组中。我的数据存储在JSON中。
那么我如何"参考"使用JSON中的图像或图像名称,如何在创建列表视图时实际访问图像?
答案 0 :(得分:7)
让我们假设您的drawable
文件夹中有一些图片:
drawable
image_name_1
image_name_2
image_name_3
image_name_4
...
将图像名称放到json:
[
{
"some_field_1": "some_value_1",
"some_field_2": "some_value_2",
"some_field_3": "some_value_3",
...
"image_name": "image_name_1"
},
{
"some_field_1": "some_value_1",
"some_field_2": "some_value_2",
"some_field_3": "some_value_3",
...
"image_name": "image_name_2"
},
{
"some_field_1": "some_value_1",
"some_field_2": "some_value_2",
"some_field_3": "some_value_3",
...
"image_name": "image_name_3"
},
...
]
从JSON获取名称并加载drawab资源:
JSONArray data; // your JSON
Context context; // context
Resources resources = context.getResources();
for (int i = 0; i < data.length(); i++) {
// getting some another JSON field
// get image name from JSON
String imageName = data.getJSONObject(i).getString("image_name");
// get resource id by image name
final int resourceId = resources.getIdentifier(imageName, "drawable", context.getPackageName());
// get drawable by resource id
Drawable drawable = resources.getDrawable(resourceId);
// get bitmap by resource id
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId);
}
<强>更新强>
将图片资源ID放入HashMap
...
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
JSONArray data; // your JSON
Context context; // context
Resources resources = context.getResources();
for (int i = 0; i < data.length(); i++) {
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("txt", "Country : " + countries[i]);
hm.put("cur","Currency : " + currency[i]);
// get image name from JSON
String imageName = data.getJSONObject(i).getString("image_name");
// get resource id by image name
final int resourceId = resources.getIdentifier(imageName, "drawable", context.getPackageName());
hm.put("flag", Integer.toString(resourceId) );
aList.add(hm);
}
答案 1 :(得分:0)
///如果您有模型类,那么这段代码可以帮助您
try {
JSONObject jsonObject = new JSONObject(loadJSONFromAsset());
JSONArray array = jsonObject.getJSONArray("Details");
Resources resources = this.getResources();
for (int i=0;i<array.length();i++){
// Parse the JSON
JSONObject jo_inside = array.getJSONObject(i);
String title = jo_inside.getString("title");
String img = jo_inside.getString("img");
// get resource id by image name
final int resourceId = resources.getIdentifier(img, "drawable", this.getPackageName());
//load Model Class
Model_Home data = new Model_Home(title,resourceId);
indexList.add(data);
}
}catch (JSONException e){
e.printStackTrace();
}
答案 2 :(得分:0)
enter try {
JSONObject jsonObject = new JSONObject(loadJSONFromAsset());
JSONArray array = jsonObject.getJSONArray("Details");
Resources resources = this.getResources();
for (int i=0;i<array.length();i++){
// Parse the JSON
JSONObject jo_inside = array.getJSONObject(i);
String title = jo_inside.getString("title");
String img = jo_inside.getString("img");
// get resource id by image name
final int resourceId = resources.getIdentifier(img, "drawable", this.getPackageName());
//load Model Class
Model_Home data = new Model_Home(title,resourceId);
indexList.add(data);
}
}catch (JSONException e){
e.printStackTrace();
}
在这里编码