如何在ListView中添加图像URL?

时间:2017-12-14 09:13:36

标签: android android-layout

我可以从外部网址添加ImageView到此列表吗?

item.put只会添加网址链接,但不会显示图片

这不重复。我只是想知道我要添加哪些附加代码来加载此列表中的图像。

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>

2 个答案:

答案 0 :(得分:0)

在xml中创建自己的包含imageview的适配器,并使用滑动库(https://github.com/bumptech/glide)在其中显示图像

答案 1 :(得分:0)

建议使用外部lib将更容易减少负载和图像处理过程。

推荐的lib:http://square.github.io/picasso

这会重复使用已经使用的图像并减少数据使用量。