2017-03-04 95 views
0

我在Android设备上写了一个测验应用程序,其中有一些问题。每个问题都有一个收音机组和至少4个单选按钮的应答选项。在收音机组中选择一个单选按钮会导致在不同的收音机组中选择一些单选按钮

作为测验活动的测验页,我用我列表视图。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/content_question_list" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
tools:context="ir.testravanshenasi.segalclinic.test.QuestionListActivity" 
tools:showIn="@layout/activity_question_list"> 

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentStart="true"> 

    <TextView 
     android:text="test questions" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/textView13" 
     android:layout_marginBottom="10dp" 
     android:textAlignment="center" 
     android:textSize="18sp" /> 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="3dp" 
     android:background="@android:color/darker_gray" 
     android:layout_alignParentTop="true" /> 

    <ListView 
     android:layout_width="match_parent" 
     android:id="@+id/activity_question_list_list" 
     android:layout_height="430dp"> 


    </ListView> 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="3dp" 
     android:background="@android:color/darker_gray" 
     android:layout_alignParentTop="true" /> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:paddingLeft="5dp" 
     android:paddingRight="5dp"> 

     <Button 
      android:text="cancel" 
      android:layout_width="wrap_content" 
      android:id="@+id/activity_question_list_cancel" 
      android:layout_weight="1" 
      android:textSize="18sp" 
      android:layout_height="40dp" 
      android:onClick="onCancelBtnClick" /> 

     <Button 
      android:text="end" 
      android:layout_width="wrap_content" 
      android:layout_height="30dp" 
      android:id="@+id/activity_question_list_complete" 
      android:layout_weight="1" 
      android:textSize="18sp" 
      android:background="@android:color/holo_green_dark" 
      android:onClick="onCompleteBtnClick" /> 
     </LinearLayout> 

    </LinearLayout> 
</RelativeLayout> 

我为每个问题行做了布局。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="10dp" 
    android:paddingLeft="10dp" 
    android:paddingRight="10dp" 
    android:paddingTop="10dp"> 

    <TextView 
    android:text="TextView" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/question_id" 
    android:visibility="gone" /> 

<TextView 
    android:text="TextView" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/question_text" /> 

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

    <RadioButton 
     android:text="RadioButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/question_option_1" 
     android:layout_weight="1" /> 

    <RadioButton 
     android:text="RadioButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/question_option_2" 
     android:layout_weight="1" /> 

    <RadioButton 
     android:text="RadioButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/question_option_3" 
     android:layout_weight="1" /> 

    <RadioButton 
     android:text="RadioButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/question_option_4" 
     android:layout_weight="1" /> 

    <RadioButton 
     android:text="RadioButton" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/question_option_5" 
     android:layout_weight="1" 
     android:visibility="gone" /> 
    </RadioGroup> 

</LinearLayout> 

我添加问题作为SimpleAdapter列表视图。

 String[] from = {"qid" , "question" , "op1","op2" , "op3" , "op4"}; 

     int[] to = { R.id.question_id , R.id.question_text , R.id.question_option_1 ,R.id.question_option_2 , R.id.question_option_3 , R.id.question_option_4 }; 

     questionsList = parse(jsonArray); 

     SimpleAdapter rlt = new SimpleAdapter(
       getBaseContext() , questionsList , R.layout.question ,from , to); 

     lv.setAdapter(rlt); 

的问题是,当我选择(例如)在一个问题的第一个答案选项,在其他一些问题,选择了第一个选项了。

我在Genymotion测试我的应用程序。

谢谢。

+0

显示正在implementig –

+0

代码中没有人能够帮助你,直到你告诉他们你完成了。 –

回答

0

我解决了这个问题,这是我的解决方案:

我做了entends SimpelAdapter类,并用它HAVA这儿过得存储问题id和选用单选按钮数的列表。当选择单选按钮时,我将它的号码存储在列表中,并且在列表i的每个循环中测试列表中是否存在单选按钮号码?我选择收音机组方法来检查或清除单选按钮的检查。

public class row_question_list extends SimpleAdapter { 

List<HashMap<String , Object>> questionsList; 

public row_question_list(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) { 
    super(context, data, resource, from, to); 
    questionsList = (List<HashMap<String , Object>>) data; 
} 

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    View v = super.getView(position, convertView, parent); 

    RadioButton op1 = (RadioButton) v.findViewById(R.id.question_option_1); 
    RadioButton op2 = (RadioButton) v.findViewById(R.id.question_option_2); 
    RadioButton op3 = (RadioButton) v.findViewById(R.id.question_option_3); 
    RadioButton op4 = (RadioButton) v.findViewById(R.id.question_option_4); 
    RadioButton op5 = (RadioButton) v.findViewById(R.id.question_option_5); 
    final RadioGroup rg = (RadioGroup) v.findViewById(R.id.question_options); 


    op1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      questionsList.get(position).put("answer","1"); 
      notifyDataSetChanged(); 
     } 
    }); 

    op2.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      questionsList.get(position).put("answer","2"); 
      notifyDataSetChanged(); 
     } 
    }); 

    op3.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      questionsList.get(position).put("answer","3"); 
      notifyDataSetChanged(); 
     } 
    }); 

    op4.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      questionsList.get(position).put("answer","4"); 
      notifyDataSetChanged(); 
     } 
    }); 

    op5.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      questionsList.get(position).put("answer","5"); 
      notifyDataSetChanged(); 
     } 
    }); 


    if(questionsList.get(position).get("answer").toString().matches("0")){ 
     rg.clearCheck(); 
     rg.refreshDrawableState(); 

    } else if(questionsList.get(position).get("answer").toString().matches("1")){ 
     rg.check(R.id.question_option_1); 
     rg.refreshDrawableState(); 

    }else if(questionsList.get(position).get("answer").toString().matches("2")){ 
     rg.check(R.id.question_option_2); 
     rg.refreshDrawableState(); 

    }else if(questionsList.get(position).get("answer").toString().matches("3")){ 
     rg.check(R.id.question_option_3); 
     rg.refreshDrawableState(); 

    }else if(questionsList.get(position).get("answer").toString().matches("4")){ 
     rg.check(R.id.question_option_4); 
     rg.refreshDrawableState(); 

    }else if(questionsList.get(position).get("answer").toString().matches("5")){ 
     rg.check(R.id.question_option_5); 
     rg.refreshDrawableState(); 

    } 

    return v; 
} 

}

,我在上榜的项目中使用这个类:

 String[] from = {"question" , "op1","op2" , "op3" , "op4"}; 

     int[] to = { R.id.question_text , R.id.question_option_1 ,R.id.question_option_2 , R.id.question_option_3 , R.id.question_option_4 }; 

     questionsList = parse(jsonArray); 

     row_question_list rql = new row_question_list(
       getBaseContext() , questionsList , R.layout.question , from , to 
     ); 

     lv.setAdapter(rql); 
相关问题