我不明白我的prepareQuestion方法中缺少什么。 MainActivity启动,但是我的RecycleView为空。一开始,在MainActivity中,我有问题和单选按钮,当时间用完或按下“确认/下一个”按钮时,下一个问题就会载入选择。我决定尝试使用RecycleView和Cards在MainActivity中一次加载所有问题。
公共类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();
答案 0 :(得分:1)
立即加载所有问题,您应该将其加入活动并将其添加到您的QuestionList
questionList.addAll(dbHelper.getAllQuestions());
adapter.notifyDataSetChanged();//to refresh view and show items ;-)
如果您想在适配器定义中获得Click监听器,则可以使用监听器,
答案 1 :(得分:0)
更改adapter.notifyDataSetChanged();
到
adapter = new QuestionAdapter (getActivity(), questionList);
recyclerView.setAdapter(adapter);