1

我是android新手,这是我的第一个应用程序,我有带有复选框(多个)和单选按钮(单)的自定义列表视图,现在我只是像复选框(q1,q3)和单选按钮(q2,q4)一样实现它。我的问题是:如何获取所选复选框和单选按钮的值,单击提交按钮如何存储这些值。请提供一些解决方案。谢谢

public class MainActivity  extends ListActivity{
String[] QuestionList = new String[] {"q1","q2","q3","q4"};
String [] Question=new String []{"question1","question2","question3","question4"};
String[] option=new String[]{"option1","option2","option3","option4","option5","option6","option7","option8","option9","option10","option11","option12","option13","option14","option15","option16"};

String radiocheck;
CheckBox cb1;
CheckBox cb2;
CheckBox cb3;
CheckBox cb4;
TextView checkText;
LinearLayout check;
LinearLayout layout;
//LinearLayout radio;
TextView radioText;
RadioGroup radio;
RadioButton rb1;
RadioButton rb2;
RadioButton rb3;
RadioButton rb4;
Button save;
Button submit;
ListView list;

MyThumbnaildapter adapter;
public class MyThumbnaildapter extends ArrayAdapter<String>
{
public MyThumbnaildapter(Context context, int textViewResourceId,String[] objects)
            {
             super(context, textViewResourceId, objects);
            }
@Override
    public View getView( int position, View convertView, ViewGroup parent) {
    ViewGroup item =getViewGroup(convertView, parent);
    radiocheck = QuestionList[position];
    int pos=position;
    check = (LinearLayout)item.findViewById(R.id.linear);
    radio = (RadioGroup)item.findViewById(R.id.radio);
    save=(Button)item.findViewById(R.id.save);
    submit=(Button)item.findViewById(R.id.button2);
    layout=(LinearLayout)item.findViewById(R.id.layoutButton);

if (radiocheck.equalsIgnoreCase("q1")||radiocheck.equalsIgnoreCase("q3"))
        {
          check.setVisibility(View.VISIBLE);
          radio.setVisibility(View.GONE);
          checkText=(TextView)item.findViewById(R.id.c1);

          checkText.setText(Question[position]);
        cb1=(CheckBox)item.findViewById(R.id.ch1);
        cb2=(CheckBox)item.findViewById(R.id.ch2);
        cb3=(CheckBox)item.findViewById(R.id.ch3);
        cb4=(CheckBox)item.findViewById(R.id.ch4);

            cb1.setText(option[(pos*4)]);
            cb2.setText(option[(pos*4)+1]);
            cb3.setText(option[(pos*4)+2]);
            cb4.setText(option[(pos*4)+3]);

        }
else if(radiocheck.equalsIgnoreCase("q2")||radiocheck.equalsIgnoreCase("q4"))
        {
            check.setVisibility(View.GONE);
            radio.setVisibility(View.VISIBLE);
         radioText=(TextView)item.findViewById(R.id.Tradio);
             rb1=(RadioButton)item.findViewById(R.id.option1);
             rb2=(RadioButton)item.findViewById(R.id.option2);
             rb3=(RadioButton)item.findViewById(R.id.option3);
             rb4=(RadioButton)item.findViewById(R.id.option4);

         radioText.setText(Question[position]);
         rb1.setText(option[(pos*4)]);
         rb2.setText(option[(pos*4)+1]);
         rb3.setText(option[(pos*4)+2]);
         rb4.setText(option[(pos*4)+3]);
         } 
if((position+1)==Question.length){
   layout.setVisibility(View.VISIBLE);
   save.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        saveAnswers();

                                }

    private void saveAnswers() {
          LinearLayout root = (LinearLayout) findViewById(R.id.totalLayout);
            loopQuestions(root);
    }


    private void loopQuestions(ViewGroup parent) {
        for(int i = 0; i < parent.getChildCount(); i++) {
            View child = parent.getChildAt(i);

                if(child instanceof RadioGroup ) {
                    //Support for RadioGroups
                    RadioGroup radio = (RadioGroup)child;
                    storeAnswer(radio.getId(), radio.getCheckedRadioButtonId());
                }
                else if(child instanceof CheckBox) {
                    //Support for checkboxe1
                    CheckBox cb1 = (CheckBox)child;
                  int  answer = cb1.isChecked() ? 1:0;
                    if(answer==1)
                    storeAnswer(cb1.getId(), answer);
                }
                else if(child instanceof CheckBox) {
                    //Support for checkboxe2
                    CheckBox cb2 = (CheckBox)child;
                  int  answer = cb2.isChecked() ? 1:0;
                    if(answer==1)
                    storeAnswer(cb2.getId(), answer);
                }


                else if(child instanceof CheckBox) {
                    //Support for checkboxe3
                    CheckBox cb3 = (CheckBox)child;
                   int answer = cb3.isChecked() ? 1:0;
                    if(answer==1)
                    storeAnswer(cb3.getId(), answer);
                }
                else if(child instanceof CheckBox) {
                    //Support for checkboxe4
                    CheckBox cb4 = (CheckBox)child;
                   int answer = cb4.isChecked() ?  1:0;
                    if(answer==1)
                    storeAnswer(cb4.getId(), answer);
                }


                if(child instanceof ViewGroup) {
                    //Nested Q&A
                    ViewGroup group = (ViewGroup)child;
                    loopQuestions(group);
                }
            }
    }

        private void storeAnswer(int id, int answer) 
        {

            //how to store answer  using map
        }
});
}

return item;        
                                                                     }
}

public ViewGroup getViewGroup(View reuse, ViewGroup parent) 
{
if(reuse instanceof ViewGroup)
return (ViewGroup)reuse;

Context context = parent.getContext(); 
 LayoutInflater inflater = LayoutInflater.from(context); 
 ViewGroup item = (ViewGroup)inflater.inflate(R.layout.checkbox, null);
 return item;
  }   


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setListAdapter(new MyThumbnaildapter(MainActivity.this, R.layout.checkbox,QuestionList));

}}

checkbox.xml 是:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" 
android:id="@+id/totalLayout">

 <LinearLayout
    android:id="@+id/Loptionr"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:visibility="visible">

 <RadioGroup
    android:id="@+id/radio"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >

<TextView
    android:id="@+id/Tradio"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/optionS"
    android:layout_alignParentTop="true"

    android:textStyle="bold" />

<RadioButton
    android:id="@+id/option4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
/>

  <RadioButton
    android:id="@+id/option3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

   <RadioButton
    android:id="@+id/option2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

 />

  <RadioButton
    android:id="@+id/option1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     />
 </RadioGroup>
 </LinearLayout>

<LinearLayout
    android:id="@+id/linear"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:visibility="visible">

  <TextView 
    android:id="@+id/c1"

    android:layout_width="wrap_content"
      android:layout_height="wrap_content"/>
 <CheckBox
    android:id="@+id/ch1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

 />

<CheckBox
    android:id="@+id/ch2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    />

<CheckBox
    android:id="@+id/ch3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

<CheckBox
    android:id="@+id/ch4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
   />


</LinearLayout>

<LinearLayout android:layout_width="wrap_content"
 android:id="@+id/layoutButton"
 android:orientation="horizontal"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/artist"
    android:layout_below="@+id/thumbnail"
    android:layout_marginLeft="44dp"
     android:visibility="gone">
<Button
    android:id="@+id/save"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Save"
    android:visibility="visible" />
 <Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
  android:layout_marginLeft="30dp"
    android:text="Submit"
    android:visibility="visible" />
</LinearLayout>

</LinearLayout> 
4

0 回答 0