Mick
Mick

Reputation: 7947

Compilation error creating an adapter in a fragment

I have a fragment which contains several views including a ListView. In order to set the adpater for the listview, I thought I'd recycle some old (working) code I'd previously written in an activity. The code in the activity looked something like this:

adapter = new myadapter(
            this,
            list_of_things,
            R.layout.custom_row_view,
            new String[] {"label","day","time"},
            new int[] {R.id.text1,R.id.text3, R.id.text2}
            );

Where myadapter was a method within my activity's class, defined like so...

public class myadapter extends SimpleAdapter { 

Now I tried to put the same myadapter method inside my fragment class and called

adapter = new myadapter(
            this,
            list_of_things,
            R.layout.custom_row_view,
            new String[] {"label","day","time"},
            new int[] {R.id.text1,R.id.text3, R.id.text2}
            );

but now I get a compile time error:

The constructor MyTestFragment.myadapter(MyTestFragment, ArrayList<HashMap<String,String>>, int, String[], int[]) is undefined

I don't know whether my bug could be some minor typo - or whether its something more fundamental, like not being allowed adapters within fragments.

Upvotes: 0

Views: 42

Answers (1)

bakriOnFire
bakriOnFire

Reputation: 2681

use getActivity() instead of this in myadapter

adapter = new myadapter(
            getActivity(),
            list_of_things,
            R.layout.custom_row_view,
            new String[] {"label","day","time"},
            new int[] {R.id.text1,R.id.text3, R.id.text2}
            );

because the this in myadapter is referencing your Fragment and not your Activity.

Upvotes: 1

Related Questions