如何使用UserInformation Class

时间:2019-05-28 19:12:52

标签: java android firebase firebase-realtime-database

我无法从实时数据库向特定的textview显示正确的用户信息。目前,他们都没有显示

此应用程序包括android studio,并基于电子邮件和userId提取用户信息。

我的数据库结构非常简单,如下所示: enter image description here

我的UserInformation类是这样的类型:

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;
}

我认为问题出在试图在TextView中显示信息时从我使用的'append'方法的数据库中提取数据。代码是:

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

    }
}

1 个答案:

答案 0 :(得分:1)

似乎您使用的是Firestore而非实时数据库,要在Firestore中检索集合的数据,请尝试以下操作:

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());
            }
        }
    });

查看文档以获取更多信息:

join