I am trying to figure out a way for the position variable from the getView()
method to be passed to the inner class. However this can't be a final variable because getView()
gets called for each item in the ListView
and hence it changes. Is there a way to access that position variable without having it be final? or making it final in a clever way that would make something like this work? Here is my code
public class AlarmListAdapter extends ArrayAdapter<Alarm> {
public AlarmListAdapter(Context context, int resource, List<Alarm> alarms) {
super(context, resource, alarms);
}
@Override
public View getView(int position, View view, ViewGroup parent) {
if (view == null) {
LayoutInflater viewInflator = LayoutInflater.from(getContext());
view = viewInflator.inflate(R.layout.item_alarm_list, null);
}
Alarm alarm = getItem(position);
if (alarm != null) {
TextView alarmName = (TextView) view
.findViewById(R.id.alarm_list_item_name);
TextView alarmTime = (TextView) view
.findViewById(R.id.alarm_list_item_time);
Switch status = (Switch) view
.findViewById(R.id.alarm_list_item_status);
alarmName.setText(alarm.getName());
alarmTime.setText(alarm.getFormattedHour() + ":"
+ alarm.getMinute() + " " + alarm.getAMPM());
status.setChecked(alarm.getOn());
status.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Alarm alarm = getItem(position);
} else {
// The toggle is disabled
}
}
});
}
return view;
}
}