本文介绍了如何从一个用户ID获取数据Firebase Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 在我的firebase数据库中,我有用户列表下有不同的信息。我想从一个userId中检索信息,我将在代码中定义,我只想从该用户那里获取信息。 我的数据看起来像; UserTable {fmXWU324VHdDb8v6h7gvNjRojnu33:{-KbJtw467nl6p253HZ537:{Name:name1,Email:[email protected],userid:fmXWU324VHdDb8v6h7gvNjRojnu33} },pJtC45fW0MMi352UiPWnWdIS7h88:{-Kb012ls9iMnzEL723o9I:{Name:name2,Email:[email protected], userid:pJtC45fW0MMi352UiPWnWdIS7h88},-Kb0aq0q25FJq38256SrC:{Name:name3,Email:[email protected], userid:pJtC45fW0MMi352UiPWnWdIS7h88},-Kb0atopfK64F6jy124Qi1u:{Name:name3,Email:something1 @ something.com,userid:p JtC45fW0MMi352UiPWnWdIS7h88} 我想说并显示该用户的所有数据fmXWU324VHdDb8v6h7gvNjRojnu33。我不知道我该怎么做。我试过了,这似乎是合乎逻辑的,但似乎并不奏效: df.child(UserTable)。child (user.getUid()))。addChildEventListener(new ChildEventListener() 它给出这个错误: / p> 无法将java.lang.String类型的对象转换为com.android.example.UserTable 另外,我也试过: c $ c> df.child(UsersTable)。child(userid)。child(user.getUid())。ad dChildEventListener(new ChildEventListener() 我似乎可以弄清楚我做错了什么,因为我所尝试过的似乎合乎逻辑,应该可以工作,请有人帮我解决。 (b) 编辑: pre $ public ArrayList< data> get(String userID){ df.child(UsersTable)。child(userID).ad dChildEventListener(new ChildEventListener(){ @Override public void onChildAdded(Data快照dataSnapshot,String s){ fetch(dataSnapshot); $ b @Override public void onChildChanged(DataSnapshot dataSnapshot,String s){ fetch(dataSnapshot); $ b $ @覆盖 public void onChildRemoved(DataSnapshot dataSnapshot){ } @Override public void onChildMoved(DataSnapshot dataSnapshot,String s){ } @Override public void onCancelled(DatabaseError databaseError){ } }); 返回数据; 更新 (DataSnapshot ds:dataSnapshot.getChildren())($ b $)$ private $ fetchData(DataSnapshot dataSnapshot) { b Userdetail userdetail = ds.getValue(Userdetail.class); userdetails.add(userdetail); $ / code> 解决方案像这样初始化数据库 $ $ $ $ $ $ $ $ mDatabase = FirebaseDatabase.getInstance()。getReference()。child(your_node_name); 然后设置 ValueEventListener 到您的数据库参考。 在 onDataChange 方法中,您可以像这样检索数据 public void onDataChange(DataSnapshot dataSnapshot){ User user = dataSnapshot.getValue(User.class); $ / code> 编辑: 用户用户= dataSnapshot.getValue(User.class){p $ p $ ValueEventListener postListener = new ValueEventListener(){ @Override public void onDataChange ); } @Override public void onCancelled(DatabaseError databaseError){ }; mDatabase.addValueEventListener(postListener); In my firebase database, I have list of users that have different information under them. I would like to retrieve information from a single userId, which I will define in the code and I want to only get information from that user.My data looks like;UserTable{ "fmXWU324VHdDb8v6h7gvNjRojnu33" : { "-KbJtw467nl6p253HZ537" : { "Name" : "name1", "Email" : "[email protected]", "userid" : "fmXWU324VHdDb8v6h7gvNjRojnu33" } }, "pJtC45fW0MMi352UiPWnWdIS7h88" : { "-Kb012ls9iMnzEL723o9I" : { "Name" : "name2", "Email" : "[email protected]", "userid" : "pJtC45fW0MMi352UiPWnWdIS7h88" }, "-Kb0aq0q25FJq38256SrC" : { "Name" : "name3", "Email" : "[email protected]", "userid" : "pJtC45fW0MMi352UiPWnWdIS7h88" }, "-Kb0atopfK64F6jy124Qi1u" : { "Name" : "name3", "Email" : "[email protected]", "userid" : "pJtC45fW0MMi352UiPWnWdIS7h88" }} and I want to, say for example read and display all data from this user "fmXWU324VHdDb8v6h7gvNjRojnu33". I am not sure how I can do that. I tried this, which seems logical but it doesn't seem to work:df.child("UserTable").child(user.getUid())).addChildEventListener(new ChildEventListener()it gives this error:Can't convert object of type java.lang.String to type com.android.example.UserTableIn addition, I have tried:df.child("UsersTable").child("userid").child(user.getUid()).addChildEventListener(new ChildEventListener() I can seem to figure out what I am doing wrong, as what I have tried seems logical and should work. Please can someone help me out.EDITED:public ArrayList<data> get(String userID){ df.child("UsersTable").child(userID).addChildEventListener(new ChildEventListener() { @Override public void onChildAdded(DataSnapshot dataSnapshot, String s) { fetch(dataSnapshot); } @Override public void onChildChanged(DataSnapshot dataSnapshot, String s) { fetch(dataSnapshot); } @Override public void onChildRemoved(DataSnapshot dataSnapshot) { } @Override public void onChildMoved(DataSnapshot dataSnapshot, String s) { } @Override public void onCancelled(DatabaseError databaseError) { }});return data;Updateprivate void fetchData(DataSnapshot dataSnapshot){ for (DataSnapshot ds : dataSnapshot.getChildren()) { Userdetail userdetail =ds.getValue(Userdetail.class); userdetails.add(userdetail ); }} 解决方案 First initialize your database like this mDatabase= FirebaseDatabase.getInstance().getReference().child("your_node_name");then try setting a ValueEventListener to your Database reference. Inside the onDataChange method you can retrieve your data like thispublic void onDataChange(DataSnapshot dataSnapshot) { User user= dataSnapshot.getValue(User.class);}EDIT: ValueEventListener postListener = new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { User user= dataSnapshot.getValue(User.class); } @Override public void onCancelled(DatabaseError databaseError) { }};mDatabase.addValueEventListener(postListener); 这篇关于如何从一个用户ID获取数据Firebase Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-18 14:10