我有RecyclerView
TextView
,其中包含一个问题和四个RadioButton
个答案。了解RadioButton
是否是正确答案的最佳方式是什么?我如何计算有多少答案是正确的?
这是我的RecyclerView.Adapter
课程:
public class QuestionarioAdapter extends RecyclerView.Adapter <QuestionarioAdapter.MyViewHolder> {
private List<Questionario> questionarioList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView perg;
public RadioButton alt1, alt2, alt3,alt4;
public MyViewHolder(View view) {
super(view);
perg = (TextView) view.findViewById(R.id.tvQuest);
alt1 = (RadioButton) view.findViewById(R.id.alt1);
alt2 = (RadioButton) view.findViewById(R.id.alt2);
alt3 = (RadioButton) view.findViewById(R.id.alt3);
alt4 = (RadioButton) view.findViewById(R.id.alt4);
}
}
public QuestionarioAdapter(List<Questionario> questionarioList) {
this.questionarioList = questionarioList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.questionario_perguntas, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Questionario questionario = questionarioList.get(position);
holder.perg.setText(questionario.getQuestao());
holder.alt1.setText(questionario.getAlt1());
holder.alt2.setText(questionario.getAlt2());
holder.alt3.setText(questionario.getAlt3());
holder.alt4.setText(questionario.getAlt4());
}
@Override
public int getItemCount() {
return questionarioList.size();
}
}
这是我的List<>
课程:
public class Questionario {
private String questao, alt1, alt2, alt3, alt4, altCerta;
public Questionario(String questao, String alt1, String alt2, String alt3, String alt4, String altCerta) {
this.questao = questao;
this.alt2 = alt2;
this.alt1 = alt1;
this.alt3 = alt3;
this.alt4 = alt4;
this.altCerta = altCerta;
}
public String getQuestao() {
return questao;
}
public void setQuestao(String questao) {
this.questao = questao;
}
public String getAlt1() {
return alt1;
}
public void setAlt1(String alt1) {
this.alt1 = alt1;
}
public String getAlt2() {
return alt2;
}
public void setAlt2(String alt2) {
this.alt2 = alt2;
}
public String getAlt3() {
return alt3;
}
public void setAlt3(String alt3) {
this.alt3 = alt3;
}
public String getAlt4() {
return alt4;
}
public void setAlt4(String alt4) {
this.alt4 = alt4;
}
public String getAltCerta() {
return altCerta;
}
public void setAltCerta(String altCerta) {
this.altCerta = altCerta;
}
}
这是显示问题的MainClass
:
private List<Questionario> questionarioList = new ArrayList<>();
private RecyclerView recyclerView;
private QuestionarioAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_perguntas);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mAdapter = new QuestionarioAdapter(questionarioList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new Divisor(this, LinearLayoutManager.VERTICAL));
recyclerView.setAdapter(mAdapter);
prepareQuestionario();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_perguntas, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void prepareQuestionario() {
Questionario questionario = new Questionario("Pergunta 1", "Alt 1","Alt 2","Alt 3","Alt 4", "1");
questionarioList.add(questionario);
questionario = new Questionario("Pergunta 2", "Alt 1","Alt 2","Alt 3","Alt 4", "2");
questionarioList.add(questionario);
questionario = new Questionario("Pergunta 3", "Alt 1","Alt 2","Alt 3","Alt 4", "3");
questionarioList.add(questionario);
questionario = new Questionario("Pergunta 4", "Alt 1","Alt 2","Alt 3","Alt 4", "1");
questionarioList.add(questionario);
questionario = new Questionario("Pergunta 5", "Alt 1","Alt 2","Alt 3","Alt 4", "2");
questionarioList.add(questionario);
questionario = new Questionario("Pergunta 6", "Alt 1","Alt 2","Alt 3","Alt 4", "3");
questionarioList.add(questionario);
questionario = new Questionario("Pergunta 7", "Alt 1","Alt 2","Alt 3","Alt 4", "4");
questionarioList.add(questionario);
questionario = new Questionario("Pergunta 8", "Alt 1","Alt 2","Alt 3","Alt 4", "2");
questionarioList.add(questionario);
questionario = new Questionario("Pergunta 9", "Alt 1","Alt 2","Alt 3","Alt 4", "2");
questionarioList.add(questionario);
questionario = new Questionario("Pergunta 10", "Alt 1","Alt 2","Alt 3","Alt 4", "2");
questionarioList.add(questionario);
questionario = new Questionario("Pergunta 11", "Alt 1","Alt 2","Alt 3","Alt 4", "1");
questionarioList.add(questionario);
questionario = new Questionario("Pergunta 12", "Alt 1","Alt 2","Alt 3","Alt 4", "2");
questionarioList.add(questionario);
questionario = new Questionario("Pergunta 13", "Alt 1","Alt 2","Alt 3","Alt 4", "4");
questionarioList.add(questionario);
questionario = new Questionario("Pergunta 14", "Alt 1","Alt 2","Alt 3","Alt 4", "3");
questionarioList.add(questionario);
questionario = new Questionario("Pergunta 15", "Alt 1","Alt 2","Alt 3","Alt 4", "3");
questionarioList.add(questionario);
mAdapter.notifyDataSetChanged();
}
}
我将创建一个计时器并获得答案。但是如何从RadioButton
获得正确答案?
答案 0 :(得分:0)
首先,在RecyclerView.Adapter
类中创建这些变量,如下所示:
private int question = 1;
private int correct;
private int incorrect;
为每个onClickListener
创建RadioButton
,如下所示:
holder.alt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (question == 1) {
// let's say this is correct
question++;
correct++;
}
else if (question == 2) {
// let's say this is incorrect
question++;
incorrect++;
}
// ...
}
});
确保为每个RadioButton
编写上述代码,并确保使用了每个问题。看你怎么走。