Basically what I have is a fragment which displays a ListView. It currently uses an ArrayAdapter. However I'm trying to extend the ArrayAdapter to make my own custom adapter. Then, when I change the code to use my new adapter, the following error appears:
"No enclosing instance of type MyActivity is accessible. Must qualify the allocation with an enclosing instance of type MyActivity (e.g. x.new A() where x is an instance of MyActivity)."
Here's the code: Note this is all nested within MyActivity
public static class MyFragment extends ListFragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public MyFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_fixed_tab, container, false);
// Temporarily get the content from an array
String [] values = new String[] { "Item1", "Item2", "Item3", "Item4" };
/******** This has no error as it is, but if I change it to CustomListAdapter, it shows the error ********/
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), R.layout.workout_row, R.id.workout_name, values);
setListAdapter(adapter);
return rootView;
}
}
private class CustomListAdapter extends ArrayAdapter<String> {
String[] list;
public CustomListAdapter(Context context, int resource,
int textViewResourceId, String[] array) {
super(context, resource, textViewResourceId, array);
list = array;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = super.getView(position, convertView, parent);
CheckBox cb = (CheckBox) findViewById(R.id.checkbox);
cb.setTag(position);
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
// TBI
}
}
});
return row;
}
}