Я сам писал код для большей части своего приложения, но когда дело дошло до использования RecyclerView
, я остановился. Оттуда я следовал учебному пособию, но когда я пытаюсь использовать программу, RecycclerView
не работает. Я получаю сообщение:
LessonPost не определяет конструктор без аргументов.
Класс адаптера LessonRecyclerAdaptor.java
public class LessonRecyclerAdaptor extends RecyclerView.Adapter<LessonRecyclerAdaptor.ViewHolder> {
public List<LessonPost> lesson_list;
public LessonRecyclerAdaptor (List<LessonPost> lesson_list) {
this.lesson_list = lesson_list;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.lesson_single_layout, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
String desc_data = lesson_list.get(position).getDescription();
holder.setDescriptionText(desc_data);
}
@Override
public int getItemCount() {
return lesson_list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private View mView;
private TextView recycler_description;
public ViewHolder(@NonNull View itemView) {
super(itemView);
mView = itemView;
}
public void setDescriptionText(String text){
recycler_description = mView.findViewById(R.id.lesson_description);
recycler_description.setText(text);
}
}
}
Класс модели LessonPost (откуда список получает свои значения)
public class LessonPost {
private String userID;
private String Lesson_name;
private String Lesson_image;
private String Lesson_description;
public LessonPost(String userID, String lessonName, String imageURL, String description) {
this.userID = userID;
this.Lesson_name = lessonName;
this.Lesson_image = imageURL;
this.Lesson_description = description;
}
public String getUserID() {
return userID;
}
public void setUserID(String userID) {
this.userID = userID;
}
public String getLessonName() {
return Lesson_name;
}
public void setLessonName(String lessonName) {
this.Lesson_name = lessonName;
}
public String getLesson_imageL() {
return Lesson_image;
}
public void setLesson_image(String imageURL) {
this.Lesson_image = imageURL;
}
public String getDescription() {
return Lesson_description;
}
public void setDescription(String Lesson_description) {
this.Lesson_description = Lesson_description;
}
}
И вызов от Main
lesson_list_main = findViewById(R.id.lesson_list_view);
lesson_list = new ArrayList<>();
lessonRecyclerAdaptor = new LessonRecyclerAdaptor(lesson_list);
lesson_list_main.setLayoutManager(new LinearLayoutManager(MainActivity.this));
lesson_list_main.setAdapter(lessonRecyclerAdaptor);
if (firestoreCheck.collection("Lessons").get() == null) {
Toast.makeText(MainActivity.this, "There are no lessons yet", Toast.LENGTH_LONG).show();
} else {
firestoreCheck.collection("Lessons").document(userID).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
firestoreCheck.collection("Lessons").addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {
if (doc.getType() == DocumentChange.Type.ADDED) {
LessonPost lessonPostMain = doc.getDocument().toObject(LessonPost.class);
lesson_list.add(lessonPostMain);
lessonRecyclerAdaptor.notifyDataSetChanged();
}
}
}
});
} else {
Toast.makeText(MainActivity.this, "Error hah ", Toast.LENGTH_LONG).show();
}
}
});