1

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;
    }
}
Gak2
  • 2,661
  • 1
  • 16
  • 28

2 Answers2

1

Create a separate .java file as CustomListAdapter.java and copy your adapter code there

In constructor of CustomListAdapter

 LayoutInflater inflater;
 public CustomListAdapter(Context context, int resource,
        int textViewResourceId, String[] array) {
    super(context, resource, textViewResourceId, array);
    list = array;
    inflater = LayoutInlfater.from(context); 
}

In getView

View row = inflater.inflate(R.layout.workout_row,parent,null); // inflate custom layout
CheckBox cb = (CheckBox) row.findViewById(R.id.checkbox);
// use the inflated view object to initialize checkbox

Also it is better to use a ViewHolder pattern

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

your adapter needs to be inside your MyFragment class, currently it is outside your class brackets

tyczj
  • 71,600
  • 54
  • 194
  • 296