【发布时间】:2018-03-07 20:46:33
【问题描述】:
试图自学如何做这些事情。我试图弄清楚如何将图像动态添加到 ListView 项。我正在做的是提取 JSON 数据,返回以下内容:
{"mdslist":
[
{
"UID":"3",
"name":"Stamford, CT",
"date":"March 10, 2018",
"icon":"badge_stamford"
},
{
"UID":"4",
"name":"Enschede, The Netherlands",
"date":"March 11, 2018",
"icon":"badge_enschede"
}
]
}
然后处理该数据并将其转储到 ListView 中。 ListView 有单独的容器来保存数据 - 请注意 ImageView 标记中的临时 img:
md_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/md_item_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:minHeight="65dp">
<ImageView
android:id="@+id/md_icon"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginBottom="4dp"
android:layout_marginTop="4dp"
android:contentDescription="@string/md_logo"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/badge_stamford" />
<TextView
android:id="@+id/md_name"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:paddingBottom="2dip"
android:paddingTop="6dip"
android:textColor="@android:color/black"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/md_icon"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/md_date"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_marginBottom="4dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:paddingBottom="2dip"
android:textColor="@color/dark_gray"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/md_icon"
app:layout_constraintTop_toBottomOf="@+id/md_name" />
</android.support.constraint.ConstraintLayout>
JSON 数据在处理后的 onPostExecute() 调用期间被注入:
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (pd.isShowing()) {
pd.dismiss();
}
/**
* Dump parsed JSON data into ListView
**/
ListAdapter adapter = new SimpleAdapter(
md_list_all.this, mdList,
R.layout.md_list_item, new String[]{"name", "date"}, new int[]{R.id.md_name, R.id.md_date});
lv.setAdapter(adapter);
}
如您所见,我无法添加图标数据。这些文件被命名为 JSON 数据,扩展名为 .jpg。我想要的是获取 JSON 的“图标”信息,并将其注入 ImageView。
【问题讨论】:
-
如果这些图标在您的服务器端存储中,则使用图标名称附加图标的根 url 路径,并使用 GLIDE 或 Picasso 将 URL 加载到 imageView 中!
-
他们不是。他们正在当地被拉进来。尽管您确实让我想知道我是否应该将它们放在服务器端并保持应用程序轻便。
-
如果您将它们放在客户端,您将无法在不更新 Playstore 中的应用的情况下进行更新。我的建议就像@Rizwanatta 所说的那样。当您从服务器返回 JSON 时,请提供图标的 url。将路径存储在列表视图中。加载图像时,使用滑行(您无需担心缓存,它会为后续调用处理它)。如果您的过程花费太多时间,您可以在加载图标之前显示微调器。
-
感谢各位的建议。我将研究 glide 以及它是如何完成的。
标签: android android-layout listview android-imageview