1

這裏是適配器類.....檢查RadioGroup中的單選按鈕ListView中檢查其他項目按鈕

public View getView(final int position, View convertView, ViewGroup parent) 
    { 
     ViewHolder holder=null; 
     Student temp = (Student) getItem(position); 
     if (convertView == null) 
     { 
      convertView = mInflator.inflate(R.layout.item_class_student, null); 
      holder = new ViewHolder(); 
      holder.nameTextView = (TextView) convertView.findViewById(R.id.row_student_name); 
      holder.classTextView = (TextView) convertView.findViewById(R.id.row_student_class); 
      holder.rollNumberTextView = (TextView) convertView.findViewById(R.id.row_student_roll_number); 
      holder.image = (ImageView) convertView.findViewById(R.id.row_student_image); 
      //holder.checkBox=(CheckBox) convertView.findViewById(R.id.cb_student_selected); 
      holder.mgroup=(RadioGroup) convertView.findViewById(R.id.rg_list); 
      holder.pre=(RadioButton) convertView.findViewById(R.id.rb_present); 
      holder.abs=(RadioButton) convertView.findViewById(R.id.rb_absent); 
      holder.leave=(RadioButton) convertView.findViewById(R.id.rb_leave); 


     convertView.setTag(holder); 
    } 


    holder = (ViewHolder) convertView.getTag(); 
    //Student temp = (Student) getItem(position); 
    holder.nameTextView.setText(temp.getName()); 
    holder.rollNumberTextView.setText("Roll No: " + temp.getRollNumber()); 
    holder.classTextView.setText(temp.getStudClass() + ""); 
    if(!flag) 
    { 
     holder.mgroup.setVisibility(View.GONE); 

    } 
    // Set Image 
    if (temp.getPhoto() != null) 
    { 
     mImageLoader.displayImage(temp.getPhoto(), holder.image, mOptions); 
    } 
    else 
    { 
     holder.image.setImageResource(R.drawable.img_default_profile); 
    } 


    if(temp.isChecked()) 
    { 
     holder.abs.setChecked(true); 
     holder.pre.setChecked(false); 
     holder.leave.setChecked(false); 
     //Toast.makeText(activity, "A", Toast.LENGTH_SHORT).show(); 
    } 
    else if(temp.isChecked()) 
    { 
     holder.abs.setChecked(false); 
     holder.pre.setChecked(true); 
     holder.leave.setChecked(false); 
     //Toast.makeText(activity, "P", Toast.LENGTH_SHORT).show(); 
    } 
    else if(temp.isChecked()) 
    { 
     holder.abs.setChecked(false); 
     holder.pre.setChecked(false); 
     holder.leave.setChecked(true); 
     //Toast.makeText(activity, "L", Toast.LENGTH_SHORT).show(); 
    } 
    holder.mgroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    { 

     @Override 
     public void onCheckedChanged(RadioGroup group, int checkedId) 
     { 
      View radioButton = group.findViewById(checkedId); 
      int radioId = group.indexOfChild(radioButton); 
      boolean checked = ((RadioButton) radioButton).isChecked(); 
      System.out.println("ID" + radioButton.getId()); 
      switch (radioId) 
      { 
      case R.id.rb_present: 
       if(checked) 
       { 
       flagPresent[position]=true; 
       flagAbsent[position]=false; 
       flagLeave[position]=false; 
       } 
       break; 

      case R.id.rb_absent: 
       if(checked) 
       { 
       flagPresent[position]=false; 
       flagAbsent[position]=true; 
       flagLeave[position]=false; 
       } 
       break; 
      case R.id.rb_leave: 
       if(checked) 
       { 
       flagPresent[position]=false; 
       flagAbsent[position]=false; 
       flagLeave[position]=true; 
       } 
       break; 
      } 


      //students.set(position, temp); 
      //refreshList(students, position); 



     } 
    }); 
    return convertView; 
} 

這是Sudent莫代爾類,其中數據RO設置.....

public class Student implements Parcelable { 

private String name; 
private String rollNumber; 
private String regId; 
private String studClass; 
private String photo; 
private boolean checked; 
private String status; 

public Student() { 
} 

public Student(Parcel in) { 
    this.name = in.readString(); 
    this.rollNumber = in.readString(); 
    this.regId = in.readString(); 
    this.studClass = in.readString(); 
    this.photo = in.readString(); 
} 

/** 
* @return the checked 
*/ 


public boolean isChecked() 
{ 
    return checked; 
} 



public void setChecked(boolean checked) 
{ 
    this.checked = checked; 
} 
+1

你是說它檢查組中的錯誤按鈕,或者它檢查不同行中的按鈕? – codeMagic 2014-09-24 13:51:00

+0

其實我想每次檢查每一行中的一個單選按鈕。但是,當我檢查任何項目按鈕,然後檢查另一個行按鈕爲相同的電臺ID和我滾動列表,然後再次重置前一行單選按鈕。請告訴我如何在每行中只選擇一個,滾動後不重置上一行按鈕。 – 2014-09-25 07:49:40

回答

0

您需要設置每個RadioButton的檢查狀態以區分它是否在正確的行中。您可以使用getTag()/setTag()。我有一個ExpandableListView,因此我將每個對象的子行ID存儲在單獨的「子」類中。

因此,我有類似於holder.myRG.setTag(childId);之後在getView()開始處獲得該ID之後。你可以使用position

之前設置此,我把我的RadioGroup小號檢查按鈕爲-1,所以每次沒有被選中,然後我檢查HashMap<String, Boolean>,看看該項目是存在的,如果它是true。如果選中項目,則將其添加到地圖onCheckedChanged()中。

這是我正在做的一個例子。

@Override 
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, 
      View convertView, ViewGroup parentView) 
{    
    final FormItemHolder holder; 
    View view = convertView; 

    // here I get the id 
    final String childId = mList.get(groupPosition).getChildren() 
      .get(childPosition).mId; 

    // I set the listener to null and uncheck both buttons--you may not need this 
    holder.rgCompliance.setOnCheckedChangeListener(null); 
    holder.rgCompliance.check(-1); 

    // in my listener, I add a reference to the button in a map declared in another class 
    @Override 
    public void onCheckedChanged(RadioGroup group, int checkedId) 
    { 
     String uid = (String) holder.myRG.getTag();         
     currentInspectionFormItem = InspectionFormData.getItemById(uid); 
     if (checkedId == R.id.rbCompliant) 
     { 
      myMap.put(uid, true); 
     }     
     else if ((checkedId == R.id.rbNotCompliant && show)) 
     { 
      if (myMap.get(uid) == null 
        || (InspectionFormData.complianceList.get(uid) != null 
        && InspectionFormData.complianceList.get(uid))) 
      { 
       myMap.put(uid, false); 
      } 
     } 
     }   
    }); 

    // here I check to see if the buttons id has been put in the list 
    // if it is present then I check the button accordingly 
    Boolean present = myMap.get(childId); 
    if (present != null) 
    { 
     if (holder.myRG.getCheckedRadioButtonId() != -1) 
      holder.myRG.setOnCheckedChangeListener(null); 
     holder.myRG.check((present.booleanValue()) ? holder.rb1.getId() : holder.rb2.getId());               
    } 
} 

我做得比所示的多得多,但我想我已經拿出了相關的部分。

0

我也遇到了同樣的問題,但我已經解決了它,我認爲你可以使用與我正在編寫的代碼相同的代碼 代碼是: - /// done是您的最終計算按鈕

done.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     final int child=listView.getChildCount(); 
      for(int i=0;i<child;i++) { 
    View rgg=listView.getChildAt(i); 

    radioGroup = (RadioGroup) rgg.findViewById(R.id.radio); 

    int selectedId=radioGroup.getCheckedRadioButtonId(); 
    radioButton = (RadioButton) rgg.findViewById(selectedId); 

} 
      } 
     });