繁体   English   中英

如何使用Android中Firebase数据库中的图像填充listview?

[英]How to populate listview with images from Firebase Database in android?

我在Firebase存储器中已将指向图像的链接存储到Firebase数据库中。 我的数据库的JSON结构如下所示:

|
 --- users
 |
 |
    ---- uid1
 |  |
 |   -- uid1
 |  |
 |   -- name
 |  |
 |   -- imageURL
 |  |
 |
 |
  -- uid2

我想做的就是用这张图片填充我的列表视图。 我还需要知道如何用图像填充listview。

我读了一些有关堆栈溢出的答案,但我不明白该如何开始。

如果有人可以提供信息和一些与此相关的代码,我将不胜感激。

编辑 :我能够从数据库中获取imageURL ,我没有被困在那。 我想知道如何用图像填充listview? 我在StackOverflow上看到了一些答案,但是没有一个可以正确解决我的问题。

可以通过从适配器中的Firebase或要设置图像的任何位置获取图像URL来完成。 假设您有要使用的图像列表的示例代码:

FirebaseDatabase.getInstance().getReference().child("users").addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    for(DataSnapshot ds:dataSnapshot.getChildren()){
                        String image = ds.child("image").getValue(String.class);
                        // You can now set your Image with any method you want, Glide Picasso or any other library
//                        GlideApp.with(getContext())
//                                .load(image)
//                                .placeholder(new ColorDrawable(getResources().getColor(R.color.colorWhite)))// you can use any color here
//                                .fitCenter()
//                                .into(image_view);// An ImageView instance
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });

在这里要做的应该是您获取要在ListView中显示的项目的地方。 获取列表后,您可以将它们传递到适配器中。 已注释的GlideApp调用应该在适配器的getView方法中发生

如果您可以使用改造从Firebase中获取图像,那么将图像填充到RecyclerView中将非常容易。 如果您不知道如何进行改造,则可以按照本教程进行。 https://www.raywenderlich.com/4539-android-networking

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM