我正在使用firebase的聊天应用程序,我想将用户信息fitch到recyclerview…当我将适配器设置为回收器视图时,我的代码是正确的,它显示一个错误。下面是我的代码:
package com.abdelatif.chatapp;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
import com.firebase.ui.firestore.FirestoreRecyclerAdapter;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
import com.squareup.picasso.Picasso;
public class ChatFragment extends Fragment {
private FirebaseFirestore firebaseFirestore;
LinearLayoutManager linearLayoutManager;
private FirebaseAuth firebaseAuth;
ImageView mimageviewofuser;
FirestoreRecyclerAdapter<FireBaseModel, NoteViewHolder> chatAdapter = null;
RecyclerView mrecyclerview;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.chatfragment,container,false);
firebaseAuth=FirebaseAuth.getInstance();
firebaseFirestore= FirebaseFirestore.getInstance();
mrecyclerview=v.findViewById(R.id.recyclerView);
// Query query=firebaseFirestore.collection("Users");
Query query=firebaseFirestore.collection("Users").whereNotEqualTo("uid",firebaseAuth.getUid());
FirestoreRecyclerOptions<FireBaseModel> allusername=new FirestoreRecyclerOptions.Builder<FireBaseModel>().setQuery(query,FireBaseModel.class).build();
chatAdapter=new FirestoreRecyclerAdapter<FireBaseModel, NoteViewHolder>(allusername) {
@Override
protected void onBindViewHolder(@NonNull NoteViewHolder noteViewHolder, int i, @NonNull FireBaseModel firebasemodel) {
noteViewHolder.particularusername.setText(firebasemodel.getName());
String uri=firebasemodel.getImage();
Picasso.get().load(uri).into(mimageviewofuser);
if(firebasemodel.getStatus().equals("Online"))
{
noteViewHolder.statusofuser.setText(firebasemodel.getStatus());
noteViewHolder.statusofuser.setTextColor(Color.GREEN);
}
else
{
noteViewHolder.statusofuser.setText(firebasemodel.getStatus());
}
noteViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getActivity(), "contactClicked", Toast.LENGTH_SHORT).show();
}
});
}
@NonNull
@Override
public NoteViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_view_layout,parent,false);
return new NoteViewHolder(view);
}
};
mrecyclerview.setHasFixedSize(true);
linearLayoutManager=new LinearLayoutManager(getContext());
linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
mrecyclerview.setLayoutManager(linearLayoutManager);
mrecyclerview.setAdapter(chatAdapter);
chatAdapter.startListening();
return v;
}
public class NoteViewHolder extends android.support.v7.widget.RecyclerView.ViewHolder
{
private TextView particularusername;
private TextView statusofuser;
public NoteViewHolder(@NonNull View itemView) {
super(itemView);
particularusername=itemView.findViewById(R.id.nameOfUser);
statusofuser=itemView.findViewById(R.id.statusOfUser);
mimageviewofuser=itemView.findViewById(R.id.imageViewOfUser);
}
}
}
错误在行:mrecyclerview.setAdapter(chatAdapter);
,错误为:'setAdapter(androidx.recyclerview.widget. recyclerview. adapter)' in 'androidx.recyclerview.widget. 'RecyclerView'不能应用于'(com.firebase.ui.firestore.FirestoreRecyclerAdapter<com.abdelatif.chatapp.FireBaseModel,com.abdelatif.chatapp.ChatFragment.NoteViewHolder>)'
我希望有人给我一个解决方案…我找了好多次都没找到。
确保在呼叫mrecyclerview.setAdapter(chatAdapter);
之前先呼叫chatAdapter.startListening();