我一直有这个问题。我已经构建了一个聊天应用程序,但是当我尝试发送消息时,它会复制屏幕上的消息。
我正在使用Firebase数据库从云中提取数据,但是,只在屏幕上复制,当我查看数据库上的消息时,它们是正常的。
这是我的问题的截图。
当我点击发送时,它会复制邮件。
这是我的Chat.class
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.util.Log;
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);
bringMessageFromCloud();
}
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);
}
bringMessageFromCloud();
messageArea.setText("");
}
public void bringMessageFromCloud() {
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) {
}
});
}
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);
}
}
方法sendMessage是发送邮件图标的OnClickListener。
有人可以帮我吗?
答案 0 :(得分:0)