Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

local image through local json in android

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?

like image 622
user3658609 Avatar asked May 28 '14 22:05

user3658609


1 Answers

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);
    }
like image 187
Igor Tyulkanov Avatar answered Oct 06 '22 00:10

Igor Tyulkanov