如何使用Firebase在Android Studio中查询此内容?

时间:2017-06-26 15:26:27

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

在我的软件中我需要一个查询,如何查询数据库中的2个信息?我需要“uid”和“book”,查看用于观看我的数据库的链接:https://i.stack.imgur.com/9TSt6.png

listWN = (RecyclerView) findViewById(R.id.listReading);

    listWN.setLayoutManager(new LinearLayoutManager(this));

    bookYouAreIn = getIntent().getStringExtra("book");

    firebaseauth = FirebaseAuth.getInstance();

    String currentUserId = firebaseauth.getCurrentUser().getUid();

    mDatabase = FirebaseDatabase.getInstance().getReference("Data");

    mQuery = mDatabase.orderByChild("book").equalTo(bookYouAreIn); **---> HOW DO I ADD HERE: AND mDatabase.orderByChild("uid").equalTo(firebaseauth.getCurrentUser().getUid());**

    mDatabase.keepSynced(true);

    if(!firebaseauth.getCurrentUser().getUid().equals(currentUserId)){

        startActivity(new Intent(WordNote.this,LoginActivity.class));
        finish();
    }


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.main_secondmenu, menu);

    return super.onCreateOptionsMenu(menu);
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if(item.getItemId() == R.id.action_addWords){

        Intent postIntent = new Intent(WordNote.this, PostActivity.class);
        postIntent.putExtra("bookToPost", bookYouAreIn);
        startActivity(postIntent);

    }

    return super.onOptionsItemSelected(item);
}

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

    final FirebaseRecyclerAdapter<MyReading, MyReadingViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter <MyReading, MyReadingViewHolder>
            (
            MyReading.class,
            R.layout.list_reading,
            MyReadingViewHolder.class,
            mQuery )

    {
        @Override
        protected void populateViewHolder(final MyReadingViewHolder viewHolder, final MyReading model, final int position) {

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

           viewHolder.setWord(model.getWord());
           viewHolder.setNote(model.getNote());

          mDatabase.child(wordNote_key).addValueEventListener(new ValueEventListener() {
              @Override
              public void onDataChange(DataSnapshot dataSnapshot) {

                  String user = (String) dataSnapshot.child("uid").getValue();
                  String bookNow = (String) dataSnapshot.child("book").getValue();

                  if(firebaseauth.getCurrentUser().getUid().equals(user)){

                      mDatabase.child(wordNote_key).orderByChild("uid").equals(user);
                  }
                  else {
                     viewHolder.nView.setVisibility(View.GONE); **--> IT DOESN'T GO AWAY, IT KEEPS STAYING AS A BLANK VIEW**


                  }


              }

              @Override
              public void onCancelled(DatabaseError databaseError) {

              }
          });

                    viewHolder.nView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Intent singleIntent = new Intent(WordNote.this, HandleWordNote.class);
                    singleIntent.putExtra("listWords", wordNote_key);
                    startActivity(singleIntent);

                }
            });

        }
    };

    listWN.setAdapter(firebaseRecyclerAdapter);

1 个答案:

答案 0 :(得分:0)

请参阅此post的答案。

在您的情况下,字段为:uid_book: "OKtUxSleCGbU0F_Pasqua"

希望它有所帮助。