TheLettuceMaster
TheLettuceMaster

Reputation: 15734

Accessing Class inside Fragment from Adapter in Android

I have a Fragment Activity which contains two Fragments. In one of the fragments I have a task class called HelpfulTask I generally access it like this:

new HelpfulTask().execute();  

But I need to access it from inside my adapter which is in a separate class outside of this Fragment activity.

Again, here is layout:

MainActivity extends FragmentActivity {

     Fragment A {

     }


     Fragment B extends ListFragment {

         class HelpfulTask extends AsyncTask {

            // How do I call/access this from separate Arraydapter?

         }

     }

}

Upvotes: 0

Views: 1168

Answers (1)

Eng.Fouad
Eng.Fouad

Reputation: 117597

Two options:

  • access it via an instance of FragmentA, like:
MainActivity a = ... // get reference to the activity instance
MainActivity.FragmentA fa = a.new FragmentA();
MainActivity.FragmentA.HelpfulTaskextends h = fa.new HelpfulTaskextends();
  • Declare the inner classes as static, so that you can access them in static way (without instances):
MainActivity.FragmentA.HelpfulTaskextends h = new MainActivity.FragmentA.HelpfulTaskextends();

Upvotes: 1

Related Questions