0

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;
    }
}
Anto
  • 4,147
  • 14
  • 61
  • 112
9er
  • 1,434
  • 2
  • 15
  • 36

1 Answers1

1

If you make the position variable final, it will be OK. You're using it as a read only variable. Also making it final has no effect on the caller, only on the body of your method you won't be allowed to change it.

isah
  • 5,013
  • 3
  • 23
  • 35