Run adapter.notifyDataSetChanged () command outside the fragment

I have one fragment, which is a list of database values. I use ListView to display values ​​on screen.

I have another snippet containing buttons for adding records to the database. So my problem is that when I add a record to the database by clicking the button on the first fragment, I need a second fragment (or second page in the application) to update the list of database values ​​that are displayed on the screen.

Here is a snippet of the database list:

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 do adapter.notifyDataSetChanged () in another fragment that contains 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(); } }); 
+4
source share
2 answers

To contact FragA β†’ MainActivity β†’ FragB directly with a method (inside 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 added by me in MainActivity.java:

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

Code added by me 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 added by me in FragManage.java

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { ... String myTag = getTag(); ((MainActivity)getActivity()).setTabFragManage(myTag); ... } 
+1
source

you can use a listener with a basic action that updates the fragments

in the snippet 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 DB frag

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

then attach the listener to the main operation that creates the fragments

 ButtonsFragment.setOnDBListener(new OnDBListener() { @Override public void DBChanged(boolean DB_Bool) { DatabaseViewFrag.update(); } }); 

when updating the database in a call to a button fragment

 onDBChanged() 

to tell everything he updated

+2
source

All Articles