Updating listview through fragments

2019-01-20 14:56发布

问题:

here is my problem I have an activity that contains two fragments. Fragment A have a listview and Fragment B is in charge of updating the database now I don't know how to update the datas in listview inside Fragment A right after updating the data in Database from Fragment B. Can you help me and give a sample code for this? Thanks a lot.

回答1:

In this example, FragmentA call notify.

INotifier

public interface INotifier {
    public void notify(Object data);
}

Utils

public class Utils {
    public static INotifier notifier;
}

FragmentA

public FragmentA extends Fragment {

   public void onCreateView(...) {

   }

   public void inSomeMethod() {
        if (Utils.notifier != null) {
           Utils.notifier.notify(data);
        }
   }
}

FragmentB

public FragmentB extends Fragment implements INotifier {

   public void onCreateView(...) {
       Utils.notifier = this;   
   }

   @Override
   public void notify(Object data) {
       // handle data
   }
}