gpt4 book ai didi

android - 如何设置添加 child 时调用的onChildAdded

转载 作者:行者123 更新时间:2023-11-29 15:37:02 26 4
gpt4 key购买 nike

public class ChatsFragment extends Fragment {

FirebaseDatabase database;
DatabaseReference myRef;
List<ListData> list;
RecyclerView recyclerview;
TextView badge;
String name, message, nameCaps;
int id, user_id;
DatabaseHelper databaseHelper;

public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle b) {
View view = inflater.inflate(R.layout.fragment_chats, group, false);
recyclerview = view.findViewById(R.id.rview);
badge = view.findViewById(R.id.badge);
database = FirebaseDatabase.getInstance();
myRef = database.getReference("message");
list = new ArrayList<>();
databaseHelper = new DatabaseHelper(getContext());
myRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
UserDetails userdetails = dataSnapshot.getValue(UserDetails.class);
name = userdetails.getName();
message = userdetails.getMessage();
id = userdetails.getId();
user_id = userdetails.getUser_id();
nameCaps = name.substring(0, 1).toUpperCase() + name.substring(1);
if (databaseHelper.checkUserID(user_id)) {
databaseHelper.updateMsg(nameCaps, message, user_id);
} else {
databaseHelper.insertData(nameCaps, message, id, user_id);
}
list = databaseHelper.getData();
RecyclerViewAdapter recycler = new RecyclerViewAdapter(list);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
recyclerview.setLayoutManager(layoutManager);
recyclerview.setItemAnimator(new DefaultItemAnimator());
recyclerview.setAdapter(recycler);
}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
UserDetails userdetails = dataSnapshot.getValue(UserDetails.class);
name = userdetails.getName();
message = userdetails.getMessage();
id = userdetails.getId();
nameCaps = name.substring(0, 1).toUpperCase() + name.substring(1);
databaseHelper.updateData(nameCaps, message, id);

}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
UserDetails userdetails = dataSnapshot.getValue(UserDetails.class);
id = userdetails.getId();
databaseHelper.deleteData(id);
}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});
return view;
}

这是我的代码。当添加一个 child 时,我将其保存在 sqlite 数据库中,并从数据库中获取数据并加载到 recyclerview 中,但是 onChildAdded() 方法调用每个当我打开应用程序时。如何设置当我添加一个 child 然后调用 onChildAdded() 否则它不会调用 onChildAdded()

最佳答案

没有这种查找方式,因为它总是返回给您 Firebase 上存在的所有相关数据。根据我的研究和知识,没有解决方案可以限制它。

有一种方法可以做到这一点:

对我来说,逻辑是要有值——例如“status”——在决定它是真的新记录还是旧记录之前需要对其进行验证,然后我将“status”设置为不同的值,这样我就不会下次得到它:

@Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildKey) {

if(dataSnapshot.hasChildren()) {

String Id = (String) dataSnapshot.child("user_id").getValue();
String status = (String) dataSnapshot.child("status").getValue();

if (Id != null && Id.equals(storedId) && status != null && status.equals("created")) {
Log.d("INCOMING_REQUEST", "This is for you!");
sessionsRef.child(dataSnapshot.getKey()).child("status").setValue("received");
}

}


}

谢谢,如果需要更多请告诉我!!

关于android - 如何设置添加 child 时调用的onChildAdded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47709576/

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