Ad

How To Show Firebase Data In TextViews Using UserInformation Class

I am having trouble showing the correct user information from my realtime database to the specific textviews. At the moment, none of them are showing

This application includes android studio and extracting user information based on email and userId.

My database structure is pretty simple as it looks like this: enter image description here

My UserInformation class is types as such:

public class UserInformation {

private String name;
private String email;
private String campus;
private String twop_num;
private Timestamp expiration_date;
private Double meals;
private Double pfunds;

public UserInformation(){

}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getCampus() {
    return campus;
}

public void setCampus(String campus) {
    this.campus = campus;
}

public String getTwop_num() {
    return twop_num;
}

public void setTwop_num(String twop_num) {
    this.twop_num = twop_num;
}

public Timestamp getExpiration_date() {
    return expiration_date;
}

public void setExpiration_date(Timestamp expiration_date) {
    this.expiration_date = expiration_date;
}

public Double getMeals() {
    return meals;
}

public void setMeals(Double meals) {
    this.meals = meals;
}


public Double getpfunds() {
    return pfunds;
}

public void setpfunds(Double punds) {
    this.pfunds = punds;
}

And I believe the problem is with pulling the data from the database of the 'append" method I am using when trying to show the information in the TextView. The code is:

    private TextView Email;
private TextView TwoPNum;
private TextView Meal;
private TextView PantherFunds;
private TextView Expiration;
private TextView Campus;
private Button logout;

//...

 Email = (TextView) findViewById(R.id.profileEmail);
 TwoPNum = (TextView) findViewById(R.id.profileUid);
 Meal = (TextView) findViewById(R.id.mealsNum);
 PantherFunds = (TextView) findViewById(R.id.pFundsNum);
 Expiration = (TextView) findViewById(R.id.expirationDate);
 Campus = (TextView) findViewById(R.id.campusText);

 //...

  private void showData(DataSnapshot dataSnapshot) {
    for (DataSnapshot ds : dataSnapshot.getChildren()) {
        UserInformation uInfo = new UserInformation();
        uInfo.setName(ds.child(userID).getValue(UserInformation.class).getName()); //set the name
        uInfo.setEmail(ds.child(userID).getValue(UserInformation.class).getEmail()); //set the email
        uInfo.setCampus(ds.child(userID).getValue(UserInformation.class).getCampus()); //set the phone_num
        uInfo.setExpiration_date(ds.child(userID).getValue(UserInformation.class).getExpiration_date());// set expiration date
        uInfo.setpfunds(ds.child(userID).getValue(UserInformation.class).getpfunds()); // get pantherfunds
        uInfo.setMeals(ds.child(userID).getValue(UserInformation.class).getMeals()); // get Meals
        uInfo.setTwop_num(ds.child(userID).getValue(UserInformation.class).getTwop_num()); // get Meals

        //display all the information
        Log.d(TAG, "showData: name: " + uInfo.getName());
        Log.d(TAG, "showData: email: " + uInfo.getEmail());
        Log.d(TAG, "showData: campus : " + uInfo.getCampus());
        Log.d(TAG, "showData: expiration_date: " + uInfo.getExpiration_date());
        Log.d(TAG, "showData: pfunds: " + uInfo.getpfunds());
        Log.d(TAG, "showData: : meals" + uInfo.getMeals());
        Log.d(TAG, "showData: twop_num: " + uInfo.getTwop_num());

        // Show Data in TextViews
        Email.append(uInfo.getEmail());
        TwoPNum.append(uInfo.getTwop_num());
        Meal.append(String.valueOf(uInfo.getMeals()));
        PantherFunds.append(String.valueOf(uInfo.getpfunds()));
        Expiration.append(String.valueOf(uInfo.getExpiration_date()));
        Campus.append(uInfo.getCampus());

    }
}
Ad

Answer

It seems you are using firestore and not realtime database, to retrieve data of the collection in firestore try the following:

db.collection("users")
    .get()
    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {
                for (QueryDocumentSnapshot document : task.getResult()) {
                    Log.d(TAG, document.getId() + " => " + document.getData());
                }
            } else {
                Log.d(TAG, "Error getting documents: ", task.getException());
            }
        }
    });

Check the docs for more info:

https://firebase.google.com/docs/firestore/query-data/get-data

Ad
source: stackoverflow.com
Ad