如何使用Glide加载图像并将图像添加到android arrayadapter

问题描述 投票:0回答:1

我正在尝试使用Glide存储库加载图像,然后最终将该图像添加到gridview中。我目前正在尝试通过创建空白imageview,加载图像,将图像添加到imageview,然后将imageview添加到我的阵列适配器来做到这一点。我与arrayadapter一起使用的布局包含单个imageview。这是我的代码,布局和尝试加载图像时遇到的错误

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


                    }
                }
            });

我的数组适配器:

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

chatroom_sent_images布局文件

  <?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>

然后是应用尝试加载图像时出现的奇怪错误:

    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

我不确定在任何地方都提到一个testView吗?有任何想法吗?谢谢!

android android-arrayadapter android-glide android-gridview
1个回答
0
投票

尝试一下:

      ImageView tempimage = new ImageView(getActivity().getApplicationContext());

代替此:

       ImageView tempimage = new ImageView(getApplicationContext());
© www.soinside.com 2019 - 2024. All rights reserved.