将数据从Firebase显示到回收者视图中

时间:2019-01-06 02:13:35

标签: android firebase firebase-realtime-database

非常不喜欢这个。我正在尝试显示Firebase数据库中的用户名和个人资料图片。我已经成功地显示了帖子图像和描述,但是这些存储在firestore中。这是我的代码状态

public class BlogRecyclerAdapter extends RecyclerView.Adapter<BlogRecyclerAdapter.ViewHolder> {

public List<BlogPost> blog_list;

private FirebaseFirestore firebaseFirestore;
private FirebaseDatabase firebaseDatabase;

private DatabaseReference mUserDatabase;
private FirebaseUser mCurrentUser;


public BlogRecyclerAdapter(List<BlogPost> blog_list) {

    this.blog_list = blog_list;

}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.blog_list_item, parent, false);
    firebaseFirestore = FirebaseFirestore.getInstance();
    return new ViewHolder(view);
}


@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {

    String desc_data = blog_list.get(position).getDesc();
    holder.setDescText(desc_data);

    String image_url = blog_list.get(position).getImage_url();
    holder.setBlogImage(image_url);

    long millisecond = blog_list.get(position).getTimestamp().getTime();
    String dateString = DateFormat.format("MM/dd/yyyy", new Date(millisecond)).toString();
    holder.setTime(dateString);

   String user_data = blog_list.get(position).getUser_id();
    holder.setUserData(user_data);


}

@Override
public int getItemCount() {
    return blog_list.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {

    private View mView;

    private TextView descView;
    private ImageView blogPostView;
    public Context mContext;

    private TextView blogDate;

    private TextView blogUserName;
    private CircleImageView blogUserImage;

    private StorageReference mImageStorage;


    public ViewHolder(View itemView) {
        super(itemView);
        mView = itemView;

    }

    public void setDescText(String descText) {

        descView = mView.findViewById(R.id.blog_desc);
        descView.setText(descText);

    }


    public void setUserData(String downloadUrl, String name){

        mImageStorage = FirebaseStorage.getInstance().getReference();

        mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
        final String current_uid = mCurrentUser.getUid();

        mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(current_uid);
        mUserDatabase.keepSynced(true);

        mUserDatabase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                String name = Objects.requireNonNull(dataSnapshot.child("name").getValue()).toString();

                StorageReference filepath = mImageStorage.child("profile_images").child(current_uid + (".jpeg"));
                Log.d("heere", "S");

                // This gets the download url async
                filepath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                    @Override
                    public void onSuccess(Uri uri) {

                        //The download url
                        final String downloadUrl =
                                uri.toString();
                        Log.d("tag", downloadUrl);
                        if (!downloadUrl.equals("default")) {

                          //  Glide.with(getApplicationContext()).load(downloadUrl).into(mDisplayImage);
                            Glide.with(itemView.getContext()).load(downloadUrl).into(blogUserImage);

                        }
                    }
                }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                       // Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_SHORT).show();
                    }
                });
                //Toast.makeText(getApplicationContext(), image, Toast.LENGTH_SHORT).show();

                Log.d("value", dataSnapshot.getValue().toString());
                Log.d("key", dataSnapshot.getKey());
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });



    }

    public void setTime (String date){

        blogDate = mView.findViewById(R.id.blog_date);
        blogDate.setText(date);

    }

    public void setBlogImage(final String downloadUri) {

        blogPostView = mView.findViewById(R.id.blog_image);
        Glide.with(itemView.getContext()).load(downloadUri).into(blogPostView);


    }}}

在setuserdata中,我可以获取数据。然后,我尝试将其显示在onBindViewHolder中。 onBind中的代码不起作用

Database Layout

0 个答案:

没有答案