我也想更新他在用户更新/更改他的个人资料图片时发布的所有帖子中的用户个人资料图片

时间:2019-05-23 22:07:01

标签: java android firebase firebase-realtime-database

enter image description here enter image description here 我正在更新用户个人资料图片,它在我想要的所有活动中均能完美运行。但是问题在于,它不会更新用户已发布的所有帖子中的用户个人资料图片。我也想在那里更新用户的个人资料图片。

这是数据库参考

postRef= FirebaseDatabase.getInstance().getReference().child("posts");

这是我的“回收站”视图设置

FirebaseRecyclerOptions<Posts> options=new FirebaseRecyclerOptions.Builder<Posts>()
            .setQuery(postRef,Posts.class)
            .build();

    FirebaseRecyclerAdapter<Posts,PostsViewHolder> firebaseRecyclerAdapter=new FirebaseRecyclerAdapter<Posts, PostsViewHolder>(options)
    {
        @Override
        protected void onBindViewHolder(@NonNull final PostsViewHolder holder, int position, @NonNull final Posts model) {

            final String postKey=getRef(position).getKey();

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent clickPostIntent=new Intent(MainActivity.this, ClickPostActivity.class);
                    clickPostIntent.putExtra("postKey",postKey);
                    startActivity(clickPostIntent);
                }
            });

            holder.username.setText(model.getFullname());
            holder.date.setText(model.getDate());
            holder.time.setText(model.getTime());

            if (model.getPost_uri_link()!=null){
                holder.post_link.setText(model.getPost_uri_link());
            }
            else {
                holder.post_link.setVisibility(View.GONE);
            }
            if (model.getPost_file_name()!=null && model.getPost_file_uri()!=null) {
                holder.file_attach_name.setText(model.getPost_file_name());
                holder.file_attach_name.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        downloadFile(MainActivity.this,model.getPost_file_name(),"gs://studentcornerproject-be4c6.appspot.com/post files",model.getPost_file_uri());
                    }
                });
            }
            else{
                holder.file_attach_name.setVisibility(View.GONE);
            }

            if (model.getDescription()!=null) {
                holder.description.setText(model.getDescription());
           }
            else {
                holder.description.setVisibility(View.GONE);
            }

            if (model.getPost_img()!=null) {
                Picasso.get().load(model.post_img).networkPolicy(NetworkPolicy.OFFLINE).placeholder(android.R.drawable.progress_indeterminate_horizontal).into(holder.post_img, new Callback() {
                    @Override
                    public void onSuccess() {

                        postRef.child("post_img").keepSynced(true);
                    }

                    @Override
                    public void onError(Exception e) {

                        Picasso.get().load(model.post_img).into(holder.post_img);
                    }
                });
            }
            else {
                holder.post_img.setVisibility(View.GONE);
            }
            Picasso.get().load(model.getProfile_image()).networkPolicy(NetworkPolicy.OFFLINE).placeholder(R.drawable.profile_icon).into(holder.prof_img, new Callback() {
                @Override
                public void onSuccess() {

                }

                @Override
                public void onError(Exception e) {

                    Picasso.get().load(model.getProfile_image()).into(holder.prof_img);
                }
            });
        }

        @NonNull
        @Override
        public PostsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

            View view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.all_posts_users,viewGroup,false);
            PostsViewHolder viewHolder=new PostsViewHolder(view);
            return viewHolder;

        }
    };
    postslist.setAdapter(firebaseRecyclerAdapter);
    firebaseRecyclerAdapter.startListening();
}

public static class PostsViewHolder extends RecyclerView.ViewHolder
{

   TextView username,date,time,description,file_attach_name,post_link;
    CircleImageView prof_img;
    ImageView post_img;

    public PostsViewHolder(@NonNull View itemView) {
        super(itemView);
        post_link=itemView.findViewById(R.id.post_link);
        username=itemView.findViewById(R.id.post_user_name);
        date=itemView.findViewById(R.id.post_date);
        time=itemView.findViewById(R.id.post_time);
        description=itemView.findViewById(R.id.post_description);
        file_attach_name=itemView.findViewById(R.id.post_filename);
        prof_img=itemView.findViewById(R.id.post_profile_img);
        post_img=itemView.findViewById(R.id.post_image);


    }

我也想更新他已经发布的所有帖子中的个人资料图片。

2 个答案:

答案 0 :(得分:0)

在您的userID节点内,您应该存储该用户帖子的ID,然后您就可以更改该用户发表的所有帖子的photo_url。

示例

height: 'parent'

然后,当您更改用户的photo_url时,您可以检索所有帖子并更改相同的photo_url

答案 1 :(得分:0)

我的问题已经解决。这是正确的代码。

 Query postQuery=postRef.orderByChild("uid").equalTo(currentUserId);
        postQuery.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot snapshot:dataSnapshot.getChildren()){
                    Log.i("Post",snapshot.getKey());
                    postRef.child(snapshot.getKey()).child("profile_image").setValue(profile_img);
                    postRef.child(snapshot.getKey()).child("fullname").setValue(fullname);




                }
            }

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

            }
        });