0

I'm new to Android and I'm trying to create a GridView inside a Fragment. But the line

addressGrid.setAdapter(new Adapter(this));

keeps giving me an error. In my code:

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

    View rootView = inflater.inflate(R.layout.fragment_name, container, false);

    addressGrid = (GridView) rootView.findViewById(R.id.gridView1);
    addressGrid.setAdapter(new Adapter(this));

    return rootView;
}

I've tried to replace the (newAdapter(this)); with

(new Adapter(getActivity())); 

and

(new Adapter(getContext())); 

which are referenced on the Android Developer site but neither seem to work. I'm really hoping someone can help me figure out why I am getting this error and how to fix it. Thank you in advance!

Seslyn
  • 807
  • 10
  • 19

1 Answers1

0

Adapter is an interface, you re trying to instantiate an interface(which is not possible in Java), you should create a class that implementes Adapter and write your code or use one of the already provided classes like SimpleArrayAdapter, CursorAdapter, etc. My recommendation would be, create a class that extends from BaseAdapter if you want a specific layout in your views and some unique functionality for it.

You can look at this question for a better understanding on how to implement your own adapter. How to customize listview using baseadapter

Hope it Helps!

Regards!

Community
  • 1
  • 1
Martin Cazares
  • 13,637
  • 10
  • 47
  • 54