1

here is adapter class.....

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

And this is Sudent Modal Class where data is ro be set.....

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;
}
codeMagic
  • 44,549
  • 13
  • 77
  • 93
Anuj Gupta
  • 11
  • 1
  • 1
    Are you saying it checks the wrong button in the group or it checks a button in a different row? – codeMagic Sep 24 '14 at 13:51
  • Actually i want to check one radio button in each row at a time. But when i check any item button and then it checks another row button for same radio id and as i scroll list then it again reset previous row radio button. Please tell me how do i select only one in each row and after scrolling it doesn't reset previous row button. – Anuj Gupta Sep 25 '14 at 07:49

2 Answers2

0

You need some way of setting the checked state for each RadioButton to distinguish if it's in the correct row or not. You can use getTag()/setTag() for that. I have an ExpandableListView so I store the child row id that I have in a separate "child" class for each object.

So I have something like holder.myRG.setTag(childId); after first getting that id at the beginning of getView(). You could probably use the position.

Before setting this, I set my RadioGroups checked button to -1 so nothing is checked each time then I check a HashMap<String, Boolean> to see if that item is there and if it is true. Items are added to the map in onCheckedChanged() if it is checked.

Here is an example of what I'm doing.

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

I'm doing a lot more than what's shown but I think I pulled out the relevant parts.

codeMagic
  • 44,549
  • 13
  • 77
  • 93
0

I had also faced same problem , but i had solved it ,I think you can just use code which is in same way as the code i am writing code is :- ///done is your final calculation button

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

}
            }
        });
Shubham Sharma
  • 2,763
  • 5
  • 31
  • 46