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.

Answers


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


Use Class_name.this or define a Context variable. and then call your makeResultsbarVisible() by using Context variable,

Like,

Context c = this;

c.makeResultsbarVisible();

Need Your Help

django - why is the request.POST object immutable?

django post

As the title asks, why did the Django guys decide to implement the request.POST object with a querydict (which, of course, in turn, makes the whole thing immutable?)

Remove string after predefined string

javascript replace

I am pulling content from an RSS feed, before using jquery to format and edit the rss feed (string) that is returned. I am using replace to replace strings and characters like so: