【问题标题】:Displaying images from firebase to my android application in recyclerview在 recyclerview 中将图像从 firebase 显示到我的 android 应用程序
【发布时间】:2017-12-09 03:17:06
【问题描述】:

Firebase Database----------

我一直在尝试使用 recyclerview 在 android 应用程序中显示存储在 firebase 中的所有图像。有没有办法这样做? 我上传了要在 firebase 存储中显示的照片及其在 firebase 数据库中的下载 URL。每个图像都有一个主 ID。我不知道如何访问所有主 ID 的所有子元素。

请帮我解决这个问题。

【问题讨论】:

  • 我看到了下载地址。你为什么不使用它?

标签: android firebase firebase-realtime-database firebase-storage


【解决方案1】:

如果使用片段,你可以在下面的 on create 方法中执行我喜欢的操作

@Override
    public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        databaseReference = FirebaseDatabase.getInstance().getReference().child("users");
        databaseReference.keepSynced(true);

        firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<AllUsers, AllUsersViewHolder>
                (AllUsers.class, R.layout.all_users_display_layout, AllUsersViewHolder.class, databaseReference) {
            @Override
            protected void populateViewHolder(AllUsersViewHolder viewHolder, AllUsers model, final int position) {

                viewHolder.setThumbImage(getContext(), model.getThumbImage());

            }
        };
        all_user_list.setAdapter(firebaseRecyclerAdapter);
    }

并创建一个从RecyclerView.ViewHolder 延伸的static class,如下所示

public static class AllUsersViewHolder extends RecyclerView.ViewHolder {

        View view;

        public AllUsersViewHolder(View itemView) {
            super(itemView);
            view = itemView;
        }

        public void setThumbImage(final Context context, final String thumb_image) {
            final CircleImageView all_user_image = (CircleImageView) view.findViewById(R.id.all_user_image);

            if(!thumb_image.equals("default_image")) {
                Picasso.with(context).load(thumb_image).networkPolicy(NetworkPolicy.OFFLINE)
                        .placeholder(R.drawable.default_image).into(all_user_image, new Callback() {
                    @Override
                    public void onSuccess() {

                    }

                    @Override
                    public void onError() {
                        Picasso.with(context).load(thumb_image).placeholder(R.drawable.default_image).into(all_user_image);
                    }
                });
            }
        }
    }

并创建一个helper class,如下所示

public class AllUsers {

    public String thumb_image;

    public AllUsers() {

    }

    public AllUsers(String thumb_image) {

        this.thumb_image = thumb_image;
    }

    public String getThumbImage() {
        return thumb_image;
    }

    public void setThumbImage(String thumb_image) {
        this.thumb_image = thumb_image;
    }


}

之后,您可以将图像检索到您的回收站视图

【讨论】:

  • 如果有问题请告诉我
猜你喜欢
  • 2020-09-03
  • 2018-03-05
  • 2017-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-22
  • 2020-01-04
  • 1970-01-01
相关资源
最近更新 更多