How to load and add images to an android arrayadapter using Glide

喜欢而已 提交于 2020-03-25 13:51:33

问题


I am trying to load an image using the Glide repository, then ultimately add that image into a gridview. I am currently trying to do this by creating a blank imageview, loading the image, adding the image to the imageview and then adding the imageview to my array adapter. The layout I am using with the arrayadapter contains a single imageview. Here is my code, layout, and error I am getting when I try to load the images

final Query chatRoomMsgs = db.collection("chatrooms").document(chatRoomID).collection("Messages").whereEqualTo("sentby", UID);
    chatRoomMsgs.get()
            .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                @Override
                public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                    //ArrayList<ImageView> sentPicsURLS = new ArrayList<>();

                    for(QueryDocumentSnapshot documentSnapshot: queryDocumentSnapshots){

                        for(int i = 0; i < queryDocumentSnapshots.size(); i++) {

                             //sentPicsURLS.add(documentSnapshot.getString("image"));
                            ImageView tempimage = new ImageView(getApplicationContext());
                            Glide.with(Chatroom.this).load("gs://testapp-72419.appspot.com" + documentSnapshot.getString("image")).into(tempimage);
                            arrayAdapter.add(tempimage);
                            arrayAdapter.notifyDataSetChanged();

                        }
                        //Do what you need with your list


                    }
                }
            });

My arrayadapter:

final ArrayAdapter arrayAdapter = new ArrayAdapter(Chatroom.this, R.layout.chatroom_sent_images,R.id.sent_iv);
    sentPics.setAdapter(arrayAdapter);

chatroom_sent_images layout file

  <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#B84CD5">

    <ImageView
        android:id="@+id/sent_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="21dp"
        android:layout_marginLeft="21dp"
        android:layout_marginTop="24dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@android:mipmap/sym_def_app_icon" />
</androidx.constraintlayout.widget.ConstraintLayout>

And then the strange error I am getting when the app tries to load the images:

    java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
Caused by: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView cannot be cast to android.widget.TextView

I'm not sure there's a single testView mentioned anywhere? Any ideas? Thanks!


回答1:


Try this as you need to define tempimage one time and u do for loop to change its content:

     ImageView tempimage = new ImageView(getApplicationContext());

   final Query chatRoomMsgs = db.collection("chatrooms").document(chatRoomID).collection("Messages").whereEqualTo("sentby", UID);
chatRoomMsgs.get()
        .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
            @Override
            public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                //ArrayList<ImageView> sentPicsURLS = new ArrayList<>();

                for(QueryDocumentSnapshot documentSnapshot: queryDocumentSnapshots){

                    for(int i = 0; i < queryDocumentSnapshots.size(); i++) {

                         //sentPicsURLS.add(documentSnapshot.getString("image"));

                        Glide.with(Chatroom.this).load("gs://testapp-72419.appspot.com" + documentSnapshot.getString("image")).into(tempimage);
                        arrayAdapter.add(tempimage);
                        arrayAdapter.notifyDataSetChanged();

                    }
                    //Do what you need with your list


                }
            }
        });


来源:https://stackoverflow.com/questions/60840789/how-to-load-and-add-images-to-an-android-arrayadapter-using-glide

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!