0

This is my app architecture so far:

I have a MainActivity which has a Fragment which has an Adapter:

Activity > Fragment > Adapter

I update my SQLiteDb from my Activity, I would like to know how I can call the method:

adapter.notifySetDataChange();

from there (or from my DatabaseHandler Class) DatabaseHandler is just a class that I can create anywhere and retrieve my SQLiteDB.

I guess this might be a suitable use case for the observer pattern, but I'm not quite sure how I can go about it.

frankelot
  • 13,666
  • 16
  • 54
  • 89
  • http://stackoverflow.com/questions/3669325/notifydatasetchanged-example/5092426#5092426 – Gokhan Arik Jun 12 '14 at 14:34
  • Right, I know I have to cal the **notifySetDataChanged()** on my adapter. problem is, I have no way of getting to it. – frankelot Jun 12 '14 at 14:46
  • 1
    First of all, it is not notifySetDataChanged. It is notifyDataSetChanged. Secondly, we don't even know what your code looks like so we can't say anything. We don't know what kind of adapter you are using, how are you passing data. Post code or give more information – Gokhan Arik Jun 12 '14 at 14:51

2 Answers2

0

I would recommend to use cursor loader pattern, which will update the adapter automatically: http://developer.android.com/training/load-data-background/setup-loader.html

Evgeni Roitburg
  • 1,958
  • 21
  • 33
  • This is intresting, but I already know how to update my DB :) Im using retrofi to do so. My problem is updating adapters once the SQLite gets updated (I might be structuring my app the wrong way and that's why I'm having trouble) – frankelot Jun 12 '14 at 14:47
0

You can expose a public method (e.g. updateMyAdapter()) in your fragment that updates your adapter and call it from your activity. You can get the fragment from your activity using

YourFragment f = (YourFragment)getFragmentManager().findFragmentById(...);
if(f != null)
    f.updateMyAdapter();
hfann
  • 599
  • 3
  • 13