Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to call getActivity() in OnItemClickListener()?

I am trying to call getActivity() in the OnItemClickListener:

class ViewTest{ //called in a fragment
setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int p, long i) {
                ((MainActivity) getActivity()).makeResultsbarVisible();
...
            }

        });

}

In the fragment, the class is instantiated as:

ViewTest editTest = new ViewTest(this);

But I get an error that I cannot fix:

The method getActivity() is undefined for the type new AdapterView.OnItemClickListener(){}

How can I call getActivity inside onItemClick()? Thanks.

like image 987
user2212461 Avatar asked Dec 12 '13 10:12

user2212461


1 Answers

You can only use getActivity inside a Fragment class or one extending it. If your onItemClickListener is in an Activity use MainActivity.this

like image 174
Apoorv Avatar answered Sep 20 '22 19:09

Apoorv