被困在通过Firebase获取名称的过程中。

huangapple 未分类评论51阅读模式
标题翻译

Stuck with fetching name through Firebase

问题

public class AccountActivity extends AppCompatActivity {
    private TextView showprofile;
    private TextView showemail;
    private DatabaseReference userprofilereference;
    private FirebaseAuth mAuth;
    private FirebaseUser user;
    private Button delete;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account);

        mAuth = FirebaseAuth.getInstance();
        user = mAuth.getCurrentUser();
        userprofilereference = FirebaseDatabase.getInstance().getReference().child(user.getUid());
        showemail = (TextView) findViewById(R.id.edit_emailp);
        showemail.setText(user.getEmail());
        showprofile = (TextView) findViewById(R.id.profile_name_textView);
        delete = (Button) findViewById(R.id.deleteaccount);

        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                deleteUser();
                startActivity(new Intent(AccountActivity.this, SignUpActivity.class));
            }
        });

        String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
        DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
        DatabaseReference uidRef = rootRef.child(uid);
        ValueEventListener eventListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                dataSnapshot.child("name");
                String name = dataSnapshot.getValue(String.class);
                showprofile.setText(name);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        };
        uidRef.addListenerForSingleValueEvent(eventListener);
    }
}
英文翻译
public
class AccountActivity extends AppCompatActivity {
    private
    TextView showprofile;
    private TextView showemail;
    private
    DatabaseReference userprofilereference;
    private FirebaseAuth mAuth;
    //    private String CurrentUserId;
    private
    FirebaseUser user;
    private Button delete;

    @Override
    protected
    void onCreate ( Bundle savedInstanceState ) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_account);

        mAuth = FirebaseAuth.getInstance ();
        user = mAuth.getCurrentUser ();
        userprofilereference = FirebaseDatabase.getInstance ().getReference ().child (user.getUid ());
        showemail = (TextView) findViewById (R.id.edit_emailp);
        showemail.setText (user.getEmail ());
        showprofile = (TextView) findViewById (R.id.profile_name_textView);
        delete = (Button) findViewById (R.id.deleteaccount);


        delete.setOnClickListener (new View.OnClickListener () {
            @Override
            public
            void onClick ( View view ) {
                deleteUser ();
                startActivity (new Intent (AccountActivity.this, SignUpActivity.class));

            }
        });
        String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
        DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
        DatabaseReference uidRef = rootRef.child(uid);
        ValueEventListener eventListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                dataSnapshot.child ("name");
                String name = dataSnapshot.getValue (String.class);
                showprofile.setText (name);
            }

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

    }

被困在通过Firebase获取名称的过程中。

答案1

得分: 0

根据您的数据库结构,不需要 uid。

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("Users");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String name = dataSnapshot.child("name").getValue(String.class);
        showprofile.setText(name);
    }
    
    @Override
    public void onCancelled(DatabaseError databaseError) {
        throw databaseError.toException(); // 永远不要忽略错误
    }
};
uidRef.addListenerForSingleValueEvent(eventListener);
英文翻译

There is no need of uid according to your database structure.

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("Users");
ValueEventListener eventListener = new ValueEventListener() {
	@Override
	public void onDataChange(DataSnapshot dataSnapshot) {
		String name = dataSnapshot.child("name").getValue(String.class);
		showprofile.setText (name);
	}

	@Override
	public void onCancelled(DatabaseError databaseError) { 
        throw databaseError.toException(); // never ignore errors
    }
};
uidRef.addListenerForSingleValueEvent(eventListener);

答案2

得分: 0

虽然Ashish的回答完全正确,但请注意您当前的数据库架构不正确,因为您只是在“Users”节点下添加了单个用户。在未来,每次您想要添加另一个用户时,所有数据都将被覆盖。因此,实际的解决方案是在“Users”节点与用户的实际数据之间添加另一层。您的新数据库架构可能如下所示:

Firebase根目录
   |
   --- Users
        |
        --- 1P7g ... 57RO(uid)
             |
             --- 邮箱:“aqsakarim@gmail.com”
             |
             --- id:“1P7g ... 57RO”
             |
             --- 姓名:“aqsa kari”
             |
             --- uid:“1P7g ... 57RO”

通过这种方式,您可以在“Users”节点中区分每个用户。话虽如此,您的实际代码将正常工作,因为我看到您在引用中使用了来自身份验证过程的那个uid。因此,为了阅读目的,您无需更改任何代码行。

编辑:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference usersRef = rootRef.collection("Users");
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DocumentReference uidRef = usersRef.document(uid);
uidRef.set(userObject);

其中userObject是包含所有这些属性的对象。

英文翻译

While Ashish's answer will perfectly fine, please note that your current database schema is not a correct one, since you are adding under a Users node only a single user. In the future, every time you want to add another user, all the data will be overridden. So the actual solution is to add another layer between the Users node and the actual data of the user. Your new database schema might look like this:

Firebase-root
   |
   --- Users
        |
        --- 1P7g ... 57RO (uid)
             |
             --- email: "aqsakarim@gmail.com"
             |
             --- id: "1P7g ... 57RO"
             |
             --- name: "aqsa kari"
             |
             --- uid: "1P7g ... 57RO"

In this way, you can distinguish each and every user within your Users node. That being said, your actual code will work, as I see you're using that uid that comes from the authentication process in your reference. So you don't have to change any line of code, for reading purposes.

Edit:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference usersRef = rootRef.collection("Users");
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DocumentReference uidRef = usersRef.document(uid);
uidRef.set(userObject);

In which userObject is the object that holds all those properties.

huangapple
  • 本文由 发表于 2020年3月16日 20:53:27
  • 转载请务必保留本文链接:https://java.coder-hub.com/60706392.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定