【问题标题】:The RecyclerView doesn't activate, and I was wondering whyRecyclerView 没有激活,我想知道为什么
【发布时间】:2019-10-27 12:55:31
【问题描述】:

我自己一直在为我的大部分应用程序编写代码,但在使用 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();

                }
            }
        });

【问题讨论】:

    标签: java android android-studio android-recyclerview


    【解决方案1】:

    你的问题不是 recyclerView 而是你的数据库。

    类的字段将使用反射填充。但是你不能在没有构造函数的情况下创建一个“默认”对象(意思是:没有预先填充的字段)。 Firebase 无法自己弄清楚你的构造函数做了什么,所以你需要一个空的构造函数(来源:Why we need an empty Constructor to passing/save a Data From Firebase?

    【讨论】:

      【解决方案2】:

      @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;
          }   
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-09-06
        • 1970-01-01
        • 2019-06-22
        • 2011-06-24
        • 2016-07-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多