Search code examples
androidlistviewcustom-lists

RadioButton selection jumps is auto selected on other Custom ListView


I have a custom listView that is populated by Excel Sheet using jXl library. Everything works fine but when I select one or more ListView Item, automatically after 8-9 entries my selection is also selected on other ListView Item.. for example if radioButton1 is selected at index 0 now If I scroll down I see radioButton1 is selected at index 8. Here is my code: CustomListView

public class CustomListView extends ArrayAdapter<ListModel> {

    Context context;
    private List<ListModel> studentsList;
    ListModel listModel;
    TextView name, date;
    RadioGroup radioGroup;
    RadioButton presentRDOButton, absentRDOButton, leaveRDOButton;
    LayoutInflater inflater;
    public static List<ListModel> attenList = new ArrayList<ListModel>();
    final JExcelClass jXl = new JExcelClass();
    boolean flagPres = true;
    boolean flagAbsen = true;
    boolean flagLeave = true;

    public AttendanceCustomListView(Context context, int resource,
            List<ListModel> studentsList) {
        super(context, resource, studentsList);
        this.context = context;
        this.studentsList = studentsList;
    }

private class ViewHolder {
        TextView name, date;
        RadioGroup radioGroup;
        RadioButton presentRDOButton, absentRDOButton, leaveRDOButton;
    }
@Override
    public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
        final int pos = position + 1;

        // inflater = (LayoutInflater) context
        // .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {
            inflater = (LayoutInflater) context
                    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.layout_list_content, null);
            holder = new ViewHolder();
            // initialization here
            holder.name = (TextView) convertView.findViewById(R.id.nameListTV);
            holder.date = (TextView) convertView.findViewById(R.id.dateTV);
            holder.radioGroup = (RadioGroup) convertView
                    .findViewById(R.id.attendanceRDG);
            holder.presentRDOButton = (RadioButton) convertView
                    .findViewById(R.id.presentRDO);
            holder.absentRDOButton = (RadioButton) convertView
                    .findViewById(R.id.absentRDO);
            holder.leaveRDOButton = (RadioButton) convertView
                    .findViewById(R.id.leaveRDO);
            holder.radioGroup
                    .setOnCheckedChangeListener(new OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(RadioGroup group,
                                int checkedId) {
                            int getPosition = (Integer) group.getTag();

                           studentsList.get(getPosition). //problem here

                           switch (checkedId) {
                            case R.id.presentRDO:
                               //code goes here
                                 break;
                                    }

                }
            });

and CustomClass

public class CustomClass extends Activity {

    ListView listView;
    List<ListModel> listModel;
CustomListView attendanceCustomListView;
    JExcelClass jXl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_listview);
        listView = (ListView) findViewById(R.id.list);

        listModel = new ArrayList<ListModel>();
        jXl = new JExcelClass();
        List<String> abc = jXl.readExcelSheet("test.xls");
        String date = this.getDate();
        for (String temp : abc) {
            listModel.add(new ListModel(temp, date));
        }
        attendanceCustomListView = new AttendanceCustomListView(this,
                R.layout.layout_list_content, listModel);
        listView.setAdapter(attendanceCustomListView);
    }
}

Please let me know If some more information is needed. Thanks.

Updated the Code Now my problem is how to set the RadioButton through RadioGroup?


Solution

  • Okay! at last i got my answer! Basically all i needed was to add 3 static final variables and use them for saving the state of the radioButton clicked. Please follow this answer, this is the most simple answer all over the internet. Thanks @hrishitiwari for pointing to the answer.