如何获取选中的单选按钮的值,以便我能够在复选框形式的下一个活动中显示它们?

时间:2018-07-26 09:52:20

标签: android checkbox android-recyclerview

我正在开发一个测验应用程序,其中用户从每张卡中选择一个单选按钮(每张卡包含一个带有四个选项的问题),当用户单击 floatingActionButton 时,用户可以看到他/她在下一个活动中解决了什么。问题编号下面的复选框表示他已经解决了该问题。 我到处搜索,但找不到任何帮助。我被困在我的项目中。 这是我的代码

MainActivity.java

package com.pratyush.onlineexamapp;
public class MainActivity extends AppCompatActivity {
private ArrayList<ModelClass> questionList = new ArrayList<>();
private RecyclerView recyclerView;
FloatingActionButton fabactsbmt,fabctprv;
private QuestionAdapter qAdapter;
private static final String FORMAT = "%02d:%02d";
private RadioGroup rg;
private RadioButton rb;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    recyclerView = (RecyclerView) findViewById(R.id.rcyclvw);
    final TextView timer=(TextView)findViewById(R.id.timer) ;
    int selectedId=rg.getCheckedRadioButtonId();
    rb=(RadioButton)findViewById(selectedId);
    qAdapter= new QuestionAdapter(this);
    fabactsbmt=(FloatingActionButton)findViewById(R.id.sbmtBtn);
    fabctprv=(FloatingActionButton)findViewById(R.id.prevwBtn);
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext(),LinearLayoutManager.HORIZONTAL, false);
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(qAdapter);


    fabctprv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent it= new Intent(MainActivity.this,PreviewDetails.class);
            String selectedFromList = (recyclerView.getItemAtPosition(position));
            it.putIntegerArrayListExtra("Respone",);
            rg.clearCheck();
            startActivity(it);
        }
    });
    fabactsbmt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(MainActivity.this,"Your test will be submitted without taking you to Preview Activity",Toast.LENGTH_SHORT).show();
        }
    });

    new CountDownTimer(60000, 1000) { // adjust the milli seconds here

        public void onTick(long millisUntilFinished) {

            timer.setText("Time Left:" + " " + String.format(FORMAT,
                    TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(
                            TimeUnit.MILLISECONDS.toHours(millisUntilFinished)),
                    TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(
                            TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))) + "/01:00");
        }

        public void onFinish() {

            AlertDialog.Builder builder = new AlertDialog.Builder(
                    MainActivity.this);
            builder.setTitle("Time Out!!!");
            builder.setMessage("Your Score is" + " " + 100);
            builder.setCancelable(false);
           /* builder.setNegativeButton("NO",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                            int which) {
                            Toast.makeText(getApplicationContext(),"No is clicked",Toast.LENGTH_LONG).show();
                        }
                    });*/
            builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    System.exit(0);
                }
            });
            builder.show();
        }
    }.start();

    prepareTest();
}
public void prepareTest(){

    ModelClass que1=new ModelClass();
    que1.setQueNo("Question:- 1/11");
    que1.setQue("who is the prime minister of india?");
    que1.setOptn1(" Manmohan Singh");
    que1.setOptn2(" Sonia Gandhi");
    que1.setOptn3(" Narendra Modi");
    que1.setOptn4(" Amit Shah");
    que1.setAnswer("Narendra Modi");
    questionList.add(que1);

    ModelClass que2=new ModelClass();
    que2.setQueNo("Question:- 2/11");
    que2.setQue("who is the firs prime minister of india?");
    que2.setOptn1("a) Manmohan Singh");
    que2.setOptn2("b) Sonia Gandhi");
    que2.setOptn3("c) Narendra Modi");
    que2.setOptn4("d) Rajeev Gandhi");
    questionList.add(que2);

    ModelClass que3=new ModelClass();
    que3.setQueNo("Question:- 3/11");
    que3.setQue("who is the president of india?");
    que3.setOptn1("a) Manmohan Singh");
    que3.setOptn2("b) Sonia Gandhi ");
    que3.setOptn3("c) Narendra Modi");
    que3.setOptn4("d) Pratibha Patil");
    questionList.add(que3);

    ModelClass que4=new ModelClass();
    que4.setQueNo("Question:- 4/11");
    que4.setQue("Nobel prize is awarded for which of the following disciplines:");
    que4.setOptn1("a) Literacy,physics");
    que4.setOptn2("b) Chemistry");
    que4.setOptn3("c) Medicine or Physiology");
    que4.setOptn4("d) All of the Above");
    questionList.add(que4);

    ModelClass que5=new ModelClass();
    que5.setQueNo("Question:- 5/11");
    que5.setQue("Garampani Sanctuary is locate in which of the following places:");
    que5.setOptn1("a) Junagarh, Gujarat");
    que5.setOptn2("b)  Kohima, Nagaland");
    que5.setOptn3("c) Diphu, Assam");
    que5.setOptn4("d) Gangtok, Sikkim");
    questionList.add(que5);

    ModelClass que6=new ModelClass();
    que6.setQueNo("Question:- 6/11");
    que6.setQue(" Entomology studies what?");
    que6.setOptn1("a) Behavior of human beings");
    que6.setOptn2("b) Insects");
    que6.setOptn3("c) The origin and history of technical and scientific terms");
    que6.setOptn4("d) The formation of rocks");
    questionList.add(que6);

    ModelClass que7=new ModelClass();
    que7.setQueNo("Question:- 7/11");
    que7.setQue("Galileo was an astronomer who");
    que7.setOptn1("a) developed the telescope");
    que7.setOptn2("b) discovered four satellites of Jupiter");
    que7.setOptn3("c) discovered that the movement of pendulum produces a regular time measurement");
    que7.setOptn4("d) All the above");
    questionList.add(que7);

    ModelClass que8=new ModelClass();
    que8.setQueNo("Question:- 8/11");
    que8.setQue("Who is the father of geometry?");
    que8.setOptn1("a) Aristotle");
    que8.setOptn2("b) Pythagoras");
    que8.setOptn3("c) Euclid");
    que8.setOptn4("d) Kepler");
    questionList.add(que8);

    ModelClass que9=new ModelClass();
    que9.setQueNo("Question:- 9/11");
    que9.setQue("Indian Player Jude Felix is associated with");
    que9.setOptn1("a) Volleyball");
    que9.setOptn2("b) Football");
    que9.setOptn3("c) Hockey");
    que9.setOptn4("d) Tennis");
    questionList.add(que9);

    ModelClass que10=new ModelClass();
    que10.setQueNo("Question:- 10/11");
    que10.setQue("The Indian, who holds the pride of beating the computers in mathematical wizard is:");
    que10.setOptn1("a) Shakuntala Devi");
    que10.setOptn2("b) Raja Ramanna");
    que10.setOptn3("c) Ramanujam");
    que10.setOptn4("d) Rina Panigrahi");
    questionList.add(que10);

    ModelClass que11=new ModelClass();
    que11.setQueNo("Question:- 11/11");
    que11.setQue("Who is popularly called as the Iron Man of India?");
    que11.setOptn1("a) Subhash Chandra Bose");
    que11.setOptn2("b) Sardar Vallabhbhai Patel");
    que11.setOptn3("c) Govind Ballabh Pant");
    que11.setOptn4("d) Jawaharlal Nehru");
    questionList.add(que11);

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


    Context context;
    public QuestionAdapter(Context context) {
        this.context = context;

    }

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

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(QuestionAdapter.MyViewHolder holder, int position) {

        ModelClass model = questionList.get(position);

        holder.queNo.setText(model.getQueNo());
        holder.que.setText(model.getQue());
        holder.opt1.setText(model.getOptn1());
        holder.opt2.setText(model.getOptn2());
        holder.opt3.setText(model.getOptn3());
        holder.opt4.setText(model.getOptn4());
       // Picasso.with(context).load(model.getImg()).into(holder.imgvw);




    }

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


    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView queNo,que,opt1,opt2,opt3,opt4;


        public MyViewHolder(View view) {
            super(view);
            //RadioGroup radioGroup=(RadioGroup)findViewById(R.id.rgrp);
            //RadioButton checkedButton = (RadioButton)findViewById(radioGroup.getCheckedRadioButtonId());
            queNo = (TextView) view.findViewById(R.id.qNotext);
            que = (TextView) view.findViewById(R.id.queText);
            opt1 = (RadioButton) view.findViewById(R.id.opt1);
            opt2 = (RadioButton) view.findViewById(R.id.opt2);
            opt3 = (RadioButton) view.findViewById(R.id.opt3);
            opt4 = (RadioButton) view.findViewById(R.id.opt4);
        }
    }
}

这是我的预览活动。java

package com.pratyush.onlineexamapp;
public class PreviewDetails extends AppCompatActivity {
private ArrayList<Model> modelArrayList= new ArrayList<>();
GridView gridView;
FloatingActionButton finalSubmit;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_preview_details);
    gridView=(GridView)findViewById(R.id.gridView);
    finalSubmit=(FloatingActionButton)findViewById(R.id.finalsbmt);
    prepareData();

    ResponseAdapter adapter=new ResponseAdapter(this);
    gridView.setAdapter(adapter);

    finalSubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(PreviewDetails.this,"Your Test will be submitted",Toast.LENGTH_SHORT).show();
        }
    });



}
public void prepareData(){
    Model qno1=new Model();
    qno1.setQnumber("Question no.1");
    qno1.setCb(true);
    modelArrayList.add(qno1);

    Model qno2=new Model();
    qno2.setQnumber("Question no.2");
    qno2.setCb(true);
    modelArrayList.add(qno2);

    Model qno3=new Model();
    qno3.setQnumber("Question no.3");
    qno3.setCb(false);
    modelArrayList.add(qno3);

    Model qno4=new Model();
    qno4.setQnumber("Question no.4");
    qno4.setCb(true);
    modelArrayList.add(qno4);

    Model qno5=new Model();
    qno5.setQnumber("Question no.5");
    qno5.setCb(false);
    modelArrayList.add(qno5);

    Model qno6=new Model();
    qno6.setQnumber("Question no.6");
    qno6.setCb(true);
    modelArrayList.add(qno6);

    Model qno7=new Model();
    qno7.setQnumber("Question no.7");
    qno7.setCb(true);
    modelArrayList.add(qno7);

    Model qno8=new Model();
    qno8.setQnumber("Question no.8");
    qno8.setCb(true);
    modelArrayList.add(qno8);

    Model qno9=new Model();
    qno9.setQnumber("Question no.9");
    qno9.setCb(false);
    modelArrayList.add(qno9);

    Model qno10=new Model();
    qno10.setQnumber("Question no.10");
    qno10.setCb(true);
    modelArrayList.add(qno10);

    Model qno11=new Model();
    qno11.setQnumber("Question no.11");
    qno11.setCb(true);
    modelArrayList.add(qno11);
}
private class ResponseAdapter extends BaseAdapter{

    private Context context;
    private LayoutInflater layoutInflater;

    private ResponseAdapter( Context context){
        this.context=context;

    }

    @Override
    public int getCount() {
        return modelArrayList.size();
    }

    @Override
    public Object getItem(int i) {
        return modelArrayList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (layoutInflater==null)
            layoutInflater=(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        if (view==null)
            view=layoutInflater.inflate(R.layout.custom_grid,null);
        TextView tv=(TextView) view.findViewById(R.id.tetView);
        CheckBox checkBox=(CheckBox) view.findViewById(R.id.checkboxe);

        tv.setText(modelArrayList.get(i).getQnumber());
        checkBox.setChecked(modelArrayList.get(i).getCb());

        return view;
    }
}

现在我只是在复选框中手动设置了这些值。谢谢!!!

2 个答案:

答案 0 :(得分:0)

您可以从广播组中获取选定的项目。一个单选组由一些单选按钮组成。

    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            switch (i){
                case R.id.ans1:
                    //may be your answer is correct.
                    //Do existing works!
                    //Save the data to the database.
                    // Retrive the data from the database in the next activity(Intent)
            }
        }
    });

答案 1 :(得分:0)

好吧,这是一个类,其中的数据将从Firebase数据库中检索,并且ar beign的问题由用户解决,然后再次上载到Firebase……可能会帮助您:

id01