我真的很想立刻从Firebase中回复邮件。我已经构建了一个聊天应用程序,但历史记录仅在发送消息后下载。当我打开聊天活动时,他们不下载。有人可以帮我吗?
下面你可以看到我的Chat类负责完成所有逻辑。
package br.sosqueen.com.sosqueen;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.GenericTypeIndicator;
import com.google.firebase.database.ValueEventListener;
import java.util.HashMap;
import java.util.Map;
/**
* Created by calvin.
*/
public class Chat extends AppCompatActivity {
private LinearLayout layout;
private EditText messageArea;
private ScrollView scrollView;
DatabaseReference reference1 = FirebaseDatabase.getInstance()
.getReferenceFromUrl("https://sosqueen-6b80b.firebaseio.com/" + UserDetails.username + "_" + UserDetails.chatWith);
DatabaseReference reference2 = FirebaseDatabase.getInstance()
.getReferenceFromUrl("https://sosqueen-6b80b.firebaseio.com/" + UserDetails.chatWith + "_" + UserDetails.username);
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
layout = (LinearLayout) findViewById(R.id.layout1);
messageArea = (EditText) findViewById(R.id.messageArea);
scrollView = (ScrollView) findViewById(R.id.scrollView);
getSupportActionBar().setTitle(UserDetails.chatWith);
public void sendMessage(View view) {
String messageText = messageArea.getText().toString();
if(! messageText.equals("")){
Map<String, String> map = new HashMap<String, String>();
map.put("message", messageText);
map.put("user", UserDetails.username);
reference1.push().setValue(map);
reference2.push().setValue(map);
}
reference1.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
GenericTypeIndicator<Map<String, String>> genericTypeIndicator = new GenericTypeIndicator<Map<String, String>>() {};
Map<String, String> map = dataSnapshot.getValue(genericTypeIndicator );
String message = map.get("message").toString();
String userName = map.get("user").toString();
if(userName.equals(UserDetails.username)){
addMessageBox(message, 1);
}
else{
addMessageBox(message, 2);
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
messageArea.setText("");
}
public void addMessageBox(String message, int type) {
TextView textView = new TextView(Chat.this);
textView.setText(message);
LinearLayout.LayoutParams linearP = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
linearP.setMargins(10, 0, 0, 10);
textView.setLayoutParams(linearP);
textView.setTypeface(Typeface.create("sans-serif-thin", Typeface.NORMAL));
textView.setTextSize(20);
if(type == 1) {
linearP.gravity = Gravity.END;
textView.setTextColor(Color.WHITE);
textView.setBackgroundResource(R.drawable.rounded_corner1);
textView.setPadding(20,20,20,20);
scrollView.post(new Runnable()
{
public void run()
{
scrollView.fullScroll(View.FOCUS_DOWN);
}
});
} else {
textView.setTextColor(Color.BLACK);
textView.setBackgroundResource(R.drawable.rounded_corner2);
textView.setPadding(20,20,20,20);
scrollView.post(new Runnable()
{
public void run()
{
scrollView.fullScroll(View.FOCUS_DOWN);
}
});
}
layout.addView(textView);
}
}
这是我使用Firebase创建聊天逻辑的类。 问题是,当我点击发送消息时,所有历史记录都会出现,但是当我打开聊天活动时,他们不会下载。
答案 0 :(得分:0)
要检索您必须使用的所有数据ValueEventListener
而不是ChildEventListener
。
值事件侦听器将检索DatabaseReference
变量中指定位置的所有数据。
请注意,ValueEventListener
将始终检索给定位置的所有孩子,即使只有一个孩子发生了更改,因此,如果您希望这种情况只发生一次,请使用addListenerForSingleValueEvent
。
对于更新,我认为您可以保留当前的代码。
请参阅:https://firebase.google.com/docs/reference/android/com/google/firebase/database/ValueEventListener
并且:https://firebase.google.com/docs/reference/android/com/google/firebase/database/ChildEventListener