1

Can I add ImageView to this list from External URL?

item.put will just add the URL link but it won't display the image at all

It's not duplicate. I just want to know what additional code I have to add to load the image on this list.

try {
    JSONObject object = new JSONObject(string);
    JSONArray offers = object.optJSONArray("data");
    ArrayList<HashMap<String, String>> list = new ArrayList<>();

    HashMap<String, String> item;
    for(int i = 0; i < offers.length(); i++) {
        JSONObject jsonChildNode = offers.getJSONObject(i);
        item = new HashMap<>();
        item.put("L1", jsonChildNode.optString("gate"));
        item.put("L2", jsonChildNode.optString("stack"));
        item.put("L3", jsonChildNode.optString("image"));
        list.add(item);
    }

    sa = new SimpleAdapter(getActivity(), list,
            R.layout.installs_item,
            new String[]{"L1", "L2", "L3"},
            new int[]{R.id.gate, R.id.stack, R.id.image});
    installs.setAdapter(sa);

} catch (JSONException e) {
    e.printStackTrace();
}

installs_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:id="@+id/L1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Head"
        android:textSize="22sp"
        android:textStyle="italic" />

    <TextView
        android:id="@+id/L2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="sub"
        android:textSize="14sp" />
    <TextView
        android:id="@+id/L3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="desc"
        android:textSize="14sp" />
    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="" />
</LinearLayout>
Pankaj Lilan
  • 4,245
  • 1
  • 29
  • 48
Fresco
  • 343
  • 3
  • 13

2 Answers2

0

Create your own adapter that contains imageview in its xml and use glide library(https://github.com/bumptech/glide) to display images in it

Harsh Patel
  • 459
  • 3
  • 11
0

It's recommended using an external lib will reduce the load and image handling process much easier.

Recommended lib: http://square.github.io/picasso

This reuses the image that already has been used and reduces data usage.

Racer
  • 152
  • 1
  • 9
  • How to add it in this loop? `item.put("L3", jsonChildNode.optString("image")); String imageUrl = jsonChildNode.optString("picture"); Picasso.with(getActivity()).load(imageUrl).into(imageView); list.add(item);` – Fresco Dec 14 '17 at 11:45
  • Just add the URL as a list item (String) and while populating the get the URL and use the same code in the array adapter. – Racer Dec 14 '17 at 13:18
  • It would be easier to understand if you could write the code. I'm quite new to this java based android dev – Fresco Dec 14 '17 at 14:16
  • 1 - Needed an array adapter (Custom array adapter) for your code replacing the SimpleAdapter. 2 - In the Adapter file you would be getting the data and replacing it with the array that you get. while doing that u need to get the URL from the Array and add it to the Picasso code. – Racer Dec 15 '17 at 07:01