1

这是适配器类.....

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 Modal Class,其中数据被设置.....

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;
}
4

2 回答 2

0

您需要某种方法为每个设置检查状态,RadioButton以区分它是否在正确的行中。你可以使用getTag()/ setTag()。我有一个ExpandableListView,所以我将每个对象的子行 ID 存储在一个单独的“子”类中。

所以我有类似holder.myRG.setTag(childId);getView(). 您可能可以使用position.

在设置此之前,我将我RadioGroup的 s 已选中按钮设置为 -1,因此每次都不会检查任何内容,然后我检查 aHashMap<String, Boolean>以查看该项目是否存在以及是否存在trueonCheckedChanged()如果选中,项目将添加到地图中。

这是我正在做的一个例子。

@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());                                                          
    }
} 

我做的比所展示的要多得多,但我想我把相关的部分拿出来了。

于 2014-09-25T12:37:29.347 回答
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);

}
            }
        });
于 2016-04-24T12:18:16.957 回答