android - 无法在 listview 数组适配器中设置图像 URL
问题描述
我正在使用 API 获取图像列表,但无法在 ListView 数组适配器中设置图像数组。图像采用字符串 URL 形式。请参阅下面的代码我尝试过的内容。
private void jsonParse() {
String url = "This is the API URL";
JsonObjectRequest objectRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject photos = response.getJSONObject("photos"); //photos
JSONArray jsonArray = photos.getJSONArray("photo"); //photos.photo[0]
ImageView[] im = new ImageView[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject pics = jsonArray.getJSONObject(i);
String id = pics.getString("id");
String server = pics.getString("server");
String farm = pics.getString("farm");
String secret = pics.getString("secret");
String URLs = "https://farm" + farm + ".staticflickr.com/" + server + "/" + id + "_" + secret + ".jpg";
System.out.println(URLs);
// Use Bitmap for converting the String form into Image Format
try {
Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(URLs).getContent());
imageView.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
im[i] = imageView;
}
//Below, 'im' is not readable by the ArrayAdapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.image_layout,im);
listView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
requestQueue.add(objectRequest);
}
ImageView[] 'im' 数组无法被 ArrayAdapter 读取。谁能帮我解决这个问题?
image_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image_id"
android:contentDescription="DefaultImage"
android:src="@android:drawable/btn_star_big_on"/>
</LinearLayout>
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#ffffff">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="430px"
android:text="Click" />
</RelativeLayout>
<ListView
android:padding="50px"
android:id="@+id/list_view_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
tools:visibility="visible" />
</LinearLayout>
解决方案
我会建议使用 Glide 或 Picasso lib 从互联网设置图像
https://github.com/bumptech/glide
Glide.with(this).load(url).into(imageView);
https://square.github.io/picasso/
Picasso.get().load(url).into(imageView);
推荐阅读
- python - 列表是否在 python 'for' 循环的每次迭代中实例化?
- python - 在散点图中可视化文本类
- powershell - PowerShell如何为外部文件中的每个条目运行一次命令
- tensorflow2.0 - 在 Windows 上使用 tensorflow-text 的解决方案(或解决方法)
- jms - StatisticsPlugin 中“大小”的值始终为 0
- continuous-integration - Helm Chart 版本
- python - 在python的同一行中使用if else for和del?
- python - 将现有的 numpy 直方图转换为提升直方图
- r - R:合并两个数据帧而不增加行
- linux - 获取 [: !=: shell 脚本中预期的一元运算符