I'm having trouble creating a Listview adapter, here's the code.
import com.actionbarsherlock.app.SherlockFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Recipes extends SherlockFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ListView recipelist = (ListView) getView().findViewById(R.id.recipe_drawer);
String[] items ={ getResources().getString(R.string.block),
getResources().getString(R.string.tool),
getResources().getString(R.string.support),
getResources().getString(R.string.veggie)
};
ArrayAdapter<String> adapt = new ArrayAdapter<String>(Recipes.this, R.layout.recipes_list_item, items);
recipelist.setAdapter(adapt);
View rootView = inflater.inflate(R.layout.recipes, container, false);
return rootView;
}
}
The error I get is in ArrayAdapter<String> adapt = new ArrayAdapter<String>(Recipes.this, R.layout.recipes_list_item, items);
and it says
The constructor ArrayAdapter(Recipes,int,String[]); is undefined.
I tried changing Recipes.this to just this and I still get the same error. Any help would be appreciated!
Change Recipes.this
to getActivity()
. You are in a Fragment
, which is not a Context
, which is the first parameter to the ArrayAdapter
family of constructors.