如何使用快照firebase查看数据库中的内容

时间:2017-06-18 23:23:21

标签: android firebase firebase-realtime-database snapshot

Json Code

Database

      package com.example.durveshshah.feedback;

       import android.app.ProgressDialog;
      import android.content.Intent;
       import android.support.annotation.NonNull;
      import android.support.v7.app.AppCompatActivity;
       import android.os.Bundle;
     import android.util.Log;
     import android.widget.ArrayAdapter;
      import android.widget.EditText;
      import android.widget.ListView;
       import android.widget.Toast;

        import com.firebase.client.Firebase;
        import com.google.android.gms.tasks.OnCompleteListener;
        import com.google.android.gms.tasks.Task;
        import com.google.firebase.auth.AuthResult;
        import com.google.firebase.auth.FirebaseAuth;
        import com.google.firebase.auth.FirebaseUser;
        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.ValueEventListener;

        import java.util.ArrayList;

       public class Main4Activity extends AppCompatActivity {

    private static final String TAG = "viewDatabase";
    private DatabaseReference mRef;
    private FirebaseDatabase mFirebaseDatabase;
    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener mAuthListener;
    private String UserId;
    private ListView mListView;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main4);

    mListView = (ListView) findViewById(R.id.list);
    mAuth = FirebaseAuth.getInstance();
    mFirebaseDatabase = FirebaseDatabase.getInstance();
    mRef = mFirebaseDatabase.getReference();
    final FirebaseUser user = mAuth.getCurrentUser();
    UserId = user.getUid();

    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if(user!=null)
            {
                //User is signed in
                Log.d(TAG,"onAuthStateChanged:signed_in:"+ user.getUid());
                Log.d(TAG,"onAuthStateChanged:signed_in:"+ user.getEmail());
            }
            else
            {
                //User is signed out
                Log.d(TAG,"onAuthStateChanged:signed_out");
            }
        }
    };


    mRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot ds : dataSnapshot.getChildren()) {


                    //Log.i("phone",ds.getKey());
                    ArrayList<String> array = new ArrayList<>();
                    array.add(String.valueOf(ds.getValue()));

                    ArrayAdapter arrayAdapter = new ArrayAdapter(Main4Activity.this, android.R.layout.simple_list_item_1, array);
                    mListView.setAdapter(arrayAdapter);


            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError)
        {
            Toast.makeText(Main4Activity.this,"Logged Out",Toast.LENGTH_SHORT).show();
        }
    });



      }


  }

我在不同的活动中使用了signinwithemailandpassword内置的firebase机制,登录后我想查看该特定用户的内容。相反,我登录的每个用户都可以看到所有数据库内容。

1 个答案:

答案 0 :(得分:0)

要实现此目的,您需要创建一个名为users的新节点。每次用户登录时,您都可以获得uid。您的数据库应如下所示:

Firebase-root
   |
   ---- users
          |
          ---- uid1
          |     |
          |     // details
          |
          ---- uid2
          |     |
          |     // details
          |
          ---- //and so on

假设您的手机节点嵌套在uid下,您可以使用以下代码:

DatabaseReference yourRef = FirebaseDatabase.getInstance().getReference().child("users").child(user.getUid()).child("phone");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String phone = ds.getValue(String.class);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
yourRef.addListenerForSingleValueEvent(eventListener);

希望它有所帮助。