4

I have one fragment that is a list of database values. I am using a ListView to display the values to the screen.

I have another fragment that contains the buttons to add entries to the database. So my problem is, is that when I add an entry to the database by hitting a button on the first fragment, I need the second fragment (or second page in the app) to update the list of database values that are shown on the screen.

Here is the database list fragment:

public class FragManage extends Fragment {

private PunchesDataSource datasource;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {

    datasource = new PunchesDataSource(getActivity());
    datasource.open();

    List<Punch> values = datasource.getAllPunches();

    // Use the SimpleCursorAdapter to show the elements in a ListView
    ArrayAdapter<Punch> adapter = new ArrayAdapter<Punch>(getActivity(),
        android.R.layout.simple_list_item_1, values);

    View myFragmentView = inflater.inflate(R.layout.fragment_manage, container, false);

    ListView list = (ListView) myFragmentView.findViewById(R.id.list);
    list.setAdapter(adapter);

    return myFragmentView;
    }
}

So I just want to perform adapter.notifyDataSetChanged() in my other fragment that contains the buttons (something like this):

    // Punch OUT button.
    Button out_button = (Button) myFragmentView.findViewById(R.id.out_button);
    out_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            datasource.createPunch("out-" + getTime());

            FragManage.adapter.notifyDataSetChanged();
        }
      });

2 Answers 2

2

you could use a listener with the main activity that updates the fragments

in the frag with buttons add these

private void onDBChanged(){
    OnDBListener.DBChanged(true);
}

public void setOnDBListener(OnDBListener listener){
    this.OnDBListener = listener;
}

public interface OnDBListener{
    public void DBChanged(boolean DB_Bool);
}

then in the DB frag

public void update(){
    adapter.notifyDataSetChanged();
}

then attach the listener in the Main activity that creates the frags

ButtonsFragment.setOnDBListener(new OnDBListener() {
    @Override
    public void DBChanged(boolean DB_Bool) {

        DatabaseViewFrag.update();
    }
});

when you update the database in the buttons fragment call

onDBChanged() 

to tell everything it has updated

4
  • I am trying to implement your solution, but I am running into some problems. Is onDBChanged() supposed to be DBChanged()? Also, what is fsChanged(true) stand for / supposed to do? Thank you for taking the time to help.
    – Zach R
    Jan 19, 2013 at 3:14
  • 1
    sorry mate i had a typo :P i have fixed the code and added the rest i forgot.
    – pvyParts
    Jan 19, 2013 at 4:31
  • Alright I have one more problem, how can I access adapter.notifyDataSetChanged(); in the update() method? I tried doing this: ((BaseAdapter) ((ListView)getActivity().findViewById(R.id.list)).getAdapter()).notifyDataSetChanged() but it says the method update() must not be static. The method update() is static because of FragManage.update().
    – Zach R
    Jan 19, 2013 at 5:32
  • 1
    Hey man thanks for all the help but I couldn't get the handler to fix that problem. Luckily I found another solution using a unique fragment method. See my solution if you want.
    – Zach R
    Jan 20, 2013 at 9:51
1

In order to communicate from FragA -> MainActivity -> FragB directly to a method (within FragB) that cannot be static , this solution should work for you.

How it works here: http://android-er.blogspot.com/2012/06/communication-between-fragments-in.html

Code I added in MainActivity.java:

String FragManage;

public void setTabFragManage(String t){
    FragManage = t;
}

public String getTabFragManage(){
 return FragManage;
}

Code I added in FragPunch.java

public void onClick(View v) {

...

String TabOfFragManage = ((MainActivity)getActivity()).getTabFragManage();

FragManage fm = (FragManage)getActivity()
    .getSupportFragmentManager()
    .findFragmentByTag(TabOfFragManage);

fm.update(); //This is what is NOT possible before, but now is.
}

Code I added in FragManage.java

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    ...
    String myTag = getTag();

        ((MainActivity)getActivity()).setTabFragManage(myTag);
    ...
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.