2016-02-10 140 views
0

我正在一个应用程序,我有多个线性布局是动态生成的。每个线性布局都有一个Id字段,Name和一个RadioGroup,其中包含两个单选按钮,分别为Yes和No.如何选择按钮后的所有单选按钮android

我还在页面底部提供了一个按钮,以便一旦点击该按钮,将选择所有YES单选按钮。

我怎样才能实现这一目标的布局是基于一些数据动态生成的:

这里是我的布局,动态重复:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="match_parent" 
    android:weightSum="10"> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="2"> 

       <LinearLayout 
        android:orientation="horizontal" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:weightSum="10"> 
        <LinearLayout 
         android:orientation="vertical" 
         android:layout_width="0dp" 
         android:layout_height="match_parent" 
         android:layout_weight="1" 
         android:gravity="center"> 
        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="1" 
         android:id="@+id/RollNum" 
         android:textSize="20dp" 
         android:textColor="@color/black" 
         android:gravity="center" 
         /> 
        </LinearLayout> 
        <LinearLayout 
         android:orientation="vertical" 
         android:layout_width="0dp" 
         android:layout_height="70dp" 
         android:layout_weight="6" 
         android:gravity="center"> 
        <TextView 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:text="Amandeep Singh and some very very long name" 
         android:gravity="center" 
         android:id="@+id/Name" 
         android:textSize="20dp" 
         android:textColor="@color/black"/> 
        </LinearLayout> 
        <LinearLayout 
         android:orientation="vertical" 
         android:layout_width="0dp" 
         android:layout_height="match_parent" 
         android:layout_weight="3" 
         android:gravity="center"> 
         <RadioGroup 
          android:id="@+id/radioAttendance" 
          android:layout_width="match_parent" 
          android:layout_height="match_parent" 
          android:orientation='horizontal' 
          android:gravity="center"> 

         <RadioButton 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="P" 
          android:id="@+id/PresentCheck" 
          android:textSize="10dp" 
          android:gravity="center" 
          android:textColor="@color/black" 
          /> 
          <RadioButton 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:text="A" 
           android:id="@+id/AbsentCheck" 
           android:textSize="15dp" 
           android:gravity="center" 
           android:textColor="@color/black" 
           /> 
          </RadioGroup> 

</LinearLayout> 
    </LinearLayout> 
     </LinearLayout> 
     <LinearLayout 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="0.1"> 
      <View 
       android:layout_width="fill_parent" 
       android:layout_height="0.1dp" 
       android:background="#ffffff" /> 
     </LinearLayout> 
</LinearLayout> 

我尝试以下方法,但没有工作:

//some code before it for button clicks and all other stuffs 

    LayoutInflater inflater = (LayoutInflater)context.getSystemService 
      (Context.LAYOUT_INFLATER_SERVICE); 
    LinearLayout scrollViewLinearlayout = (LinearLayout)findViewById(R.id.parent); // The layout inside scroll view 
    //int count=50; 
    Studentlist = Arrays.copyOfRange(StudentlistInterim, 1, StudentlistInterim.length); 
    for(int i = 0; i < Studentlist.length; i++){ 
     String data = Studentlist[i]; 
     String RollNum = data.split("--")[0]; 
     String Name = data.split("--")[1]; 
     Log.d("Name is:", Name); 

     LinearLayout layout2 = new LinearLayout(context); 
     layout2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
     View item = inflater.inflate(R.layout.item_layout, null, false); 

     layout2.addView(item); 
     layout2.setId(i); 
     TextView trollnum = (TextView) item.findViewById(R.id.RollNum); 
     trollnum.setText(RollNum); 
     TextView tname = (TextView) item.findViewById(R.id.Name); 
     tname.setText(Name); 
     RadioButton rPresent = (RadioButton)item.findViewById(R.id.PresentCheck); 
     RadioButton rAbsent = (RadioButton)item.findViewById(R.id.AbsentCheck); 
     rPresent.setId(i); //tried to set a unique Id for all YES 
     rAbsent.setId(Studentlist.length+i); // to make the id unique for each YES and NO entry 

     scrollViewLinearlayout.addView(layout2); 


     Log.d("Id is: ", trollnum.getText().toString()); 
    } 
} 

@Override 
public void onClick(View v){ 
    switch(v.getId()){ 
     case R.id.bMarkAllPresent: 
      for (int i=0;i<Studentlist.length;i++){//iterate over the list for the radio buttons 
       RadioButton r1 = (RadioButton)findViewById(i); 
       RadioGroup rg = (RadioGroup)findViewById(R.id.radioAttendance); 
       rg.check(r1.getId()); /* get the id of radio button one by one and mark it checked*/ 
       //Log.d("Button check id:", (r1.getId())); 
      } 
      break; 

我得到了 'java.lang.ClassCastException:android.widget.LinearLayout不能转换到android.widget.RadioButton' 错误在onclick()方法

我不确定这种方法是否有效,或者这是否是最佳方法。

请帮忙。

编辑-1:

用做它作为每答案的列表方式后。

 RadioButton rPresent = (RadioButton)item.findViewById(R.id.PresentCheck); 
     RadioButton rAbsent = (RadioButton)item.findViewById(R.id.AbsentCheck); 
     //rPresent.setId(i); 
     //rAbsent.setId(Studentlist.length+i); // to make the id unique for each present and absent entry 
     presentRadioList.add(rPresent); //getting null pointer here 
     absentRadioList.add(rAbsent); 

我得到空指针异常。

回答

1

我建议保留两个List<RadioButton> s,一个用于“是”选项,另一个用于“否”选择。然后你可以遍历适当的列表。

+0

你能否请详细说明一下代码。 –

+0

@AdedeepSingh我将这些细节留给读者阅读。首先学习'List'接口和'ArrayList'类。 –

+0

嗨,我试过你描述的方式,但我得到了nullPointerException。我在编辑中粘贴了上面的代码。请检查并告诉我是否缺少任何东西。 –