Inside a fragment, I have a button which I want to "trigger" the filling of a Listview from a database, using a Cursor Adapter.
The code for the fragment goes something like this:
public class TabFragment6 extends ListFragment {
/** (non-Javadoc)
* @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LinearLayout Layout6 = (LinearLayout) inflater.inflate(R.layout.tab_frag6_layout, container, false);
// Register for the Button.OnClick event
Button bShopGuit = (Button)Layout6.findViewById(R.id.buttonshopguit);
bShopGuit.setOnClickListener(new View.OnClickListener() { //abre setonclicklistener(
@Override
public void onClick(View v) {
Cursor allItems;
MyDatabase db;
Context ctx = (Context)TabFragment6.this.getActivity();
db = new MyDatabase(ctx);
allItems = db.getGuitarItems();
ListAdapter adapter = new SimpleCursorAdapter (ctx,
android.R.layout.simple_list_item_1,
allItems,
new String[] {"ItemName"},
new int[] {android.R.id.text1});
getListView().setAdapter(adapter);
}
}); //fecha )
return Layout6;
}
However, I'm getting an error in the adapter: The method getactivity() is undefined for the type new View.OnClickListener(){}
How can I fix this? Thanks!!