如何从viewflipper中不同布局内的单选按钮值获取值

时间:2014-04-02 08:16:18

标签: android viewflipper radio-group

我正在使用视图鳍状肢创建一个小测验,其中包含4个问题和答案。我使用了radiobutton和小组来回答问题。我已经在viewflipper中使用4个布局进行4个问题并立即回答我的问题是我可以从第一个问题获得第一个问题的值,但我无法从其他布局中的其他问题中得到答案。有人帮我这个。我在下面发布了我的代码

提前致谢。

public class Comptest extends Activity implements OnClickListener
{
    //urls

    String url="**************************.php";

    //json array

    JSONArray jarray;

    //Array list

    ArrayList<String> c_alquestion = new ArrayList<String>();

    ArrayList<String> c_alcorrectanswer = new ArrayList<String>();

    ArrayList<String> c_alwronganswer1 = new ArrayList<String>();

    ArrayList<String> c_alwronganswer2 = new ArrayList<String>();

    ArrayList<String> c_alwronganswer3 = new ArrayList<String>();

    ArrayList<String> c_alwronganswer4 = new ArrayList<String>();

    ArrayList<String> c_alwronganswer5 = new ArrayList<String>();

    //quiz array list

    ArrayList<String> q_question=new ArrayList<String>();

    ArrayList<String> temp=new ArrayList<String>();

    ArrayList<String> ansl=new ArrayList<String>();
    ArrayList<String> wans1=new ArrayList<String>();
    ArrayList<String> wans2=new ArrayList<String>();
    ArrayList<String> wans3=new ArrayList<String>();
    ArrayList<String> wans4=new ArrayList<String>();

    int count=0;

    //PagerAdapter tst_padapter;

    int id1,id2,id3,id4;

    ViewFlipper viewFlipper;

    Button Next, Previous;

    Animation slide_in_left, slide_out_right;

    //Textview questions

    TextView qst1,qst2,qst3,qst4;
    //Radio button

    RadioButton rb1_1,rb1_2,rb1_3,rb1_4,rb1_5,rb1_6;
    RadioButton rb2_1,rb2_2,rb2_3,rb2_4,rb2_5,rb2_6;
    RadioButton rb3_1,rb3_2,rb3_3,rb3_4,rb3_5,rb3_6;
    RadioButton rb4_1,rb4_2,rb4_3,rb4_4,rb4_5,rb4_6;
    RadioButton rb5_1,rb5_2,rb5_3,rb5_4,rb5_5,rb5_6;

    RadioGroup rag1,rag2,rag3,rag4;

    RadioButton rab1,rab2,rab3,rab4;

     private float initialXpoint;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.comp);

    viewFlipper = (ViewFlipper) findViewById(R.id.ViewFlipper01);

    rag1=(RadioGroup)findViewById(R.id.radioanswer);

    intit();

    slide_in_left = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left);

    slide_out_right = AnimationUtils.loadAnimation(this,android.R.anim.slide_out_right);

    viewFlipper.setInAnimation(slide_in_left);

    viewFlipper.setOutAnimation(slide_out_right);

    Next = (Button) findViewById(R.id.nextbtn);

    Previous = (Button) findViewById(R.id.previousbtn);



     Next.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) 
            {
                // TODO Auto-generated method stub

                id1 = rag1.getCheckedRadioButtonId();
                rab1=(RadioButton)findViewById(id1);
                System.out.println("1nd question answer"+rab1.getText());
                    count++;
                    switch(count)
                    {

                    case 1:

                        temp.clear();
                        temp.add(c_alcorrectanswer.get(1));
                        temp.add(c_alwronganswer1.get(1));
                        temp.add(c_alwronganswer2.get(1));
                        temp.add(c_alwronganswer3.get(1));
                        temp.add(c_alwronganswer4.get(1));
                        temp.add(c_alwronganswer5.get(1));

                        Collections.shuffle(temp);
                        rb2_1.setText(temp.get(0));
                        rb2_2.setText(temp.get(1));
                        rb2_3.setText(temp.get(2));
                        rb2_4.setText(temp.get(3));
                        rb2_5.setText(temp.get(4));
                        rb2_6.setText(temp.get(5));



                        //viewFlipper.showNext();
                        break;

                    case 2:

                        temp.clear();
                        temp.add(c_alcorrectanswer.get(2));
                        temp.add(c_alwronganswer1.get(2));
                        temp.add(c_alwronganswer2.get(2));
                        temp.add(c_alwronganswer3.get(2));
                        temp.add(c_alwronganswer4.get(2));
                        temp.add(c_alwronganswer5.get(2));

                        Collections.shuffle(temp);
                        rb3_1.setText(temp.get(0));
                        rb3_2.setText(temp.get(1));
                        rb3_3.setText(temp.get(2));
                        rb3_4.setText(temp.get(3));
                        rb3_5.setText(temp.get(4));
                        rb3_6.setText(temp.get(5));

                        //viewFlipper.showNext();
                        break;

                    case 3:

                        temp.clear();
                        temp.add(c_alcorrectanswer.get(3));
                        temp.add(c_alwronganswer1.get(3));
                        temp.add(c_alwronganswer2.get(3));
                        temp.add(c_alwronganswer3.get(3));
                        temp.add(c_alwronganswer4.get(3));
                        temp.add(c_alwronganswer5.get(3));

                        Collections.shuffle(temp);
                        rb4_1.setText(temp.get(0));
                        rb4_2.setText(temp.get(1));
                        rb4_3.setText(temp.get(2));
                        rb4_4.setText(temp.get(3));
                        rb4_5.setText(temp.get(4));
                        rb4_6.setText(temp.get(5));

                        //viewFlipper.showNext();
                        break;
                    }


                    viewFlipper.showNext();

                    /*id2 = rag2.getCheckedRadioButtonId();
                    rab2=(RadioButton)findViewById(id2);
                    System.out.println("2nd question answer"+rab2.getText());

                    id3 = rag3.getCheckedRadioButtonId();
                    rab3=(RadioButton)findViewById(id3);
                    System.out.println("2nd question answer"+rab3.getText());

                    id4 = rag4.getCheckedRadioButtonId();
                    rab4=(RadioButton)findViewById(id4);
                    System.out.println("2nd question answer"+rab4.getText());
            */      

            }
        });

            Previous.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

                viewFlipper.showPrevious();
            }
        });

    new Dailytask().execute();

    Collections.shuffle(temp);
    System.out.println("<-----going to set----->"+temp);
    /*rb1_1.setText(temp.get(0));
    rb1_2.setText(temp.get(1));
    rb1_3.setText(temp.get(2));
    rb1_4.setText(temp.get(3));
    rb1_5.setText(temp.get(4));*/
}


private void intit()
{
    // TODO Auto-generated method stub
    qst1=(TextView)findViewById(R.id.c_qst);
    qst2=(TextView)findViewById(R.id.c_qst2);
    qst3=(TextView)findViewById(R.id.c_qst3);
    qst4=(TextView)findViewById(R.id.c_qst4);


    rag1=(RadioGroup)findViewById(R.id.radioanswer);
    rag2=(RadioGroup)findViewById(R.id.radioanswer2);
    rag3=(RadioGroup)findViewById(R.id.radioanswer3);
    rag4=(RadioGroup)findViewById(R.id.radioanswer4);

    rb1_1=(RadioButton)findViewById(R.id.btn1_1);
    rb1_2=(RadioButton)findViewById(R.id.btn1_2);
    rb1_3=(RadioButton)findViewById(R.id.btn1_3);
    rb1_4=(RadioButton)findViewById(R.id.btn1_4);
    rb1_5=(RadioButton)findViewById(R.id.btn1_5);
    rb1_6=(RadioButton)findViewById(R.id.btn1_6);

    rb2_1=(RadioButton)findViewById(R.id.btn2_1);
    rb2_2=(RadioButton)findViewById(R.id.btn2_2);
    rb2_3=(RadioButton)findViewById(R.id.btn2_3);
    rb2_4=(RadioButton)findViewById(R.id.btn2_4);
    rb2_5=(RadioButton)findViewById(R.id.btn2_5);
    rb2_6=(RadioButton)findViewById(R.id.btn2_6);

    rb3_1=(RadioButton)findViewById(R.id.btn3_1);
    rb3_2=(RadioButton)findViewById(R.id.btn3_2);
    rb3_3=(RadioButton)findViewById(R.id.btn3_3);
    rb3_4=(RadioButton)findViewById(R.id.btn3_4);
    rb3_5=(RadioButton)findViewById(R.id.btn3_5);
    rb3_6=(RadioButton)findViewById(R.id.btn3_6);

    rb4_1=(RadioButton)findViewById(R.id.btn4_1);
    rb4_2=(RadioButton)findViewById(R.id.btn4_2);
    rb4_3=(RadioButton)findViewById(R.id.btn4_3);
    rb4_4=(RadioButton)findViewById(R.id.btn4_4);
    rb4_5=(RadioButton)findViewById(R.id.btn4_5);
    rb4_6=(RadioButton)findViewById(R.id.btn4_6);

    /*rb5_1=(RadioButton)findViewById(R.id.btn5);
    rb5_2=(RadioButton)findViewById(R.id.btn2);
    rb5_3=(RadioButton)findViewById(R.id.btn3);
    rb5_4=(RadioButton)findViewById(R.id.btn4);
    rb5_5=(RadioButton)findViewById(R.id.btn5);
*/}


/*@Override
public boolean onTouchEvent(MotionEvent touchevent)
{
    switch (touchevent.getAction())
    {
    case MotionEvent.ACTION_DOWN:
        initialXpoint = touchevent.getX();
        break;
        case MotionEvent.ACTION_UP:
        float finalX = touchevent.getX();
        if (initialXpoint > finalX)
        {
            if (viewFlipper.getDisplayedChild() == 1)
            break;
            viewFlipper.showNext();
        }
        else
        {
            if (viewFlipper.getDisplayedChild() == 0)
            break;
            viewFlipper.showPrevious();
        }
        break;
    }
    return false;
}
*/

@Override
public void onClick(View v) 
{
    // TODO Auto-generated method stub

    /*id=c_radioanswergroup.getCheckedRadioButtonId();

    c_radioanswerButton = (RadioButton) findViewById(id);

    System.out.println("answer--"+c_radioanswerButton.getText());
    */

}



private class Dailytask extends AsyncTask<String, String, JSONObject> 
{
    private ProgressDialog pDialog;

    @Override
    protected void onPreExecute() 
    {
        super.onPreExecute();



        pDialog = new ProgressDialog(Comptest.this);

        pDialog.setMessage("Loading Assessment ...");

        pDialog.setIndeterminate(false);

        pDialog.setCancelable(true);

        pDialog.show();
    }

    @Override
    protected JSONObject doInBackground(String... args) 
    {
        Json j = new Json();
        // Getting JSON from URL
        JSONObject json = j.getJSONFromUrl(url);
        return json;
    }

    @Override
    protected void onPostExecute(JSONObject json)
    {

        System.out.println("---------------return today test json------------"+json);

        pDialog.dismiss();

        try
        {
                // Getting JSON Array from URL
            JSONObject jobj = json.getJSONObject("response");

            jarray = jobj.getJSONArray("YearTest");

            System.out.println("json array length"+jarray.length());

            for (int i = 0; i < jarray.length(); i++) 
            {
                JSONObject data = jarray.getJSONObject(i);
                // Storing JSON item in a Variable

                c_alquestion.add(data.getString("question"));

                c_alcorrectanswer.add(data.getString("correctanswer"));

                c_alwronganswer1.add(data.getString("wronganswer1"));

                c_alwronganswer2.add(data.getString("wronganswer2"));

                c_alwronganswer3.add(data.getString("wronganswer3"));

                c_alwronganswer4.add(data.getString("wronganswer4"));

                c_alwronganswer5.add(data.getString("wronganswer5"));


                System.out.println("question------->"+c_alquestion);
                System.out.println("correct answer------------>"+c_alcorrectanswer);
                System.out.println("wroang ans1------------>"+c_alwronganswer1);

                System.out.println("wroang ans2------------>"+c_alwronganswer2);
                System.out.println("wroang ans3------------>"+c_alwronganswer3);
                System.out.println("wroang ans4------------>"+c_alwronganswer4);

                System.out.println("wroang ans5------------>"+c_alwronganswer5);



            }


        }
        catch(Exception e)
        {
            System.out.println("<------EXCEPTION------>"+e);
        }

        /*Collections.shuffle(c_alquestion);
        System.out.println("<------------shuffle------>"+c_alquestion);
        */
        for(int i=0;i<4;i++)
        {
            q_question.add(c_alquestion.get(i));

        }
        System.out.println("<-----question list after shuffle------>"+q_question);
        qst1.setText(q_question.get(0));
        qst2.setText(q_question.get(1));
        qst3.setText(q_question.get(2));
        qst4.setText(q_question.get(3));


        temp.add(c_alcorrectanswer.get(0));
        temp.add(c_alwronganswer1.get(0));
        temp.add(c_alwronganswer2.get(0));
        temp.add(c_alwronganswer3.get(0));
        temp.add(c_alwronganswer4.get(0));
        temp.add(c_alwronganswer5.get(0));

        System.out.println("<-----temp data--->"+temp);

        Collections.shuffle(temp);
        rb1_1.setText(temp.get(0));
        rb1_2.setText(temp.get(1));
        rb1_3.setText(temp.get(2));
        rb1_4.setText(temp.get(3));
        rb1_5.setText(temp.get(4));
        rb1_6.setText(temp.get(5));



    }

}
}

1 个答案:

答案 0 :(得分:0)

使用getDisplayedChild()方法获取子ID,并从特定布局或子级获取视图的值。