【问题标题】:Unable to set the Image Url in listview array adapter无法在 listview 数组适配器中设置图像 URL
【发布时间】:2020-07-09 22:47:56
【问题描述】:

我正在使用 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>

【问题讨论】:

    标签: android android-studio listview android-arrayadapter


    【解决方案1】:

    我会建议使用 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);
    

    【讨论】:

    • 您好,感谢您的回复,我也尝试过使用毕加索,但我无法在 Listview 中使用阵列适配器发送图像。看到这个ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(MainActivity.this, R.layout.image_layout,im); listView.setAdapter(adapter);
    • 基本上你应该实现自定义适配器 abhiandroid.com/ui/listview 这是如何为列表视图构建自定义适配器的示例,但无论如何我建议将 ListView 更改为 RecyclerView developer.android.com/guide/topics/ui/layout/recyclerview
    • 感谢关于 Listview 和适配器的教程,,,,,我的要求很简单,这就是我不想使用回收站视图的原因,我只是在寻找如何存储字符串 url 的方法图像到一个数组 ,,,,,.使用位图或毕加索.....你能告诉我如何使用毕加索将图像存储在有效的数组对象中吗?否则使用回收站视图太好了,非常感谢您对此的关注。
    【解决方案2】:

    你好,你应该使用更新的android api。你太旧了

    这里是如何更新你的项目

    1. 首先使用 RecycelrView 代替 listview
    2. 要从服务器 api 获取数据,请使用 Retrofit 而不是原始服务器 api call.it 该死的容易
    3. 然后使用 recyclerview 适配器显示数据。它简单而干净。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-31
      • 2016-06-22
      • 2014-11-09
      • 1970-01-01
      相关资源
      最近更新 更多