문제

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!

올바른 솔루션이 없습니다

다른 팁

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!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top