我正在设计在线测验App。我设计了PlayQuiz.java文件如下:
public class PlayQuiz extends AppCompatActivity {
private RecyclerView recyclerView;
DataBaseHelper database;
private List quizList;
private QuizAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_quiz);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
recyclerView=(RecyclerView)findViewById(R.id.recycler_view_quiz_display);
database= new DataBaseHelper(PlayQuiz.this);
quizList= database.fillObjQuesList();
adapter=new QuizAdapter(quizList,getApplicationContext());
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
});
}
}
现在,这是我的QuizAdapter.java文件
public class QuizAdapter extends RecyclerView.Adapter{
private List questionList;
private Context context;
public QuizAdapter(List questionList, Context context) {
this.questionList = questionList;
this.context = context;
}
@NonNull
@Override
public CustomViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView= LayoutInflater.from(parent.getContext()).inflate(R.layout.quiz_display_format,parent,false);
return new CustomViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull final CustomViewHolder holder, final int position) {
DmQuiz questionsList=questionList.get(position);
holder.tvquestion.getLayoutParams().width= LinearLayout.LayoutParams.WRAP_CONTENT;
holder.tvquestion.setText(questionsList.getQuestion());
holder.optA.setText(questionsList.getOpta());
holder.optB.setText(questionsList.getOptb());
holder.optC.setText(questionsList.getOptc());
holder.optD.setText(questionsList.getOptd());
holder.optA.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.optA.setBackgroundColor(context.getResources().getColor(R.color.colorButton));
holder.optA.setBackgroundResource(R.drawable.button_border); holder.optB.setBackgroundResource(R.drawable.button_border_unselected); holder.optC.setBackgroundResource(R.drawable.button_border_unselected); holder.optD.setBackgroundResource(R.drawable.button_border_unselected);
}
});
holder.optB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.optA.setBackgroundResource(R.drawable.button_border_unselected);
holder.optB.setBackgroundResource(R.drawable.button_border);
holder.optC.setBackgroundResource(R.drawable.button_border_unselected);
holder.optD.setBackgroundResource(R.drawable.button_border_unselected);
}
});
holder.optC.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.optA.setBackgroundResource(R.drawable.button_border_unselected);
holder.optB.setBackgroundResource(R.drawable.button_border_unselected);
holder.optC.setBackgroundResource(R.drawable.button_border);
holder.optD.setBackgroundResource(R.drawable.button_border_unselected);
}
});
holder.optD.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.optA.setBackgroundResource(R.drawable.button_border_unselected);
holder.optB.setBackgroundResource(R.drawable.button_border_unselected);
holder.optC.setBackgroundResource(R.drawable.button_border_unselected);
holder.optD.setBackgroundResource(R.drawable.button_border);
}
});
holder.tvClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.optA.setBackgroundResource(R.drawable.button_border_unselected);
holder.optB.setBackgroundResource(R.drawable.button_border_unselected);
holder.optC.setBackgroundResource(R.drawable.button_border_unselected);
holder.optD.setBackgroundResource(R.drawable.button_border_unselected);
}
});
}
@Override
public int getItemCount() {
return questionList.size();
}
public class CustomViewHolder extends RecyclerView.ViewHolder{
TextView tvquestion, tvClear;
Button optA,optB,optC,optD;
public CustomViewHolder(View itemView) {
super(itemView);
tvquestion=(TextView)itemView.findViewById(R.id.tvQuestion);
optA=(Button)itemView.findViewById(R.id.button1);
optB=(Button)itemView.findViewById(R.id.button2);
optC=(Button)itemView.findViewById(R.id.button3);
optD=(Button)itemView.findViewById(R.id.button4);
tvClear=(TextView)itemView.findViewById(R.id.tvClear);
}
}
public QuizAdapter(List questionList)
{
this.questionList=questionList;
}
public void setSearchOperation(List newList){
questionList= new ArrayList<>();
questionList.addAll(newList);
notifyDataSetChanged();
}
}
Recyclerview中的数据正在从SQLite数据库中显示。
现在我想处理/存储多个按钮上的点击并将其发送到远程MySql服务器。我很困惑,如何存储不同问题的回复?我应该使用ArrayList或其他东西,请帮助......