使用模型类在聊天应用程序中附加文档时出现以下错误 错误
E / RecyclerView:未连接适配器;跳过布局
onCreate类中使用的RecyclerView代码
private RecyclerView recyclerView; //declaration
recyclerView = findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
linearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(linearLayoutManager);
在onCreate()中调用的方法是sendmessages和readMessages作为下面的代码
private void sendMessage(String message) {
Log.d("Check Message Sending", "First");
long timeStamp = System.currentTimeMillis();
String messageId = Long.toString(timeStamp);
String photourl = "12345464356", imageurl = "abcdefghijklmnopqr";
DocumentReference chatPathCurrent = mFireBaseFireStore.collection("Chats").document();
chatPathCurrent.set(new FriendlyMessage(message, currentUsername,photourl,imageurl ));
}
private void readMessages() {
mChat = new ArrayList<>();
CollectionReference usersPath = mFireBaseFireStore.collection("Chats");
usersPath.orderBy("timeStamp", Query.Direction.ASCENDING).addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot value,
@Nullable FirebaseFirestoreException e) {
if (e != null) {
return;
}
mChat.clear();
for (QueryDocumentSnapshot doc : value) {
FriendlyMessage message = doc.toObject(FriendlyMessage.class);
mChat.add(message);
chatAdapter = new ChatAdapter(context, mChat);
recyclerView.setAdapter(chatAdapter);
}
}
});
}
DataModel类
public class FriendlyMessage {
private String id;
private String text;
private String name;
private String photoUrl;
private String imageUrl;
public FriendlyMessage() {
}
public FriendlyMessage(String text, String name, String photoUrl, String imageUrl) {
this.text = text;
this.name = name;
this.photoUrl = photoUrl;
this.imageUrl = imageUrl;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public void setText(String text) {
this.text = text;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhotoUrl() {
return photoUrl;
}
public String getText() {
return text;
}
public void setPhotoUrl(String photoUrl) {
this.photoUrl = photoUrl;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
}
此聊天应用程序使用的ChatAdapter代码如下
public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.ViewHolder> implements Filterable {
private static final int MSG_TYPE_LEFT = 0;
private static final int MSG_TYPE_RIGHT = 1;
private static final int MSG_TYPE_IMAGE_LEFT = 2;
private static final int MSG_TYPE_IMAGE_RIGHT = 3;
private static final int MSG_TYPE_ATTACHMENT_LEFT = 4;
private static final int MSG_TYPE_ATTACHMENT_RIGHT = 5;
private Context mContext;
private List<FriendlyMessage> mChat;
private List<FriendlyMessage> mChatFull;
private boolean isSender;
private boolean darkModeOn;
public ChatAdapter(Context mContext, List<FriendlyMessage> mChat) {
this.mContext = mContext;
this.mChat = mChat;
mChatFull = new ArrayList<>(mChat);
//this.repository = repository;
// darkModeOn = sharedPreferences.getBoolean("darkModeOn", true);
}
@NonNull
@Override
public ChatAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (viewType == MSG_TYPE_LEFT) {
View view = LayoutInflater.from(mContext).inflate(R.layout.chat_item_left, parent, false);
return new ChatAdapter.ViewHolder(view);
} else if (viewType == MSG_TYPE_RIGHT) {
View view = LayoutInflater.from(mContext).inflate(R.layout.chat_item_right, parent, false);
return new ChatAdapter.ViewHolder(view);
} else if (viewType == MSG_TYPE_IMAGE_LEFT) {
View view = LayoutInflater.from(mContext).inflate(R.layout.image_item_left, parent, false);
return new ChatAdapter.ViewHolder(view);
} else if (viewType == MSG_TYPE_IMAGE_RIGHT){
View view = LayoutInflater.from(mContext).inflate(R.layout.image_item_right, parent, false);
return new ChatAdapter.ViewHolder(view);
} else if (viewType == MSG_TYPE_ATTACHMENT_LEFT) {
View view = LayoutInflater.from(mContext).inflate(R.layout.attachment_item_left, parent, false);
return new ChatAdapter.ViewHolder(view);
} else {
View view = LayoutInflater.from(mContext).inflate(R.layout.attachment_item_right, parent, false);
return new ChatAdapter.ViewHolder(view);
}
}
@Override
public void onBindViewHolder(@NonNull ChatAdapter.ViewHolder holder, int position) {
final FriendlyMessage message = mChat.get(position);
holder.textViewShowMessage.setText(message.getText());
}
@Override
public int getItemCount() {
return mChat.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView textViewShowMessage;
private TextView textViewTimeStamp;
private ImageView imageViewBlueTicks;
private ImageView imageViewGreyTicks;
private ImageView imageViewProfilePic;
private ImageView imageViewChatImage;
public ViewHolder(@NonNull View itemView) {
super(itemView);
textViewShowMessage = itemView.findViewById(R.id.show_message);
textViewTimeStamp = itemView.findViewById(R.id.text_view_time_stamp);
imageViewProfilePic = itemView.findViewById(R.id.profile_pic);
imageViewChatImage = itemView.findViewById(R.id.show_image);
}
}
@Override
public int getItemViewType(int position) {
return MSG_TYPE_RIGHT;
}
public static String getDate(long milliSeconds, String dateFormat) {
SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
return formatter.format(calendar.getTime());
}
private void getImage(final String imageUrl, final String fileName) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
Bitmap bitmap = getBitmapFromURL(imageUrl);
//External directory path to save file
String folder = Environment.getExternalStorageDirectory() + File.separator + "Hive/Images/";
//Create folder if it does not exist
File directory = new File(folder);
if (!directory.exists()) {
directory.mkdirs();
}
try {
FileOutputStream out = new FileOutputStream(folder + fileName + ".jpg");
if (bitmap != null) {
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
}
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
return BitmapFactory.decodeStream(input);
} catch (IOException e) {
return null;
}
}
private boolean fileExists(String fileName) {
String filePath = Environment.getExternalStorageDirectory() + File.separator + "Hive/Images/" + fileName + ".jpg";
File file = new File(filePath);
if (file.exists()) {
return true;
} else {
return false;
}
}
private void loadLocally(String fileName, ImageView imageView) {
String filePath = Environment.getExternalStorageDirectory() + File.separator + "Hive/Images/" + fileName + ".jpg";
File file = new File(filePath);
if (file.exists()) {
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
imageView.setImageBitmap(bitmap);
}
}
@Override
public Filter getFilter() {
return exampleFilter;
}
private Filter exampleFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
List<FriendlyMessage> filteredList = new ArrayList<>();
if (constraint == null || constraint.length() == 0) {
filteredList.addAll(mChatFull);
} else {
String filterPattern = constraint.toString().toLowerCase().trim();
for (FriendlyMessage message : mChatFull) {
if (message.getText().toLowerCase().contains(filterPattern)) {
filteredList.add(message);
}
}
}
FilterResults results = new FilterResults();
results.values = filteredList;
return results;
}
@Override
@SuppressWarnings("unchecked")
protected void publishResults(CharSequence constraint, FilterResults results) {
mChat.clear();
mChat.addAll((List) results.values);
notifyDataSetChanged();
}
};
}
如果需要更多详细信息,请告知我,我正在使用Firestore数据库和android java。