gpt4 book ai didi

android - 访问 firebase 回收器适配器中的子节点

转载 作者:行者123 更新时间:2023-11-30 00:51:22 24 4
gpt4 key购买 nike

我有我的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);

我的export.json是

 "admin" : {
"ZpMBDchTkPVh7El3EsIKEdmV28n1" : {
"bio" : "",
"conversations" : {
"Chinwendu Chiebidolu" : {
"-KYCaWdm7IJrF-yau0ES" : {
"message" : "hello",
"rfirstname" : "Chinwendu",
"rimgUrl" : "",
"rlastname" : "Chiebidolu",
"rname" : "Chinwendu Chiebidolu",
"timestamp" : 1480918181229,
"userId" : "lgsmNM2LXuVq8NBoDwDCaoe5B613"
}
},
"Damilola Aromiwe" : {
"-KYBzfTc5xHCPGM06QIE" : {
"message" : "hi",
"rfirstname" : "Damilola",
"rimgUrl" : "2124",
"rlastname" : "Aromiwe",
"rname" : "Damilola Aromiwe",
"timestamp" : 1480907995851,
"userId" : "ujNyb5erkSN7oS3aGZlXyc9Q0YJ2"
},
"-KYCYYkifzI-61dBKsTT" : {
"message" : "hello",
"rfirstname" : "Damilola",
"rimgUrl" : "2124",
"rlastname" : "Aromiwe",
"rname" : "Damilola Aromiwe",
"timestamp" : 1480917404523,
"userId" : "ujNyb5erkSN7oS3aGZlXyc9Q0YJ2"
},
"-KYCZ19NM2yKinU5eSLt" : {
"message" : "hello mr dammy, are you there? ",
"rfirstname" : "Damilola",
"rimgUrl" : "2124",
"rlastname" : "Aromiwe",
"rname" : "Damilola Aromiwe",
"timestamp" : 1480917527734,
"userId" : "ujNyb5erkSN7oS3aGZlXyc9Q0YJ2"
}
}
},
"email" : "toyosi@gmail.com",
"firstname" : "Toyosi",
"imgUrl" : "2124",
"lastname" : "Yusuf",
"name" : "toyosi",
"parent" : true,
"phone" : "",
"teacher" : false,
"userId" : "ZpMBDchTkPVh7El3EsIKEdmV28n1"
}
},
"teachers" : {
"inFa52OMArdKyUTMYrkRqWs50uD2" : {
"bio" : "",
"email" : "david@gmail.com",
"firstname" : "David",
"imgUrl" : "",
"lastname" : "Akinyemi",
"name" : "",
"parent" : false,
"phone" : "",
"teacher" : true,
"userId" : "inFa52OMArdKyUTMYrkRqWs50uD2"
},
"xITyS4TrbWXPBhHYAgz0ZsdyYFa2" : {
"bio" : "",
"email" : "ejura@gmail.com",
"firstname" : "Ejura",
"imgUrl" : "",
"lastname" : "Ayeni",
"name" : "",
"parent" : false,
"phone" : "",
"teacher" : true,
"userId" : "xITyS4TrbWXPBhHYAgz0ZsdyYFa2"
}
},
"users" : {
"HJ55v7xUGmXQN2o1AQFDG9JtzCy1" : {
"bio" : "",
"email" : "aurora@gmail.com",
"firstname" : "",
"imgUrl" : "",
"lastname" : "",
"name" : "",
"parent" : true,
"phone" : "",
"teacher" : false,
"userId" : "HJ55v7xUGmXQN2o1AQFDG9JtzCy1"
},
"KrVp7D6gLoZoH6WcHT5tmyqb3o53" : {
"bio" : "",
"email" : "akinyemidvd@gmail.com",
"firstname" : "",
"imgUrl" : "",
"lastname" : "",
"name" : "",
"parent" : true,
"phone" : "",
"teacher" : false,
"userId" : "KrVp7D6gLoZoH6WcHT5tmyqb3o53"
},
"lgsmNM2LXuVq8NBoDwDCaoe5B613" : {
"bio" : "",
"conversations" : {
"Toyosi Yusuf" : {
"-KYCaWdHGbWxsk8h7EWy" : {
"message" : "hello",
"sfirstname" : "Toyosi",
"simageurl" : "2124",
"slastname" : "Yusuf",
"timestamp" : 1480918179544
}
}
},
"email" : "chinwe@gmail.com",
"firstname" : "Chinwendu",
"imgUrl" : "",
"lastname" : "Chiebidolu",
"name" : "",
"parent" : true,
"phone" : "",
"teacher" : false,
"userId" : "lgsmNM2LXuVq8NBoDwDCaoe5B613"
},
"ujNyb5erkSN7oS3aGZlXyc9Q0YJ2" : {
"bio" : "",
"conversations" : {
"Toyosi Yusuf" : {
"-KYBzfT_p8N2oW7oqSke" : {
"message" : "hi",
"sfirstname" : "Toyosi",
"simageurl" : "2124",
"slastname" : "Yusuf",
"timestamp" : 1480907995531
},
"-KYCYYkfORU70Y1_PGug" : {
"message" : "hello",
"sfirstname" : "Toyosi",
"simageurl" : "2124",
"slastname" : "Yusuf",
"timestamp" : 1480917401926
},
"-KYCZ19KyX8X5G25CEz5" : {
"message" : "hello mr dammy, are you there? ",
"sfirstname" : "Toyosi",
"simageurl" : "2124",
"slastname" : "Yusuf",
"timestamp" : 1480917527335
}
}
},
"email" : "dammyaro@gmail.com",
"firstname" : "Damilola",
"imgUrl" : "2124",
"lastname" : "Aromiwe",
"name" : "",
"parent" : true,
"phone" : "",
"teacher" : false,
"userId" : "ujNyb5erkSN7oS3aGZlXyc9Q0YJ2"
}
}
}

chatList.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 数据库 的工作原理。完整的文档是 here ,您应该阅读该自述文件以了解其工作原理。

populateViewHolder 方法的工作原理几乎与 onChildAdded/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);

...
}

顺便说一句,您的 ChatList 模型类缺少属性,或者属性不反射(reflect)数据库中的键,因此某些方法可能返回 null 值。 Read and Write Data on Android

关于android - 访问 firebase 回收器适配器中的子节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40970983/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com