RecyclerView 是空白的。我想念什么?

RecycleView is blank. What do i miss?

我不明白我在 prepareQuestion 方法中遗漏了什么。 MainActivity 启动但我的 RecycleView 是空的。一开始,在 MainActivity 中,我有问题和单选按钮,当按下时间 运行 或 "Confirm/Next" 按钮时,下一个问题和选择正在加载。我决定尝试使用 RecycleView 和 Cards 在 MainActivity 中一次加载所有问题。

public class MainActivity 扩展 AppCompatActivity {

private QuestionAdapter adapter;
private ArrayList<Question> questionList;
private int questionCountTotal;
private long backPressedTime;

public QuizDbHelper dbHelper;
RecyclerView recyclerView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recyclerView = findViewById(R.id.recycler_view);
    questionList = new ArrayList<>();
    adapter = new QuestionAdapter(this, questionList);

    RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this, 1);
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(adapter);

    dbHelper = new QuizDbHelper(this);
    questionList = dbHelper.getAllQuestions();
    questionCountTotal = questionList.size();
    Collections.shuffle(questionList);


    prepareQuestion();


}


private void prepareQuestion() {

    //Tried different things here. Maybe problem is somewhere else?
    Question q = new Question();
    questionList.add(q);
    //        questionList.addAll(dbHelper.getAllQuestions());
    adapter.notifyDataSetChanged();



}

}

这是我的 QuestionAdapter

public class QuestionAdapter extends RecyclerView.Adapter<QuestionAdapter.MyViewHolder> {

private ArrayList<Question> questionList;

public class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView tvQuestion, tvScore, tvQuestionCount, tvCountdown;
    public RadioGroup rbGroup;
    public RadioButton rb1, rb2, rb3, rb4;



    public MyViewHolder(View view) {
        super(view);
        tvQuestion = view.findViewById(R.id.question);
        tvScore = view.findViewById(R.id.score);
        tvQuestionCount = view.findViewById(R.id.question_count);
        tvCountdown = view.findViewById(R.id.count_down);
        rbGroup = view.findViewById(R.id.radiog_group);
        rb1 = view.findViewById(R.id.radio_button1);
        rb2 = view.findViewById(R.id.radio_button2);
        rb3 = view.findViewById(R.id.radio_button3);
        rb4 = view.findViewById(R.id.radio_button4);
    }
}


public QuestionAdapter(Context mContext, ArrayList<Question> questionList) {
    this.questionList = questionList;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.question_card, parent, false);

    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
    Question question = questionList.get(position);
    holder.tvQuestion.setText(question.getQuestion());
    holder.rb1.setText(question.getOption1());
    holder.rb2.setText(question.getOption2());
    holder.rb3.setText(question.getOption3());
    holder.rb4.setText(question.getOption4());
}

@Override
public int getItemCount() {
    return questionList.size();

How it was before Now instead of lot of cards blank screen

要一次加载所有问题,您应该将它们放入您的 activity 并将它们添加到您的问题列表

questionList.addAll(dbHelper.getAllQuestions());
adapter.notifyDataSetChanged();//to refresh view and show items ;-)

如果你想在你的适配器定义中获得点击监听器,你可以使用监听器,

在你的 preparequestion() 方法中 改变 adapter.notifyDataSetChanged();

adapter = new QuestionAdapter (getActivity(), questionList);
recyclerView.setAdapter(adapter);