我有一个聊天应用程序,我正在我的应用程序上实现。我能够看到来自所有用户的聊天,但我真的不知道为什么我的应用程序在使用onDataChange尝试更新时获取重复聊天。当我点击我的按钮发送聊天时,fetch不会产生重复的onDataChange但是当我从不同的活动转移到聊天活动时,就是当fetch产生重复时
我做错了什么?
例如
Hi - 6.30pm
How are you 6.30pm
//duplicate on list view
Hi - 6.30pm
How are you - 6.30pm
这是我的代码
protected override void OnCreate(Bundle savedInstanceState)
{
button.Click += delegate {
PostMessage();
};
if(FirebaseAuth.Instance.CurrentUser == null){
StartActivityForResult(new Android.Content.Intent(this, typeof(LoginActivity)), MyResultCode);
}
else{
Toast.MakeText(this, "Welcome" +FirebaseAuth.Instance.CurrentUser.DisplayName, ToastLength.Short).Show();
DisplayChatMessages();
}
}
private async void PostMessage()
{
var items = await firebase.Child("").PostAsync(new MessageContent(FirebaseAuth.Instance.CurrentUser.DisplayName, edtChat.Text));
edtChat.Text = "";
}
public void OnCancelled(DatabaseError error)
{
}
public void OnDataChange(DataSnapshot snapshot)
{
DisplayChatMessages();
}
private async void DisplayChatMessages()
{
lstMessage.Clear();
var items = await firebase.Child("").OnceAsync<MessageContent>();
foreach (var item in items)
lstMessage.Add(item.Object);
ChatViewAdapter adapter = new ChatViewAdapter(this, lstMessage);
lstChat.Adapter = adapter;
}
}
}