2017-03-04 45 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); 
相關問題