所以我正在使用firebase构建聊天应用程序。我得到它将消息添加到Firebase数据库。因此消息被推送但我无法在RecyclerView上显示消息历史记录或新发送的消息。我搜索过,大多数答案是使用adapter.startListening()添加onStart(),使用adapter.stopListening()添加onStop()。这对我没有用。任何建议表示赞赏:)
的build.gradle(APP)
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
//firebase
implementation 'com.google.firebase:firebase-messaging:12.0.1'
compile 'com.google.firebase:firebase-core:12.0.1'
compile 'com.google.firebase:firebase-auth:12.0.1'
compile 'com.google.firebase:firebase-database:12.0.1'
compile 'com.firebaseui:firebase-ui-database:3.1.0'
}
apply plugin: 'com.google.gms.google-services'
用于聊天活动的XML
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerViewMessages"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_above="@+id/editTextMessage"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<EditText
android:id="@+id/editTextMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@+id/buttonSend"
android:layout_toStartOf="@+id/buttonSend"
android:ems="10"
android:inputType="textPersonName"
android:hint="Type Message"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="467dp" />
<Button
android:id="@+id/buttonSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/editTextMessage"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="84dp"
android:layout_marginRight="84dp"
android:text="Send"
tools:layout_editor_absoluteX="297dp"
tools:layout_editor_absoluteY="465dp" />
聊天活动
public class UserChat extends AppCompatActivity {
private RecyclerView recyclerView;
private EditText editTextMessageTyped;
private Button buttonSend;
private FirebaseAuth firebaseAuth;
private FirebaseRecyclerAdapter<Message, ChatHolder> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_chat);
buttonSend = (Button) findViewById(R.id.buttonSend);
buttonSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendMessage();
}
});
Query query = FirebaseDatabase.getInstance().getReference("Chat").child("messageUser");
FirebaseRecyclerOptions<Message> options = new FirebaseRecyclerOptions.Builder<Message>()
.setQuery(query, Message.class)
.setLifecycleOwner(this)
.build();
adapter = new FirebaseRecyclerAdapter<Message, ChatHolder>(options) {
@Override
public ChatHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_message, parent, false);
return new ChatHolder(view);
}
@Override
protected void onBindViewHolder(ChatHolder holder, int position, Message model) {
}
};
adapter.notifyDataSetChanged();
recyclerView = (RecyclerView) findViewById(R.id.recyclerViewMessages);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(adapter);
}
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
private void sendMessage() {
editTextMessageTyped = (EditText) findViewById(R.id.editTextMessage);
// Read the input field and push a new instance of ChatMessage to the Firebase database
FirebaseDatabase.getInstance().getReference("Chat").push()
.setValue(new Message(FirebaseAuth.getInstance().getCurrentUser().getEmail(),
editTextMessageTyped.getText().toString().trim()));
// Clear the input editText
editTextMessageTyped.setText(" ");
}
}
消息模型类
public class Message {
private String MessageUser, MessageBody;
private long timestamp;
public Message(String messageUser, String messageBody) {
this.MessageUser = messageUser;
this.MessageBody = messageBody;
// Initialize to current time
this.timestamp = new Date().getTime();
}
public Message() {
}
public String getMessageUser() {
return MessageUser;
}
public String getMessageBody() {
return MessageBody;
}
public Long getTimestamp() {
return timestamp;
}
}
聊天持有人类
public class ChatHolder extends RecyclerView.ViewHolder {
private TextView textViewMessageBody, textViewMessageTime;
public ChatHolder(View itemView) {
super(itemView);
textViewMessageBody = (TextView) itemView.findViewById(R.id.textViewMessageBody);
textViewMessageTime = (TextView) itemView.findViewById(R.id.textViewMessageTime);
}
public void blind(Message message) {
setTextViewMessageBody(message.getMessageBody());
setTextViewMessageTime("9:40");
}
public void setTextViewMessageBody(String messageBody) {
textViewMessageBody.setText(messageBody);
}
public void setTextViewMessageTime(String messageTime) {
textViewMessageTime.setText(messageTime);
}
}
XML item_Message
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textViewMessageBody"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="MessageBody" />
<TextView
android:id="@+id/textViewMessageTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/textViewMessageBody"
android:text="Time" />
</RelativeLayout>
Firebase JSON
{
"Chat" : {
"-LDba09F6-kJeQXC9XIq" : {
"messageBody" : "cc",
"messageUser" : "abc2@abc.com",
"timestamp" : 1527525151377
},
"-LDbaS2uc6M8tp4Lbcar" : {
"messageBody" : "nn",
"messageUser" : "abc2@abc.com",
"timestamp" : 1527525265659
},
"-LDbaSmSLVF_g3UWLlNh" : {
"messageBody" : "ee",
"messageUser" : "abc1@abc.com",
"timestamp" : 1527525268637
},
"-LDbb1xtj7w8ohPfeTgr" : {
"messageBody" : "oo",
"messageUser" : "abc2@abc.com",
"timestamp" : 1527525420926
},
"-LDbkI7--YXP_VPpg3fG" : {
"messageBody" : "jj",
"messageUser" : "abc2@abc.com",
"timestamp" : 1527527846402
}
}
}
发生了什么的屏幕截图