在recyclerview中将图像从firebase显示到我的android应用程序

时间:2017-12-09 03:17:06

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

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

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

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

如果使用片段,我可以在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);
    }

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

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;
    }


}

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