如何使用从Firebase存储恢复的图像填充Gridview [英] How To Populate A Gridview With Images Retreieved From Firebase Storage

查看:84
本文介绍了如何使用从Firebase存储恢复的图像填充Gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在发布此帖子之前已经审查了其他类似的帖子,我的与众不同 我目前正在从Firestore数据库中检索下载网址的列表,然后尝试从Firebase存储中下载这些图像以在gridview中显示它们.

I have reviewed other similar posts before posting this, mine is different I am current retrieving a list of download urls from my Firestore data base, then trying to download those images from my firebase storage to display them in a gridview.

到目前为止,这是我的代码:

This is my code so far:

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

                    for(QueryDocumentSnapshot documentSnapshot: queryDocumentSnapshots){

                        for(int i = 0; i < queryDocumentSnapshots.size(); i++) {
                            sentPicsURLS.add(documentSnapshot.get("image").toString());

                            if(i == (queryDocumentSnapshots.size()-1)){
                                //now download the images and place them into the proper view
                                for(int z = 0; z < sentPicsURLS.size(); z++){


                                }

                            }
                        }

                    }
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {

                }
            });

这是将图像拉入并推入gridview的位置:

This is where the images should be pulled and pushed into a gridview:

for(int z = 0; z < sentPicsURLS.size(); z++){
         //right here    
}

但是我在创建可以处理此问题的适配器时遇到了麻烦.活动中有一个有效的gridview,并且有一个布局文件,其中包含带有ID的imageview.

But I am having trouble creating an adapter that can handle this. I have a valid gridview in the activity and I have a layout file that contains an imageview with a ID.

final ArrayAdapter arrayAdapter = new ArrayAdapter(Chatroom.this, R.layout.chatroom_sent_images,R.id.sent_iv);
    sentPics.setAdapter(arrayAdapter);
    sentPics.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //empty for now
        }
    });

我实际上想遍历sentPicsURLS然后将它们添加到适配器中的部分(似乎是),也许是在//right here for循环内添加了arrayAdapter.addAll(sentPicsURLS);之类的东西?

The part I am missing (seems to be) where I actually loop through sentPicsURLS and then add them to the adapter... maybe with something like arrayAdapter.addAll(sentPicsURLS); inside the //right here for loop?

现在,gridview显示为空,甚至R.layout.chatroom_sent_images中都不包含默认图像视图.我觉得我是如此亲密,我想念什么?谢谢!

Right now the gridview is showing empty without even the default image view included in R.layout.chatroom_sent_images. I feel like I am so close, what am I missing? Thanks!

修改 这是我的聊天室数据库结构,每个聊天室和聊天室消息的结构都相同.

Edit Here is my chatroom database structure, every chatroom and chatroom message is structured the same way.

推荐答案

正如我在您的屏幕快照中所看到的,您的文档只包含一个image.因此,要解决此问题,就不需要额外的内部for循环.要创建这些图像的列表,请使用以下代码行:

As I see in your screenshot, your document hols only a single image. So to solve this, there is no need for an extra inner for-loop. To create a list of those images, please use the following lines of code:

Query chatRoomMsgs = db.collection("chatrooms").document(chatRoomID)
    .collection("Messages").whereEqualTo("sentby", firebaseAuth.getUid());
chatRoomMsgs.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            ArrayList<String> sentPicsURLS = new ArrayList<>();
            for (QueryDocumentSnapshot document : task.getResult()) {
                sentPicsURLS.add(document.getString("image"));
            }
            //Do what you need to do with your list
            Log.d(TAG, "List size: " + sentPicsURLS.size());
        }
    }
});

还请注意,Firebase API是异步的,您只能在回调中使用该列表.现在,您的logcat中的结果将是列表的大小.

Be also aware that Firebase APIs are asynchronous and you can use that list only inside the callback. Now, the result in your logcat will be size of your list.

这篇关于如何使用从Firebase存储恢复的图像填充Gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆