Firebase:查询使用push()

时间:2017-01-03 20:44:18

标签: android firebase firebase-realtime-database

所以我的Firebase数据库看起来像这样:

{
  "lists" : {
    "-KZh-vvPcPGVqC22k2Bo" : {
      "dateCreated" : "2016-12-23",
      "listDescription" : "My Christmas Wish List for 2016",
      "listTitle" : "William's Christmas List",
      "user" : "ztGAx7eplGeZgdjqnegrtbfuyUy2"
    }
  },
  "users" : {
    "8pJJuscerZRGdwGGImnWlCKSEed2" : {
      "email" : "example@example.com",
      "name" : "Alyson"
    },
    "ztGAx7eplGeZgdjqnegrtbfuyUy2" : {
      "email" : "example@example.com",
      "name" : "William"
    }
  }
}

我试图只返回用户字段等于登录用户的UID的列表对象。这样登录用户就可以获得他所有的列表。但是由于push()生成的唯一键我无法在用户创建新列表对象时附加到列表中,因此我无法查询列表中的数据。如何查询列表,以便只获得与已登录用户的UID匹配的列表?到目前为止我有这个

package com.fanciestw.listpro;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;

public class allList extends AppCompatActivity {

    private FirebaseAuth mAuth = FirebaseAuth.getInstance();
    private FirebaseAuth.AuthStateListener mAuthStateListener;
    private FirebaseDatabase mDatabase = FirebaseDatabase.getInstance();
    private DatabaseReference mList = mDatabase.getReference().child("lists");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_list);
        mAuthStateListener = new FirebaseAuth.AuthStateListener(){
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth){
                FirebaseUser user = firebaseAuth.getCurrentUser();
                if(user != null) {
                    Log.d("User Activity", "User Signed In");
                } else {
                    Log.d("User Activity", "User Signed Out");
                    signout(getCurrentFocus());
                }
            }
        };
        mList.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                List newList = dataSnapshot.getValue(List.class);
                Log.d("List Returned", newList.listTitle + " " + newList.listDescription);
                updateList();
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {

            }
            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {
                updateList();
            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

    @Override
    public void onStart(){
        super.onStart();
        Log.d("allList Activity", "onStart");
        mAuth.addAuthStateListener(mAuthStateListener);
    }

    @Override
    public void onStop(){
        super.onStop();
        Log.d("allList Activity", "onStop");
        if(mAuthStateListener != null) mAuth.removeAuthStateListener(mAuthStateListener);
    }

    public void addNewList(View view){
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Add New List");

        LayoutInflater inflater = this.getLayoutInflater();
        final View dialogView = inflater.inflate(R.layout.add_new_list_form, null);

        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout
        builder.setView(dialogView);

        // Set up the buttons
        builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String title = ((EditText)dialogView.findViewById(R.id.add_list_title)).getText().toString();
                String desc = ((EditText)dialogView.findViewById(R.id.add_list_desc)).getText().toString();

                Log.d("New List Details", title + ", " + desc);
                //TODO::Store created list with title and desc in database
                List newList = new List(title, desc, mAuth.getCurrentUser().getUid());
                String newListID = mList.push().getKey();
                mList.child(newListID).setValue(newList);
            }
        });
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

        builder.show();
    }

    public void updateList(){
        //Want to get lists where lists.user == firebaseAuth.getCurrentUser().UID();
    }

    public void signout(View view){
        mAuth.signOut();
        Intent intent = new Intent(this, login.class);
        startActivity(intent);
    }
}

1 个答案:

答案 0 :(得分:2)

<强>声明

    private FirebaseAuth mAuth;
    private FirebaseUser mCurrentUser;
    private String userId = null;

<强>初始化

    mAuth = FirebaseAuth.getInstance();
    mCurrentUser = mAuth.getCurrentUser();
    userId = mCurrentUser.getUid().toString();
    String UIDstring = (String) dataSnapshot.child("users").getValue();

确定

if (userId.equals(UIDstring)) {
       //true
    } else {
      // false
   }