要抬起头来,我没有足够的声誉来嵌入图像,因此我使用了链接。抱歉给您带来不便。
比方说,用户A在一个地方发布了一个游乐设施(第1趟),而另一个用户B在不同地方发布了2个游乐设施(第2骑行)。
现在,用户A想就骑乘2和骑乘3与用户B联系。他向每个游乐设施发送了不同的消息。
单击下面的链接以获取用户A和用户B之间的模型图像。
我描述应用的模型
我正在使用Firebase Realtime数据库,这是我的结构。
{
"Messages" : {
"-LeZUmoNdJq-7UfAmV2t" : {
"message" : " hey",
"receiver" : "z5bkSX2nGXUNmH1M86JYCpl5kqR2",
"rideid" : "-LaYv8que8JpNsisZWHA",
"sender" : "VT1c2whd1ygRFkcuql2JE6fvp8r1"
}
},
"Rides" : {
"-LaYv8que8JpNsisZWHA" : {
"date" : "2019-03-22",
"description" : "bbbbbbbbbbbbb",
"from" : "Windsor",
"image" : "Imagelink",
"price" : "11",
"to" : "Toronto",
"uid" : "z5bkSX2nGXUNmH1M86JYCpl5kqR2",
"z5bkSX2nGXUNmH1M86JYCpl5kqR2" : "true"
}
},
"Tokens" : {
"VT1c2whd1ygRFkcuql2JE6fvp8r1" : {
"token" : "dFPFysLrIGg:APA91bHK53tytelaYvonYobXDAk4d9cO5e8YH9UBXsags2j5B-NIpyPTD5UpulBYlIKUUH1DWbbZx6IAmjnxD7bt07fAIH-RtTI7mZvuQML13ySqqWr8tm9fRxIL6-9hEc6NdiP3Wc42"
}
},
"Users" : {
"9ALNuqjCy0ddQ13MDwmNHeWtX1j2" : {
"email" : "xyz@gmail.com",
"id" : "9ALNuqjCy0ddQ13MDwmNHeWtX1j2",
"name" : "XYZ"
}
}
}
我确实有一个代码在工作,但是问题是当我与同一用户但骑不同的人开始聊天时,它没有显示。
此外,我确实收到了ConcurrentModificationException,我不知道为什么。
private FirebaseUser currentUser;
private List<Ride> rideList;
private List<String> userListString;
private List<User> users;
currentUser = FirebaseAuth.getInstance().getCurrentUser();
reference = FirebaseDatabase.getInstance().getReference("Messages");
userListString = new ArrayList<>();
rideList = new ArrayList<>();
reference.addValueEventListener(new ValueEventListener()
{
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
{
userListString.clear();
Log.w("Messages Size:",dataSnapshot.getChildrenCount()+"");
for(DataSnapshot ds: dataSnapshot.getChildren())
{
Messages messages = ds.getValue(Messages.class);
if(messages.getSender().equals(currentUser.getUid()))
{
reference = FirebaseDatabase.getInstance().getReference("Rides").child(messages.getRideid());
reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
{
ride = dataSnapshot.getValue(Ride.class);
ride.setKey(dataSnapshot.getKey());
rideList.add(ride);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
userListString.add(messages.getReceiver());
}
if(messages.getReceiver().equals(currentUser.getUid()))
{
reference = FirebaseDatabase.getInstance().getReference("Rides").child(messages.getRideid());
reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
{
ride2 = dataSnapshot.getValue(Ride.class);
ride2.setKey(dataSnapshot.getKey());
rideList.add(ride2);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
{
}
});
userListString.add(messages.getSender());
}
}
readChats();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
{
}
});
private void readChats()
{
users = new ArrayList<>();
reference = FirebaseDatabase.getInstance().getReference("Users");
reference.addValueEventListener(new ValueEventListener()
{
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
{
users.clear();
for (DataSnapshot ds: dataSnapshot.getChildren())
{
User user = ds.getValue(User.class);
for (String id: userListString)
{
if(user.getId().equals(id))
{
if(users.size() != 0)
{
for(User user1: users)
{
if(!user.getId().equals(user1.getId()))
{
users.add(user);
}
}
}
else
{
users.add(user);
}
}
}
}
adapter = new MyMessagesAdapter(getContext(),users,getFragmentManager(),rideList);
myMessageRecyclerView.setAdapter(adapter);
if(adapter.getItemCount() == 0)
{
noMessageTextView.setVisibility(View.VISIBLE);
}
else
{
noMessageTextView.setVisibility(View.GONE);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
{
}
});
}