local image through local json in android
Asked Answered
V

3

7

I have a local json with some static data that is displayed in a listview.

Each object should have an image/logo that is also shown in the listview. The image will be local and in the drawable folder.

The data/listview is similar to this http://wptrafficanalyzer.in/blog/android-itemclicklistener-for-a-listview-with-images-and-text/

In this tutorial all the data and images are stored in arrays. My data is stored in JSON.

So how do I "reference" to an image or image name in JSON and how do I actually access the image when creating the listview?

Vladikavkaz answered 28/5, 2014 at 22:33 Comment(4)
You'll need to convert the JSON data using a JSON parser or something like gson to convert it to java objects, and then go from there.Crary
I have already done this. My question is how I should store and parse the correct image?Vladikavkaz
If the images are in the drawable folder, then you'll need a way to map between the data objects and the drawable resources, e.g. with a switch statement. If the image is part of the JSON object, you may want to look into Base64 encoding/decoding the images.Crary
Why are you doing double work. if your data is local then use array. you are making json and again you are extract value to array. for knowledge purpose it is good for the practice.Audette
A
8

Let's imagine that you have some images in your drawable folder:

drawable
    image_name_1
    image_name_2
    image_name_3
    image_name_4
    ...

Put the name of image to 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"
    },
    ...
]

Get name from JSON and load drawab resource:

    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);
    }

Update

Put image resource id into 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);
    }
Allysonalma answered 28/5, 2014 at 23:16 Comment(1)
How do I pass the image to the hashmap and the listview? Please look at this link: wptrafficanalyzer.in/blog/… I have basically the same setup for creating the listview. Thanks!Vladikavkaz
A
0

//if you have a model class then this code help you

 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();
    }
Agonist answered 22/2, 2019 at 4:0 Comment(0)
G
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();
}

code here

Gambado answered 16/6, 2020 at 5:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.