Android 访问firebase回收器适配器中的子节点
我有我的Android 访问firebase回收器适配器中的子节点,android,firebase,firebase-realtime-database,Android,Firebase,Firebase Realtime Database,我有我的firebaseRecyclerAdapter,它的populateViewHolder如下 Query queryRef = chatRef.orderByKey(); queryRef.addChildEventListener(new ChildEventListener() { @Override public void onChildAdded(DataSnapshot snapshot, String previousChild) { Syst
firebaseRecyclerAdapter
,它的populateViewHolder
如下
Query queryRef = chatRef.orderByKey();
queryRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot snapshot, String previousChild) {
System.out.println(snapshot.getKey());
for (DataSnapshot snapshot1 : snapshot.getChildren()) {
final String firstname = snapshot.getKey();
chatViewHolder.setName(firstname);
chatViewHolder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Log.w(TAG, "You clicked on "+position);
//String firstname = tList.getFirstname();
//String lastname = tList.getLastname();
//firstname = firstname.substring(0, 1).toUpperCase() + firstname.substring(1); //convert first string to uppercase
//lastname = lastname.substring(0, 1).toUpperCase() + lastname.substring(1);// same thing happening here
//String name = (firstname + " " + lastname); // concatenate firstname and lastname variable.
Intent intent = new Intent(getActivity(), MainChat.class); //change to onclick
intent.putExtra("fullname", firstname);
//you can name the keys whatever you like
//intent.putExtra("image", userList.getImgUrl().toString()); //note that all these values have to be primitive (i.e boolean, int, double, String, etc.)
startActivity(intent);
}
});
}
这是用回收器适配器备份的,我的数据库看起来像
users
uid
conversations
sLname + sFname
-KyF........:
message: hi
timestamp: ServerValue.timestamp
-KyFr......:
message:hello
timestamp: ServerValue.timestamp
firebaseRecyclerAdapter的实现
RecyclerView recycler = (RecyclerView) rootView.findViewById(R.id.recyclerview4);
recycler.setHasFixedSize(true);
recycler.setLayoutManager(new LinearLayoutManager(getActivity()));
FirebaseRecyclerAdapter mAdapter = new FirebaseRecyclerAdapter<ChatList, ChatHolder>(ChatList.class, R.layout.chatlistrow, ChatHolder.class, chatRef) {
@Override
public void populateViewHolder(final ChatHolder chatViewHolder, final ChatList chatList, final int position) {
//try catch block to catch events of no posts, it will most likely return a null error, so I'm catching it, else
//find its exception and catch itef
contactList = new ArrayList<String>();
//start here and getkey for everyone
contactList.add(chatRef.getKey());
//String firstname = chatRef.getKey();
//chatViewHolder.setName(firstname);
Query queryRef = chatRef.orderByKey();
queryRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot snapshot, String previousChild) {
System.out.println(snapshot.getKey());
for (DataSnapshot snapshot1 : snapshot.getChildren()) {
final String firstname = snapshot.getKey();
chatViewHolder.setName(firstname);
chatViewHolder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Log.w(TAG, "You clicked on "+position);
Intent intent = new Intent(getActivity(), MainChat.class); //change to onclick
intent.putExtra("fullname", firstname);
//you can name the keys whatever you like
//intent.putExtra("image", userList.getImgUrl().toString()); //note that all these values have to be primitive (i.e boolean, int, double, String, etc.)
startActivity(intent);
}
});
}
//testing
}
@Override
public void onChildChanged(DataSnapshot snapshot, String previousChild) {
}
@Override
public void onChildMoved(DataSnapshot snapshot, String previousChild) {
}
@Override
public void onChildRemoved(DataSnapshot snapshot) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
// ....
});
}
};
recycler.setAdapter(mAdapter);
class()指向此处
package com.mordred.theschoolapp;
/**
* Created by mordred on 11/29/16.
*/
public class ChatList {
public String userId;
public String rname;
public String rlastname;
public String rimageurl;
public ChatList() {
// Default constructor required for calls to DataSnapshot.getValue(User.class)
}
public String getFirstname() {
return rname;
}
public void setFirstname(String firstname) {
this.rname = firstname;
}
public String getLastname() {
return rlastname;
}
public void setLastname(String lastname) {
this.rlastname = lastname;
}
public String getImgUrl() {
return rimageurl;
}
public void setImgUrl(String imgUrl) {
this.rimageurl = imgUrl;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
}
这里的问题是,我试图在回收器适配器中递归地检索这些消息。
但是由于前面的代码在上面,我只是不断地重复第一条消息。
有人能告诉我发生了什么事,我怎样才能修复它吗?如果你需要更多的信息,尽管问。谢谢看起来您不明白
Firebase UI数据库
是如何工作的。完整的文档是,您应该阅读该自述文件以了解其工作原理
populateViewHolder
方法的工作原理与onchildaded/onChildChanged
几乎相同,它返回引用节点下的每个子节点
所以应该是这样的
chatRef = FirebaseDatabase.getInstance().getReference("users").child(uid).child("conversations");
...
@Override
public void populateViewHolder(final ChatHolder chatViewHolder, final ChatList chatList, final int position) {
contactList = new ArrayList<String>();
// this is what you want
String fullName = mAdapter.getRef(position).getKey();
contactList.add(fullName);
...
}
chatRef=FirebaseDatabase.getInstance().getReference(“用户”).child(uid).child(“对话”);
...
@凌驾
public void populateViewHolder(最终聊天室持有者聊天室视图持有者、最终聊天室列表聊天室列表、最终int位置){
contactList=新的ArrayList();
//这就是你想要的
字符串fullName=mAdapter.getRef(position.getKey();
联系人列表。添加(全名);
...
}
顺便说一下,您的
ChatList
模型类缺少属性,或者属性没有反映数据库中的键,因此某些方法可能返回null
值 我不明白你的问题是什么。您能否发布FirebaseRecyclerAdapter
的实现,指出适配器侦听数据库的节点,以及chatRef
指向的位置。另外,您在数据库中的sLname+sFname
是什么意思?最好发布它的确切JSON格式,只需从Firebase数据库控制台导出JSON即可。谢谢,我刚刚更新了我的问题,所以chatRef
指向此节点users///conversations///code>?是的,它指向这里非常感谢。但我完全理解如何做到这一点,但这里的问题是,这不是我想要返回的,我想要返回对话的直接子对象。不是那些。那么chatRef
指向的是用户//对话//
不是用户//对话//
@Mordredeyes,我想回复每一次谈话,谢谢。我正要把你的答案标记为正确,但我注意到mAdapter一直说firebaseRecyclerAdapter需要被宣布为最终版本。当我宣布它是最终的,它说它从未初始化过
chatRef = FirebaseDatabase.getInstance().getReference("users").child(uid).child("conversations");
...
@Override
public void populateViewHolder(final ChatHolder chatViewHolder, final ChatList chatList, final int position) {
contactList = new ArrayList<String>();
// this is what you want
String fullName = mAdapter.getRef(position).getKey();
contactList.add(fullName);
...
}