在ListView中检查RadioGroup的单选按钮检查其他项目按钮

时间:2014-09-24 13:31:07

标签: android android-listview android-radiogroup android-radiobutton

这里是适配器类.....

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

2 个答案:

答案 0 :(得分:0)

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

首先在holder.myRG.setTag(childId);开头获取该ID之后,我有类似getView()的内容。您可以使用position

在设置之前,我将RadioGroup s选中按钮设置为-1,因此每次都检查没有任何内容然后检查HashMap<String, Boolean>以查看该项是否存在以及是否{{1} }}。如果选中,则会在true中将项目添加到地图中。

以下是我正在做的事情的一个例子。

onCheckedChanged()

我做的不仅仅是展示的内容,但我认为我已经取出了相关部分。

答案 1 :(得分:0)

我也面临同样的问题,但我已经解决了,我认为你可以使用与我写的代码相同的代码 代码是: - ///完成是你的最终计算按钮

<resources>

<color name="custom_theme_color">#42A5F5</color>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->

    <item name="colorPrimaryDark">@color/custom_theme_color</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>