2017-06-18 55 views
-1

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內置火力機制在不同的活動和簽約後我希望查看特定用戶的內容查看從數據庫內容。相反,所有數據庫內容在我登錄的每個用戶中都可見。

+0

請添加您的火力數據庫結構和要檢索的數據? –

+0

這是唯一的結構 –

+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); 

希望它能幫助。

+0

非常感謝Alex。在你的代碼的幫助下做了一些修改,並最終得到了它 –

+0

欣賞它。謝謝 –

+0

有什麼好嗎? –