HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Radio ListView onCheckedChangeListener?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
radiooncheckedchangelistenerlistview

Problem

This is my adapter class, to create a custom listView with three radiobuttons:

```
public class Attendancelist_customadapter extends ArrayAdapter {
///needs two arraylists - one for the peopledetails, and one for the attendance status. read from file. passed to this.
///then assemble into a ListView with customview - in this.
///three radio buttons per item. invited, not attending, attending.
///
Context context;
int layoutResourceId;
ArrayList people_attendance = null;
String[] attendance_status;
String ATTENDING ="ATTENDING";
String NOT_ATTENDING = "NOT ATTENDING";
String INVITED = "INVITED";
public Attendancelist_customadapter(Context context, int layoutResourceId, ArrayList people_attendance){
super(context, layoutResourceId, people_attendance);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.people_attendance = people_attendance;
this.attendance_status = new String[this.people_attendance.size()];

}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
attendance_holder holder = null;
People_Attendance people_array[] = people_attendance.toArray(new People_Attendance[people_attendance.size()]);
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);

holder = new attendance_holder();
holder.txtName = (TextView)row.findViewById(R.id.name_txt);
holder.txtNumber = (TextView)row.findViewById(R.id.number_txt);
holder.attendance_group = row.findViewById(R.id.Attendance_Group);
holder.not_attending = (RadioButton) holder.attendance_group.findViewById(R.id.not_attending_radio);
holder.attending = (RadioButton) holder.attendance_group.findViewById(R.id.attending_radio);

Solution

This is your lucky day, there is an "easier" way to do this. First, we create a custom inner class: (put this code inside your current class, just like you would do with a method)

private class MyCheckedChange implements CompoundButton.OnCheckedChangeListener {
    private final String status;
    public MyCheckedChange(String status) {
        this.status = status;
    }
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
        int position = (Integer) buttonView.getTag();
        if (isChecked == true){
            attendance_status[position] = status;
        }
    }
}


Then you can instantiate this class with a corresponding status value.

holder.attending.setOnCheckedChangeListener(new MyCheckedChange(ATTENDING));
holder.not_attending.setOnCheckedChangeListener(new MyCheckedChange(NOT_ATTENDING));
holder.invited.setOnCheckedChangeListener(new MyCheckedChange(INVITED));


Further suggestions: (use them if you like them)

Use an enum for attendance status:

public enum AttendingStatus {
    NOT_ATTENDING, ATTENDING, INVITED;
}


And use AttendingStatus[] attendance_status; instead of your string array.

This will open up the possibility of many things, if you would like:

You can match up your radiobuttons to an attendingstatus, by using a Map for example, which could reduce code duplication in the getChecked() method by grabbing a RadioButtons corresponding AttendingStatus by using AttendingStatus status = map.get(radioButton); and using status.toString().replaceAll("_", " "); to get it's string version.

I don't know your skill level so this might be a bit advanced for you, but if you are willing to learn, I really suggest that you start playing around with enums. You will love them :)

Spacing

`}else if(attending.isChecked()){`


is a bit tightly written, you can increase readability a bit by using more spaces, like this:

`} else if (attending.isChecked()) {`


Private, final, and constants

There are plenty of instance variables in your code that could be private, final, and possibly static.

String ATTENDING = "ATTENDING";
String NOT_ATTENDING = "NOT ATTENDING";
String INVITED = "INVITED";


All these are constants, they should never change, and should therefore be declared private static final

private static final String ATTENDING = "ATTENDING";
private static final String NOT_ATTENDING = "NOT ATTENDING";
private static final String INVITED = "INVITED";


I believe that all of your variable declarations above your constructor can be marked private, and I also believe that all them can be final:

private final Context context; 
private final int layoutResourceId;    
private final ArrayList people_attendance = null;
private final String[] attendance_status;


Making them private will let the compiler tell you if any of them are not used, and making them final will ensure you that once initialized in the constructor, their value / object-reference cannot change.

Naming

Using class names with _ in Java is unusual and not convention, PeopleAttendance would be a better name. Also, class names should start with an upper-case letter and use CamelCasing, making attendance_holder --> AttendanceHolder.

Overall though, I get the impression that you seem to know what you are doing coding-wise, good work :)

Code Snippets

private class MyCheckedChange implements CompoundButton.OnCheckedChangeListener {
    private final String status;
    public MyCheckedChange(String status) {
        this.status = status;
    }
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
        int position = (Integer) buttonView.getTag();
        if (isChecked == true){
            attendance_status[position] = status;
        }
    }
}
holder.attending.setOnCheckedChangeListener(new MyCheckedChange(ATTENDING));
holder.not_attending.setOnCheckedChangeListener(new MyCheckedChange(NOT_ATTENDING));
holder.invited.setOnCheckedChangeListener(new MyCheckedChange(INVITED));
public enum AttendingStatus {
    NOT_ATTENDING, ATTENDING, INVITED;
}
`}else if(attending.isChecked()){`
`} else if (attending.isChecked()) {`

Context

StackExchange Code Review Q#41218, answer score: 3

Revisions (0)

No revisions yet.