2017-09-02 82 views
0

我正在創建一個應用程序,用戶可以創建一個帳戶,也可以登錄。此外,我有一個用戶可以查看其個人信息的個人資料頁面。我的問題是如何從特定或當前用戶檢索全名,電子郵件,密碼等數據到我的個人檔案類中?Android:如何從Firebase中的當前用戶檢索數據?

另外,我爲我的個人檔案類使用了一個片段。

這裏是我的數據庫結構:

Here is my database structure

這裏是我的代碼:

_8_ViewEventMember_Profile.java

public class _8_ViewEventMember_Profile extends Fragment { 

private FirebaseAuth auth; 
TextView name, email, password, bday, country, mobileno; 

View myView; 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    myView = inflater.inflate(R.layout.activity__8__view_event_member_profile, container, false); 

    ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Profile"); 

    name = (TextView) getActivity().findViewById(R.id.textName); 
    email = (TextView) getActivity().findViewById(R.id.textEmail); 
    password = (TextView) getActivity().findViewById(R.id.textPassword); 
    bday = (TextView) getActivity().findViewById(R.id.textCountry); 
    country = (TextView) getActivity().findViewById(R.id.textCountry); 
    mobileno = (TextView) getActivity().findViewById(R.id.textMobileNumber); 

    auth = FirebaseAuth.getInstance(); 

    FirebaseAuth.AuthStateListener authListener = new FirebaseAuth.AuthStateListener() { 
     @Override 
     public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { 
      FirebaseUser firebaseUser = firebaseAuth.getCurrentUser(); 
      if(firebaseUser != null){ 
       String userName = firebaseUser.getDisplayName(); 
       String userEmail = firebaseUser.getEmail(); 

       FirebaseDatabase db = FirebaseDatabase.getInstance(); 
       String key = db.getReference("accounts").push().getKey(); 

       Map<String, Object> childUpdates = new HashMap<>(); 
       childUpdates.put("fullname", userName); 


       name.setText(userName); 
       email.setText(userEmail); 

      } 
     } 
    }; 
    return myView; 
} 
} 

activity__8__view_event_member_profile.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_margin="20dp"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:layout_marginTop="20dp"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Name:" 
     android:textColor="@color/colorBlack" 
     android:textSize="20sp"/> 

    <TextView 
     android:id="@+id/textName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Name" 
     android:textColor="@color/colorBlack" 
     android:textSize="20sp" 
     android:layout_marginLeft="55dp"/> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:layout_marginTop="20dp"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Email:" 
     android:textColor="@color/colorBlack" 
     android:textSize="20sp"/> 

    <TextView 
     android:id="@+id/textEmail" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Email" 
     android:textColor="@color/colorBlack" 
     android:textSize="20sp" 
     android:layout_marginLeft="60dp"/> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:layout_marginTop="20dp"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Password:" 
     android:textColor="@color/colorBlack" 
     android:textSize="20sp"/> 

    <TextView 
     android:id="@+id/textPassword" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Password" 
     android:textColor="@color/colorBlack" 
     android:textSize="20sp" 
     android:layout_marginLeft="23dp"/> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:layout_marginTop="20dp"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Birthday:" 
     android:textColor="@color/colorBlack" 
     android:textSize="20sp"/> 

    <TextView 
     android:id="@+id/textBday" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Birthday" 
     android:textColor="@color/colorBlack" 
     android:textSize="20sp" 
     android:layout_marginLeft="40dp"/> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:layout_marginTop="20dp"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Country:" 
     android:textColor="@color/colorBlack" 
     android:textSize="20sp"/> 

    <TextView 
     android:id="@+id/textCountry" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Country" 
     android:textColor="@color/colorBlack" 
     android:textSize="20sp" 
     android:layout_marginLeft="45dp"/> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:layout_marginTop="20dp"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Mobile No.:" 
     android:textColor="@color/colorBlack" 
     android:textSize="20sp"/> 

    <TextView 
     android:id="@+id/textMobileNumber" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Mobile Number" 
     android:textColor="@color/colorBlack" 
     android:textSize="20sp" 
     android:layout_marginLeft="20dp"/> 

    </LinearLayout> 

</LinearLayout> 

我希望你能幫助我!謝謝!

+0

你的意思是你想從列表中選擇特定的用戶並查看他/她的數據? – sabsab

+0

是的。當用戶在應用程序中登錄時,他的數據將立即從firebase中檢索並顯示在配置文件類中。我將如何做到這一點? –

+0

抱歉,延遲。你弄明白了,還是需要回答? – sabsab

回答

0

在進行開發之前,您必須structure you data以用戶規定的方式對數據進行非規範化,平整和複製。在決定最終結構之前,您應該知道如何以及何時檢索數據。如果你計劃好數據庫結構,你將簡化你的編程過程,你將會節省很多時間。


我給這會複製用戶樹內的用戶的密鑰作爲用戶ID如下解決方案:

acounts: 
---- -KSOMEKEYVALUE 
--------brithday:***** 
--------fullname:***** 
--------id: KSOMEKEYVALUE 

然後,當登錄過程成功,您檢索的數據基於其獨特的ID特定的用戶,如下所示:

DatabaseReference ref = database.getReference("accounts"); 
    login=ref.child(userID); //here user will sign in using his name so it is 
          //based on userID, directory will change dynamically 

然後您將您的聽衆到特定的用戶ID如下:

// Attach a listener to read the data at our posts reference 
    login.addValueEventListener(new ValueEventListener() { 
    @Override 
    public void onDataChange(DataSnapshot dataSnapshot) { 
     FirebaseUser firebaseUser = dataSnapshot.getValue(Post.class); 

    } 

    @Override 
    public void onCancelled(DatabaseError databaseError) { 
     System.out.println("The read failed: " + databaseError.getCode()); 
    } 
}); 

firebaseUser對象應包含登錄用戶的數據。

+0

謝謝你!但是我如何將它顯示在列表視圖中? –

+0

改爲使用Recycler視圖,它是現代化的方式,可替代列表視圖和漸變視圖。怎麼做?這是一個不同的問題。這個鏈接幫了我很多。它的俄語,但很明白要理解。 https://www.youtube.com/watch?v=PJTs8vZKlWw&vl=en – sabsab

相關問題