无法在列表视图阵列适配器中设置图像Url



我正在使用API获取图像列表,无法在ListView array Adapter中设置Image数组。图像的格式为字符串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);

你好,你应该使用更新的android api.你在太旧的时代

以下是如何更新您的项目

  1. 首先使用RecycelView而不是listview
  2. 要从服务器api获取数据,请使用Reform而不是原始服务器api打电话。太容易了
  3. 然后使用recyclerview适配器来显示数据。这既简单又干净

最新更新