Firebase会恢复单个文本数据并将其设置为textview

时间:2018-03-17 21:10:41

标签: android database firebase firebase-realtime-database textview

我一直在尝试只检索保存在firebase中的用户名数据,并将其显示在导航抽屉的文本视图中,但我不断收到错误,应用程序崩溃或文本视图中没有显示任何内容,应用程序也没有显示。崩溃。应用程序不会因此代码崩溃。

app

这是我的数据库

This is my database

数据模型

public class users {
    private String userID;
    private String userEmail;
    private String userName;
    private String userUsername;


    public users() {

    }

    public users(String userID, String userEmail, String userName, String userUsername) {
        this.userID = userID;
        this.userEmail = userEmail;
        this.userName = userName;
        this.userUsername = userUsername;
    }

    public String getUserID() {
        return userID;
    }

    public String getUserEmail() {
        return userEmail;
    }

    public String getUserName() {
        return userName;
    }

    public String getUserUsername() {
        return userUsername;
    }

    public void setUserID(String userID) {
        this.userID = userID;
    }

    public void setUserEmail(String userEmail) {
        this.userEmail = userEmail;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public void setUserUsername(String userUsername) {
        this.userUsername = userUsername;
    }
}

主要活动

//ASSIGNING FIREBASE OBJECTS.
mAuth = FirebaseAuth.getInstance();
mFire = FirebaseDatabase.getInstance();
user = mAuth.getCurrentUser();
userID = user.getUid();

mDatabase = mFire.getReference("users");
usernameRef = mDatabase.child(userID);


//METHODS
checkIfUserSignedIN();
InitImgLoader();
setProfileImg();



//NAVIGATION LAYOUT.
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);



//HEADER VIEW LAYOUT.
username = findViewById(R.id.Username_onDrawer);
View headView = navigationView.getHeaderView(0);
ImageView pp = headView.findViewById(R.id.profile_image);
pp.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        Intent i = new Intent(MainActivity.this, ProfileActivity.class);
        startActivity(i);
    }
});

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


       for (DataSnapshot ds : dataSnapshot.child("userUsername").getChildren()){
            users usernamE = new users();
            usernamE.setUserUsername(ds.child(userID).getValue(users.class).getUserUsername());


            username.setText(usernamE.getUserUsername());
        }

    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

I'm trying to set username below this image view

**更新!***强文***** 只有使用以下代码才能拥有这样的数据库...

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


                String UserUserName = String.valueOf(dataSnapshot.child("userUsername").getValue());


                username.setText(UserUserName);

            }

database with No user id

这是我将数据保存到firebase的方式

 //USER SIGNUP INFORMATION METHOD.
private void push(){

    String Email = email.getText().toString().trim();
    String Name = name.getText().toString().trim();
    String Username = userName.getText().toString().trim();

    String id = mDatabase.push().getKey();

    users Users = new users(id, Email, Name, Username);

    mDatabase.child(id).setValue(Users);

2 个答案:

答案 0 :(得分:0)

如果您要获取用户名,可以执行以下操作:

usernameRef = mDatabase.child(userID);
usernameRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {

        String names=dataSnapshot.child("userUsername").getValue().toString();
        username.setText(names);


}

快照将是userid,然后你不需要迭代,只能访问它,因为它是一个直接的孩子。

答案 1 :(得分:0)

如果我是你,我使用uid作为唯一标识,而不是push()方法生成的推送密钥。因此,请更改以下代码行:

String id = mDatabase.push().getKey();
users Users = new users(id, Email, Name, Username);
mDatabase.child(id).setValue(Users);

使用:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
users Users = new users(uid, Email, Name, Username);
mDatabase.child(uid).setValue(Users);

要从数据库获取数据,请使用String类:

使用以下代码
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("users").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String userEmail = dataSnapshot.child("userEmail").getValue(String.class);
        String userID = dataSnapshot.child("userID").getValue(String.class);
        String userName = dataSnapshot.child("userName").getValue(String.class);
        String userUsername = dataSnapshot.child("userUsername").getValue(String.class);
        Log.d("TAG", userEmail + " / " + userID + " / " + userName + " / " + userUsername);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);

如果您想使用模型类,请使用以下代码:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("users").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        users u = dataSnapshot.getValue(users.class);
        String userEmail = u.getUserEmail();
        String userID = u.getUserID();
        String userName = u.getUserName();
        String userUsername = u.getUserUsername();
        Log.d("TAG", userEmail + " / " + userID + " / " + userName + " / " + userUsername);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);

在这两种情况下,请删除您的所有数据并添加新的数据。