I'm creating an application where a user can create an account and also can log in. Also I have a profile page where the user can see his personal information. My question is how can I retrieve data like fullname, email, password, etc from a specific or current user into my profile class?
In addition, I'm using a fragment for my profile class.
Here is my database structure:
Here is my code:
_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>
I hope you could help me! Thank you!
Before going to the development you have to structure you data denormalize,flatten and duplicate your data in a user prescriptive way. And before you decide your final structure, you should know that how and when you retrieve that data. If you plan well for your database structure you will simplify your programming process and you will save a lot of time.
My solution for this would be duplicating the key of the user inside the user tree as an user ID as follows:
acounts:
---- -KSOMEKEYVALUE
--------brithday:*****
--------fullname:*****
--------id: KSOMEKEYVALUE
Then when the log in process succeed, you retrieve the data for that specific user based on their unique ID as follows:
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
Then you attach your listener to that specific userID as follows:
// 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());
}
});
The firebaseUser
object should contain the data for the logged in user.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.