Search code examples
javaandroidandroid-fragmentsandroid-adapterandroid-cursoradapter

How to obtain adapter from fragment in activity without getting null pointer exception?


As the title say, what's the way to get instance of CursorAdapter created in my CustomListFragment. MainActivity layout contains CustomListFragment in layout file. I added method getAdapter() in CustomListFragment, and in MainActivity I created instance of fragment, but when I call method from MainActivity in which I have method call of getAdapter() i.e. adapter = customListFragment.getAdapter(); exeception occurs.

Is there any way to avoid that duplicate code and do all method calls and adapter initializations in one class? Also, I tried with interface, but no luck.


Solution

  • I have another activity for adding items to db, but, when I return back to main activity, added item is missing until I refresh and change cursor.

    It should suffice to change cursor in your fragment's onResume(). No need to make parent activity involved.