RecyclerView无法激活,我想知道为什么

时间:2019-10-27 12:55:31

标签: java android android-studio android-recyclerview

我一直在为自己的大多数应用程序编写代码,但是当涉及到使用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();

                }
            }
        });

2 个答案:

答案 0 :(得分:0)

您的问题不是recyclerView,而是您的数据库。

  

该类的字段将使用反射填充。但是,如果没有构造函数,则无法创建“默认”对象(意味着:没有预填充任何字段)。 Firebase无法自行弄清楚构造函数的作用,所以这就是为什么您需要一个空的构造函数的原因(来源:Why we need an empty Constructor to passing/save a Data From Firebase?

答案 1 :(得分:0)

@Charlie是正确的,firebase使用了一个空的反射构造函数b / c。您需要做类似的事情

public class LessonPost {

   // first constructor, used by firebase
   public LessonPost() {
     // ... empty constructor
   }

   // second constructor, used in another place of your app
   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;
    }   

}