MAOL MAOL - 7 days ago 5
Android Question

How to get context of the implemented callback inside a method of a fragment

I've defined the next header for a Fragment:

public class AllVideosFragment extends Fragment implements AutomaticCallback{


Inside of this Fragment, I have the next function to handle a spinner:

// Listener called when spinner item selected
spinnerLanguages.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View v, int position, long id) {
// Get selected row data to show on screen
String language = ((TextView) v.findViewById(R.id.tvSpinnerRow)).getText().toString();

if(language.equals(getString(R.string.str_en)))
Hawk.put(Config.TV_LANGUAGE, "en");
if(language.equals(getString(R.string.str_es)))
Hawk.put(Config.TV_LANGUAGE, "es");

//We need to retrive data again
new AutomaticRequest().getLives(String.valueOf(Hawk.get(Config.TV_LANGUAGE)), **CONTEXT OF THE CALLBACK**);
}

@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}

});


If I would have the method outside of the spinner handle method, I could use "this", but inside I don't know what I need to use.

new AutomaticRequest().getLives(String.valueOf(Hawk.get(Config.TV_LANGUAGE)), this);


Which method I need to call in replacement of "this"?

Thank you for your help.

Answer

You are in an anonymous scope and this will refer to the scope. If you want to pass an outer this you have to explicitly name it in this case

AllVideosFragment.this