无法获取RecyclerView的当前用户ID的子级

时间:2018-09-30 12:12:03

标签: android firebase firebase-realtime-database firebaseui

我想使用getCurrentUser().getUid()设置带有当前用户ID的子代的RecyclerView。您可以在下图中看到的数据结构enter image description here

在上图中, SQyOq80egYehjqx4sgiyeNcW8P02 当前用户ID ,我想获取所有这些ID的子代并显示在RecyclerView中。在上图中,孩子是 wed5qPTCdcQVzVlRcBrMo1NX43v1 ,其值是 2018年9月29日。我的问题是如何分别获取这些子代价值并在RecyclerView中显示。例如,我为 Date (当前userId的值)编写了代码,该代码给出了致命错误。我知道无法理解的Model类错误。

注意:此行显示错误。 Log.d("sdfsdfdgfdfsdfd", blogPost.getDate());

活动:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
        DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
        Query query = rootRef.child("Friends").child(uid);

        FirebaseRecyclerOptions<BlogPost> firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<BlogPost>()
                .setQuery(query, BlogPost.class)
                .build();

        firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<BlogPost, BlogPostHolder>(firebaseRecyclerOptions) {
            @Override
            protected void onBindViewHolder(@NonNull BlogPostHolder blogPostHolder, int position, @NonNull BlogPost blogPost) {

                Log.d("sdfsdfdgfdfsdfd", blogPost.getDate());
                blogPostHolder.setBlogPost(blogPost);
            }

            @Override
            public BlogPostHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);

                return new BlogPostHolder(view);
            }
        };
        recyclerView.setAdapter(firebaseRecyclerAdapter);
    }

    @Override
    protected void onStart() {
        super.onStart();
        firebaseRecyclerAdapter.startListening();
    }

    @Override
    protected void onStop() {
        super.onStop();

        if (firebaseRecyclerAdapter!= null) {
            firebaseRecyclerAdapter.stopListening();
        }
    }

    private class BlogPostHolder extends RecyclerView.ViewHolder {
        private TextView  userDateTextView;

        BlogPostHolder(View itemView) {
            super(itemView);
            userDateTextView = itemView.findViewById(R.id.user_date);
        }

        void setBlogPost(BlogPost blogPost) {
            String date = blogPost.getDate();
            userDateTextView.setText(date);
        }
    }
}

型号:

public class BlogPost {
    public String date;

    public BlogPost() {}

    public BlogPost(String date) {
        this.date = date;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }


}

2 个答案:

答案 0 :(得分:1)

将Firebase中的子键更改为日期

enter image description here

答案 1 :(得分:0)

JSON中的wed5qPTCdcQVzVlRcBrMo1NX43v1似乎是动态的,这意味着每个用户都有不同的密钥。 Firebase客户端无法将这些信息解析到您的BlogPost类中,该类仅具有静态属性。

这意味着您必须提供自己的SnapshotParser类才能将DataSnapshot转换为BlogPost对象。基于FirebaseUI documentation,看起来应该像这样:

FirebaseRecyclerOptions<BlogPost> firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<BlogPost>()
  .setQuery(query, , new SnapshotParser<Chat>() {
    @NonNull
    @Override
    public BlogPost parseSnapshot(@NonNull DataSnapshot snapshot) {
        // first let Firebase read any static properties
        BlogPost post = snapshot.getValue(BlogPost.class); 

        // then do the parsing of the dynamic properties ourselves
        for (DataSnapshot friendSnapshot: snapshot.getChildren()) {
            String friendUID = friendSnapshot.getKey();
            String friendDate = friendSnapshot.getValue(String.class);

            post.setDate(friendDate);
        }

        return post;
    }
}).build();

上面的代码由FirebaseUI为其从数据库中获取的每个新快照调用。它首先调用snapshot.getValue(BlogPost.class)将JSON的静态部分转换为post,然后解析好友本身。目前,它会将找到的任何值设置为帖子的date属性。您可能需要对其进行修改以适合您的确切用例。