0

I have a class extending the Application class which I use for some global variables and methods.

That works fine in my activities and fragments, however now I would like to call a non static method of that class within my class which extends a CursorAdapter.

The tricky thing is that I could not find a way to retrieve a reference to the Application extended class from the CursorAdapter. Is there any way to do so?

Thomas
  • 2,751
  • 5
  • 31
  • 52

1 Answers1

0
(YourApplication)mContext.getApplicationContext();

Should give you a reference to the application object from within your class which extends CursorAdapter. You need to downcast to your specific application class. If this is not what you wanted, please supply some example of your source where you´re trying to call your application object.

Magnus
  • 1,483
  • 11
  • 14
  • I thought of doing it like that, but after reading http://stackoverflow.com/questions/5018545/getapplication-vs-getapplicationcontext I was unsure if `getApplicationContext()` would be the best way to go – Thomas Dec 03 '13 at 11:12
  • I was unaware of that limitation :( Are you sending your activity as the context to the class derived from cursoradapter? In that case cast mContext to activity and use getApplication() on the casted activity. Second option send an interface to your derived cursoradapter class and have the clients (activity) implementing this interface. – Magnus Dec 03 '13 at 12:40