2017-06-24 76 views
-2

當一個標題的單選按鈕被選中時,其他單選按鈕也被自己檢查。並擴大所有和刷屏幕的單選按鈕標題正在交換或轉移。 請幫助如何管理動態製作的形式與許多單選按鈕選項。在我的情況下,窗體是在可擴展列表視圖。任何幫助將不勝感激。實現OnClickListener動態創建ExpandableListView與在Android中具有Radiobuttons的項目

public class ExpandableListAdapter extends BaseExpandableListAdapter implements ExpandableListView.OnGroupExpandListener{ 
public Context context; 
private List<String> listDataHeader; 
private HashMap<String,List<String>> listHashMap; 
private List<Services> cpvalue; 
private List<Options> options; 
//private boolean[] flag; 
public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String,List<String>> listHashMap,List<Services> cpvalue) { 
    this.context = context; 
    this.listDataHeader = listDataHeader; 
    this.listHashMap = listHashMap; 
    this.cpvalue=cpvalue; 
} 

@Override 
public int getGroupCount() { 
    return listDataHeader.size(); 
} 

@Override 
public int getChildrenCount(int groupPosition) { 
    return listHashMap.get(listDataHeader.get(groupPosition)).size(); 
} 


@Override 
public Object getGroup(int groupPosition) { 
    return listDataHeader.get(groupPosition); 
} 

@Override 
public Object getChild(int groupPosition, int childPosition) { 
    return listHashMap.get(listDataHeader.get(groupPosition)).get(childPosition); 
} 

@Override 
public long getGroupId(int groupPosition) { 
    return groupPosition; 
} 

@Override 
public long getChildId(int groupPosition, int childPosition) { 
    return childPosition; 
} 

@Override 
public boolean hasStableIds() { 
    return false; 
} 

@Override 
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 
    String headerTitle=(String)getGroup(groupPosition); 
    if(convertView==null){ 
     LayoutInflater layoutInflater=(LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView=layoutInflater.inflate(R.layout.list_group,null); 
    } 
    TextView lblListHeader=(TextView) convertView.findViewById(R.id.lblListHeader); 
    lblListHeader.setTypeface(null, Typeface.BOLD); 
    lblListHeader.setText(headerTitle); 
    return convertView; 
} 

@Override 
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 
    final String childText=(String)getChild(groupPosition,childPosition); 
    if(convertView==null){ 
     LayoutInflater layoutInflater=(LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView=layoutInflater.inflate(R.layout.list_item,null); 
     RadioGroup radioGroup = (RadioGroup) convertView.findViewById(R.id.radioGroup); 
     options=cpvalue.get(groupPosition).getCheckPoints().get(childPosition).getCp_options(); 
     /*flag=new boolean[options.size()]; 
     for(int f=1;f<options.size();f++) { 
      flag[f]=false; 
     }*/ 
     for (int k = 0; k < options.size(); k++) { 
      View radioButtonView = layoutInflater.inflate(R.layout.check_radio_button, null); 
      RadioButton radioButton = (RadioButton) radioButtonView 
        .findViewById(R.id.radio_button); 
      //radioButton.setId(options.get(k).getId()); 
      radioButton.setText(options.get(k).getCp_value()); 
      radioButton.setOnCheckedChangeListener(getOnCheckDoSomething(radioButton)); 
      // radioButton.setOnCheckedChangeListener(this); 
      radioGroup.addView(radioButtonView); 

     } 
     /*radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() 
     { 
      @Override 
      public void onCheckedChanged(RadioGroup group, int checkedId) { 
       // checkedId is the RadioButton selected 
       Toast.makeText(context, "okay", Toast.LENGTH_SHORT).show(); 
      } 
     });*/ 
    } 
    TextView txtListChild=(TextView)convertView.findViewById(R.id.lblListItem); 
    txtListChild.setText(childText); 

    return convertView; 
} 

@Override 
public boolean isChildSelectable(int groupPosition, int childPosition) { 
    return true; 
} 




RadioButton.OnCheckedChangeListener getOnCheckDoSomething(final RadioButton button) { 
    return new RadioButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      Log.d("hello" , "hogaya"); 
      Toast.makeText(context, "okay", Toast.LENGTH_SHORT).show(); 


     } 
    }; 
} 

@Override 
public void onGroupExpand(int groupPosition) { 

} 

}

enter image description here

回答

0

這是因爲單選按鈕的創建是有問題的。如果我會動態創建單選按鈕,我會做一些如下,

final RadioButton[] rb; 
//You can create a new radio group  
//RadioGroup rg = new RadioGroup(this.context); //create the RadioGroup 
// Or use from your xml 
    RadioGroup radioGroup = (RadioGroup) convertView.findViewById(R.id.radioGroup); 

radioGroup.setOrientation(RadioGroup.HORIZONTAL);//or RadioGroup.VERTICAL 
    for(int i=0; i < options.size(); i++){ 
     rb[i] = new RadioButton(this.context); 
     radioGroup.addView(rb[i]); //the RadioButtons are added to the radioGroup 
     rb[i].setText(options.get(k).getCp_value()); 
    } 
// if you added new radio group  
//convertView.addView(rg);//you add the whole RadioGroup to the layout, I suppose convertView is a layout view 

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() 
     { 
      @Override 
      public void onCheckedChanged(RadioGroup group, int checkedId) { 
       // checkedId is the RadioButton selected 
       Toast.makeText(context, "okay", Toast.LENGTH_SHORT).show(); 
      } 
     }); 
相關問題