Android Firebase按用户ID过滤

时间:2017-03-26 13:06:38

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

我制作了简单的“帖子博客”应用,用户可以发布特定帖子。 所有帖子都显示在FirebaseRecyclerAdapter中,它们的工作正常。 但是我想过滤帖子,因此它只显示当前的用户帖子。

我已经定义了按用户ID过滤的查询,但由于某种原因它无法正常工作,因为它仍会显示所有用户的帖子。

userAuth = FirebaseAuth.getInstance();
    mCurrentUser = userAuth.getCurrentUser();
    AuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            if (firebaseAuth.getCurrentUser() == null) {
                Intent loginIntent = new Intent(MainActivity.this, LogInActivity.class);
                loginIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(loginIntent);
            }
        }
    };


    myDatabase = FirebaseDatabase.getInstance().getReference().child("HelpPosts");
    DatabaseOfUsers = FirebaseDatabase.getInstance().getReference().child("Users");

    //make a filter for post
    userId = userAuth.getCurrentUser().getUid();
    CurrentUser = FirebaseDatabase.getInstance().getReference().child("HelpPosts");
    mQuerryCurrentUser = CurrentUser.orderByChild("uid").equalTo(userId);

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

    userAuth.addAuthStateListener(AuthListener);

    FirebaseRecyclerAdapter<Post,PostViewHolder>firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Post, PostViewHolder>
            (Post.class,
             R.layout.post_row,
             PostViewHolder.class,
             CurrentUser) {


        @Override
        protected void populateViewHolder(PostViewHolder viewHolder, Post model, final int position) {
            final String post_key = getRef(position).getKey();
            viewHolder.setTitle(model.getTitle());
            viewHolder.setDesc(model.getDesc());
            viewHolder.setTnumber(model.getPhone());
            viewHolder.setUsername(model.getUsername());

            viewHolder.myView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent singleView = new Intent(MainActivity.this,PostView.class);
                    singleView.putExtra("post_id",post_key);
                    startActivity(singleView);
                }
            });
        }
    };
    recyclerView.setAdapter(firebaseRecyclerAdapter);
}

Firebase数据

"HelpPosts" : {
"-Kg9elNpSVKA1EY6Fh6X" : {
  "Address" : "test",
  "Desc" : "test",
  "Phone" : "5555",
  "Title" : "test1",
  "uid" : "FuOOyBOVtDaqrY4mgpquH935HSV2",
  "username" : "marius"
},
"-Kg9euxfW9ivRq80NwV3" : {
  "Address" : "test",
  "Desc" : "test",
  "Phone" : "2222",
  "Title" : "test2",
  "uid" : "FuOOyBOVtDaqrY4mgpquH935HSV2",
  "username" : "marius"
},
"-Kg9fIrq-hmUucb_R1b7" : {
  "Address" : "test",
  "Desc" : "test",
  "Phone" : "2222",
  "Title" : "test3",
  "uid" : "MgADOIrKbPQGo7pkfJzzCJAMwOk1",
  "username" : "mario"
}

}

2 个答案:

答案 0 :(得分:2)

你有很棒的代码。而且您只错过了一小步:在bluetoothSerial.isEnabled( () => { console.log("Bluetooth is enabled"); this.myboolflag = true; // Should work now!!! }, () => { console.log("Bluetooth is *not* enabled"); } ); 初始化时,您应该使用this而不是FirebaseRecyclerAdapter。像这样:

mQuerryCurrentUser

那应该这样做

答案 1 :(得分:-2)

userId = userAuth.getCurrentUser().getUid();
CurrentUser =FirebaseDatabase.getInstance().getReference().child("HelpPosts");
FirebaseRecyclerAdapter<Post,PostViewHolder>firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Post, PostViewHolder>
(Post.class,
        R.layout.post_row,
        PostViewHolder.class 
        CurrentUser.orderByChild("uid").equalTo(userId))