Question

I've got a Fragment containing it's own layout. This layout has a ListView. Now I've tried to add an adapter for this ListView like this:

public static class LatestFragment extends Fragment {
    ListView listView;
    String[] values = {
            "Test", "Test2"
    };

    public LatestFragment() {

    }
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_latest, container, false);
        listView = (ListView) rootView.findViewById(R.id.fragment_latest_listview1);
        listView.setAdapter(new CustomListAdapter(getActivity(), values));
        return rootView;
    }
}

But I keep getting an error at

new CustomListAdapter(getActivity(), values)

Eclipse tells me following:

No enclosing instance of type MainActivity is accessible. Must qualify the
allocation with an enclosing instance of type MainActivity (e.g. x.new A() 
where x is an instance of MainActivity).

Why can't I set up an adapter for my ListView?

Was it helpful?

Solution

My guess is that CustomListAdapter is not defined as a static inner class, and therefore it cannot be used from another static inner class.

Also, please get rid of the constructor, or at least chain to the superclass constructor.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top