0

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!!

tyb
  • 4,749
  • 11
  • 31
  • 38

2 Answers2

3

use this db = new MyDatabase(getActivity()); instead of Context ctx = (Context)TabFragment6.this.getActivity(); and db = new MyDatabase(ctx);

Venkata Krishna
  • 1,543
  • 1
  • 11
  • 19
1

You cannot call getActivity() in the new View.OnClickListener() { interface for it doesn't have this method there. you may probably want to create final reference to your activity and use it within your onClick() method.

You are creating a new anonymous class which implements View.OnClickListener interface which doesn't have getActivity() method.

should be something like this:

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

             LinearLayout Layout6 = (LinearLayout) inflater.inflate(R.layout.tab_frag6_layout, container, false);
     final Activity activity = this.getActivity(); //this will call your tabfragment's associated activity 


     ............
 //and in your onClick()
             db = new MyDatabase(activity); // or try to use the Contexts or cast to them depends on your constructor parameters            

hope it helps

Sergey Benner
  • 4,421
  • 2
  • 22
  • 29
  • Hmm, how would I do that? Sorry, don't have much experience with android. – tyb Feb 13 '12 at 16:52
  • Sure you can, so long as there's an encapsulating instance of a `Fragment` you can cast as he's doing. I tried your exact code, tyb, and didn't have that error. Sure you saved after doing your cast? – Brian Dupuis Feb 13 '12 at 16:57
  • Yes, it is working now too. I had saved it, but the error didnt disappear until i modified the file. Must have been some glitch in Eclipse. Thanks for your time! – tyb Feb 13 '12 at 17:00