我是RecyclerView和Firebase概念的新手。我正在使用该聊天应用程序,该应用程序从用户(消息)获取输入,并使用DisplayName(Google登录,因此为Username)并将其存储到Firebase的实时数据库中。 “ mAuthor”和“ mMessage”的存储工作正常,当我尝试检索数据并将其显示在recyclerView中时,出现了问题。根据Logcat,没有崩溃错误,但是,我的聊天应用程序屏幕(在recyclerView中)没有任何显示。
firebase和recyclerView的依赖项已被仔细添加。
这是我的活动课:
private String mDisplayName;
private EditText mMessage;
private ImageView mSendButton;
private RecyclerView mRecycler;
private DatabaseReference mDatabaseReference;
private MyAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
mMessage=(EditText) findViewById(R.id.messageInput);
mSendButton=(ImageView) findViewById(R.id.sendButton);
setupDisplayName();
mDatabaseReference= FirebaseDatabase.getInstance().getReference();
mRecycler=(RecyclerView) findViewById(R.id.chat_recycler_view);
mRecycler.setHasFixedSize(true);
LinearLayoutManager layoutManager= new LinearLayoutManager(ChatActivity.this);
mRecycler.setLayoutManager(layoutManager);
mMessage.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
sendMessage();
return true;
}
});
mSendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendMessage();
}
});
}
private void setupDisplayName(){
Intent mIntent= getIntent();
String option= mIntent.getStringExtra("Options");
if(option.equals("normal")){
SharedPreferences prefs= getSharedPreferences(RegisterActivity.CHAT_PREFS,0);
mDisplayName=prefs.getString(RegisterActivity.DISPLAY_NAME_KEY,null);
Log.d("Google","Shared prefs: DisplayName:"+mDisplayName);
}
else if(option.equals("google")){
SharedPreferences prefs= getSharedPreferences(MainActivity.CHAT_PREFS,0);
mDisplayName=prefs.getString(MainActivity.DISPLAY_NAME_KEY,null);
Log.d("Google","Shared prefs: DisplayName:"+mDisplayName);
}
if(mDisplayName==null)
mDisplayName="Anonymous";
}
private void sendMessage() {
String input= mMessage.getText().toString();
if(!input.equals("")){
Log.d("Google","I sent something");
InstantMessage mObject= new InstantMessage(mDisplayName, input);
mDatabaseReference.child("messages").push().setValue(mObject);
mMessage.setText("");
}
}
@Override
public void onStart() {
super.onStart();
mAdapter= new MyAdapter(mDatabaseReference, mDisplayName);
mRecycler.setAdapter(mAdapter);
}
@Override
public void onStop(){
super.onStop();
mAdapter.CleanUp();
}
这是我的Model课:
public class InstantMessage {
private String mAuthor;
private String mMessage;
public InstantMessage(String mAuthor, String mMessage) {
this.mAuthor = mAuthor;
this.mMessage = mMessage;
}
public InstantMessage() {
}
public String getmAuthor() {
return mAuthor;
}
public void setmAuthor(String mAuthor) {
this.mAuthor = mAuthor;
}
public void setmMessage(String mMessage) {
this.mMessage = mMessage;
}
public String getmMessage() {
return mMessage;
}
}
这是我的适配器类:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private DatabaseReference mReference;
private String mDisplayName;
private List<InstantMessage> mList;
private ChildEventListener mListener= new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
InstantMessage msg= dataSnapshot.getValue(InstantMessage.class);
mList.add(msg);
Log.d("Google","Datasnapshot added:"+dataSnapshot.toString());
notifyDataSetChanged();
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
};
public MyAdapter( DatabaseReference mReference, String mDisplayName) {
this.mReference = mReference.child("messages");
mReference.addChildEventListener(mListener);
this.mDisplayName = mDisplayName;
mList= new ArrayList<>();
}
@NonNull
@Override
public MyAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_chat_row, parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyAdapter.ViewHolder holder, int position) {
holder.mAuthorName.setText(mList.get(position).getmAuthor());
holder.mText.setText(mList.get(position).getmMessage());
}
@Override
public int getItemCount() {
Log.d("Google","Item count:"+mList.size());
return mList.size();
}
public void CleanUp(){
mReference.removeEventListener(mListener);
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView mAuthorName;
TextView mText;
public ViewHolder(View itemView) {
super(itemView);
mAuthorName=(TextView) itemView.findViewById(R.id.Chatauthor);
mText= (TextView) itemView.findViewById(R.id.Chatmessage);
}
}
}
我不确定自己在做什么错。在onChildAdded方法的log语句中,datasnapshot.toString()返回合法值。但是,Log.d(“ Google”,“ Item Count:” + mList.size())每次都会返回ItemCount:1
PS:这是发送到Firebase的数据:https://imageshack.com/a/img922/1458/HoOWxO.png
我的activity_chat_row.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/Chatauthor"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_gravity="start"
android:textColor="#3CB371"
android:gravity="center_vertical"
android:textSize="15sp"
android:textStyle="bold"
android:text="Author"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/Chatmessage"
android:layout_gravity="start"
android:gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:padding="10dp"
android:textSize="15sp"
android:text="Text"
android:textColor="@android:color/primary_text_light"
/>
</LinearLayout>