0

創建用戶配置文件的數據編輯屏幕。目的是檢索保存在Firebase數據庫中的數據,並將其顯示在EditText中。數據庫數據已經建議供用戶編輯或不編輯,可選。在EditText中檢索並顯示Firebase數據並再次編輯內容保存

EXAMPLE: 
Firebase Database 
- users 
--iduser 
----name 
----lastname 


Screen Example: 
Fields: 
- EditText1 (mName) 
- EditText2 (mLastName) 

我想是這樣的:

MainActivity: 
private EditText mName; 
private EditText mLastName; 

    private DatabaseReference mDatabaseUsers; 


    private FirebaseAuth mAuth; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     toolbar.setTitle("Dashboard"); 
     setSupportActionBar(toolbar); 

     mDatabaseUsers = FirebaseDatabase.getInstance().getReference().child("Users"); 


     /*User logged in*/ 
     final String user_id = mAuth.getCurrentUser().getUid(); 
     mName = (EditText) findViewById(R.id.edtName); 
     mLastName = (EditText) findViewById(R.id.edtLastName);   

     /*I have already created a String to capture the logged-in user ID because I need to access only those user's data.*/  
     /* I've got this far, I need help to set the value in EditText that will be displayed. Thank you!!*/ 

     mDatabaseUsers.addValueEventListener(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 




      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 

      } 
     }); 



} 

的目的是設置在EditText上的內容,就像這樣:mName.setText( 「銀行資料」,TextView.BufferType.EDITABLE);

然後,將更改的數據保存回數據庫。

回答

1

嘗試類似的東西:

MainActivity: 
private EditText mName; 
private EditText mLastName; 

    private DatabaseReference mDatabaseUser_name,mDatabaseUser_lastname; 


    private FirebaseAuth mAuth; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     toolbar.setTitle("Dashboard"); 
     setSupportActionBar(toolbar); 


     /*User logged in*/ 
     final String user_id = mAuth.getCurrentUser().getUid(); 
     mName = (EditText) findViewById(R.id.edtName); 
     mLastName = (EditText) findViewById(R.id.edtLastName);   

     mDatabaseUser_name = FirebaseDatabase.getInstance().getReference().child("Users").child(user_id).child("name"); 
     mDatabaseUser_lastname = FirebaseDatabase.getInstance().getReference().child("Users").child(user_id).child("lastname"); 

     mDatabaseUser_name.addValueEventListener(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
        mName.settext(dataSnapshot.getValue(String.class)); 

      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 

      } 
     }); 

     mDatabaseUser_lastname.addValueEventListener(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
        mLastName.settext(dataSnapshot.getValue(String.class)); 

      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 

      } 
     }); 

} 
0

您有在數據檢索的問題和顯示,數據檢索可以用不同的方式來完成的方式,我喜歡的是通過使用Query它的簡單和街頭花,但首先之前我寫的代碼,它讓我澄清一件事,你這樣說:

EXAMPLE: 
Firebase Database 
- users 
--iduser 
----name 
----lastname 

所以,你必須通過寫這

達到你的數據庫
 mDatabaseUsers = FirebaseDatabase.getInstance().getReference().child("users");// small u not U 

所以,這是失控的方式,現在你在你的數據庫中有很多不同的方式這個作品,但是這是我選擇:

Query query = firebaseDatabase.child(//write your user id here); 
      query.addValueEventListener(new ValueEventListener() { 
       @Override 
       public void onDataChange(DataSnapshot dataSnapshot) { 
        if (dataSnapshot!=null){ 
    mName.setText(dataSnapshot.child("name").getValue(String.class)); 
// the same for the last name 
         } 

        else { 
    mName.setText("There is no mutching data "); 
        } 
       } 

       @Override 
       public void onCancelled(DatabaseError databaseError) { 

       } 
      }); 
相關問題