Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7503603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:16:59+00:00 2026-05-29T21:16:59+00:00

Inside a fragment, I have a button which I want to trigger the filling

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T21:17:02+00:00Added an answer on May 29, 2026 at 9:17 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have an Activity which has a FragmentPagerAdapter and inside each fragment I
I'm using Rails 3. I want to display generated html fragment inside erb template
I have a fadeout animation in a view (which is inside a fragment), and
When I load a gridview (with adapter weekAdapter) from inside a fragment (weekFragment) for
I'm trying to show a MapView inside a fragment (using the hacked compatibility library).
Inside my code I'm generating hashes of URLs, (which are practically of unbounded length).
Inside my template function I have the following code: TypeName myFunction() { TypeName result;
I want to create custom templated control which controls inside of template coluld be
I have a SeachView inside of a Fragment in an Android 3.1 application currently
Basically I have an HTML fragment with <br> and <p></p> inside. I was able

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.