You could use Intents and Intent Filters with a Broadcast Receiver
for this.
- Create a BroadcastReceiver instance in the fragment where you want to update the data.
- Create an IntentFilter and set an action string (maybe 'db.update') to it and register it with your application context (you could do this via your fragment by calling getActivity().getApplicationContext().registerReceiver(receiver, filter).
- In your AlertDialog, after you update your database, create an Intent with the same action string you set above (in our case, 'db.update') and use context to send it out (getActivity().getApplicationContext().sendBroadcast(intent)). Your BroadcastReceiver's onReceive() method would be called in your fragment and you can call the method to refresh or reload your data there. See sample code below:
Say this is your fragment
public class YourFragment extends Fragment {
private GridView mGridView;
private BaseAdapter mAdapter;
private BroadcastReceiver mReceiver;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//inflate view as usual
View view = inflater.inflate(R.layout.yourlayour, container, false);
...
//create instance of broadcast receiver
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) { //when intent is receiver, this method is called
if(intent.getAction().contentEquals("db.update")){
//update intent received, call method to refresh your content loader
refreshFragment();
}
}
};
//create a new intent filter
IntentFilter mDataUpdateFilter = new IntentFilter("db.update");
//register our broadcast receiver and intent filter
getActivity().getApplicationContext().registerReceiver(mReceiver, mDataUpdateFilter);
return view;
}
@Override
public void onDestroy() {
super.onDestroy();
//never forget to unregister the receiver when you're done, it could cause your app to crash
//if it receives an intent and calls null pointing methods in your code
getActivity().getApplicationContext().unregisterReceiver(mReceiver);
} }
Then in your AlertDialog as you did above, send the intent to this receiver by:
DataBaseHelper dbh = DataBaseHelper(this);
...
positiveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dbh.addVal(new TheData(editName.getText().toString(), editAge.getText().toString())); //adds a row to the Sqlite Db
//Create an intent with our action
Intent updateIntent = new Intent("db.update");
//send the intent by
getContext().getApplicationContext().sendBroadcast(updateIntent);
dialog.dismiss();
}
});
...
Don't forget to unregister your broadcast receiver when your fragment is destroyed. Call getActivity().getApplicationContext().unregisterReceiver(receiver); in your onDestroy() method.
I should also point out that the onReceive() method of your broadcast receiver would always be called on the main thread, even if you send your intent from a background thread.