我如何获取FirebaseRecylerAdapter android studio实时数据库的所有数据

问题描述 投票:0回答:1

我正在将Firebase实时数据库用于Android Studio。代码java。我使用实时数据库。我尝试将用户关注的人员的所有帖子都带走。但我不能把所有的帖子都带走只有用户发送的最新帖子才来

我有这个数据库:

enter image description here

enter image description here

岗位班级:

public class Post {

    private String title , content  , currentUserID , date , time , fullname , nickname , timestamp ;

    public Post() {

    }


    public Post(String title, String content, String currentUserID , String date , String time , String fullname , String nickname , String timestamp ) {
        this.title = title;
        this.content = content;
        this.currentUserID = currentUserID;
        this.date = date ;
        this.time = time ;
        this.fullname = fullname ;
        this.nickname = nickname ;
        this.timestamp = timestamp ;

    }



    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getCurrentUserID() {
        return currentUserID;
    }

    public void setCurrentUserID(String currentUserID) {
        this.currentUserID = currentUserID;
    }

    public String getDate() {
        return date;
    }

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

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }


我参考数据库als:

RecyclerView recyclerView;
DatabaseReference profileUserRef , Followers   ;
FirebaseDatabase firebaseDatabase ;
FirebaseRecyclerOptions<Post> options ;
FirebaseRecyclerAdapter<Post,MyRecyclerViewHolder> adapter ;
FirebaseAuth mAuth ;
String  currentUserID;
Post post ;

 mAuth = FirebaseAuth.getInstance();
 currentUserID = mAuth.getCurrentUser().getUid();
 firebaseDatabase = FirebaseDatabase.getInstance();
 Followers = FirebaseDatabase.getInstance().getReference().child("Follow");
 recyclerView = homefragment.findViewById(R.id.recycler_view);
 recyclerView.setHasFixedSize(true);
 linearLayoutManager = new LinearLayoutManager(getContext());
 linearLayoutManager.setReverseLayout(true);
 linearLayoutManager.setStackFromEnd(true);
 recyclerView.setLayoutManager(linearLayoutManager);
 profileUserRef = FirebaseDatabase.getInstance().getReference().child("Users");


我的代码:

public void getPost() {

        options =
                new FirebaseRecyclerOptions.Builder<Post>()
                        .setQuery(Followers.child(currentUserID), Post.class)
                        .build();

        adapter =
                new FirebaseRecyclerAdapter<Post, MyRecyclerViewHolder>(options) {
                    @Override
                    protected void onBindViewHolder(@NonNull final MyRecyclerViewHolder holder, final int position, @NonNull final Post model ) {
                        list_user_id = getRef(position).getKey();
                        final String PostKey = getRef(position).getKey();


                        DatabaseReference getTypeRef = getRef(position).child("request_type").getRef();

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

                                if (dataSnapshot.exists()){
                                   String type = dataSnapshot.getValue().toString();
                                     if (type.equals("follow")){

                                         profileUserRef.child(PostKey).child("Post").orderByChild("timestamp").addValueEventListener(new ValueEventListener() {
                                             @Override
                                             public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                                                 if (dataSnapshot.exists()){

                                                     for (final DataSnapshot snapshot : dataSnapshot.getChildren()){
                                                         post = snapshot.getValue(Post.class);


                                                             holder.txt_title.setText(post.getTitle());

                                                             holder.txt_comment.setText(post.getContent());

                                                             holder.txtpostname.setText(post.getFullname());

                                                     }


                                                 }

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

                                             }
                                         });

                                     }
                                }

                            }

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

                            }
                        });


                    }

                    @NonNull
                    @Override
                    public MyRecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
                        View itemView = LayoutInflater.from(getContext()).inflate(R.layout.post_item,viewGroup,false);
                        return new MyRecyclerViewHolder(itemView);

                    }

                };

        adapter.startListening();
        recyclerView.setAdapter(adapter);


    }

如何获得所关注用户的所有帖子?

android firebase firebase-realtime-database firebaseui
1个回答
0
投票

您应该定义Post类的Arraylist;例如:

var latlnginfo = ArrayList<Post>()

然后您将数据像这样添加到Arraylist中:

 database.addValueEventListener(object : ValueEventListener {
        override fun onCancelled(p0: DatabaseError) {
        }

        override fun onDataChange(p0: DataSnapshot) {
            if (p0.exists()) {
                latlnginfo.clear()
                for (h in p0.children) {
                    latlnginfo.add(h.getValue(Post::class.java)!!)
                }}}}

我认为您代码的问题是您没有将数据添加到数组中,而只显示了最后一项。

© www.soinside.com 2019 - 2024. All rights reserved.