0

it is really strange, many people has asked this question but not even one useful answer

I have a MainActivity class

public class MainActivity extends FragmentActivity implements ActionBar.TabListener

with 3 actionbar tabs (sections 1,2,3) each section has its own fragment in the package

public class FragmentA extends Fragment
public class FragmentB extends Fragment
public class FragmentC extends Fragment

in the FragmentA I have a listView with its arraylist adapter.

in the MainActivity I fetch some data and put it in the sqlite database.

as soon as new data are added to database I want to notify the listview that the dataset is changed and populate the new data

it sounds easy but I have really got stucked in that ! I just want to know how to do this refresh, rebuild, recreate ... what ever it is from MainActivity

if any one can help ? thanks

Amir-Mousavi
  • 4,273
  • 12
  • 70
  • 123

3 Answers3

0

The simplest is to use a Local Broadcast manager. developer.android.com/reference/android/support/v4/content/… You can register your UI to receive a broadcast which you can fire when the data has been saved in the database. When the UI receives this event you can notify your adapter to refresh the list view

Ajit Pratap Singh
  • 1,299
  • 12
  • 24
  • this is exactly my question where should I notify the FragmentA to get refreshed , or notify the listadapter in that for dataSetChaned – Amir-Mousavi Jan 25 '14 at 07:44
  • 1
    The simplest is to use a Local Broadcast manager. http://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html You can register your UI to receive a broadcast which you can fire when the data has been saved in the database. When the UI receives this event you can notify your adapter to refresh the list view – Ajit Pratap Singh Jan 25 '14 at 07:54
  • You have enough reputation to post this as a comment. So this is the not as answer. This should be comment.!! – Piyush Jan 25 '14 at 07:57
  • @Piyush - Thanks for the information. Check this on how to use a local broadcast manager http://stackoverflow.com/questions/8802157/how-to-use-localbroadcastmanager – Ajit Pratap Singh Jan 25 '14 at 07:58
  • broadcast is not useful here! the FragmentA receives the broadcast and in onReceive() I call myAdapter.notifyDataSetChanged but nothing happens – Amir-Mousavi Jan 25 '14 at 09:24
  • yeyeye the Broadcast works , the best way for notifying the change , but my stupid mistake was in the instance of the database handler ; any time that the broadcast is received I should make new instance of the database. but broadcasting idea was awesome thanks – Amir-Mousavi Jan 25 '14 at 09:40
0

in your activity.

this.fragment.adapter.notifyDataSetChanged();
Muhammad Aamir Ali
  • 20,419
  • 10
  • 66
  • 57
0

Hehe.

Is this the poor idea?

public interface INotifyDataChange {
    public void notify(Object dataChanged); // Object stand for whatever you want. :">
}

You MainActivity

MainActivity {
    public static INotifyDataChange notifier;
    public void inSomeMethod() {
       // do input data.
       if (notifier != null) {
           notifier.notify(dataWillBeAttached);
       }
    }
}

Your Fragment:

FragmentA or B, or C implements INotifyDataChange {
    onCreateView() {
       MainActivity.notifier = this;
    }

    @Override
    public void notify(Object data) {
        Toast.make(..., "I've changed data " + data.toString(), ...).show();
        yourListView.notifyDataSetChanged(); // or anything like this.
    }

}
Luc
  • 2,800
  • 2
  • 25
  • 46