I have a class "ListFrag" implementing ListFragment and within that class, I have an nested static DialogFragment class "SortDialogFragment" which is invoked through one of the options in the options menu. The purpose of the dialog is to provide the user with the set of options on how they want the list to be sorted. I am able to display the dialog just fine and the user is able is to choose the option. However, I am not sure how I would refresh the listview without having access to listview's "notifyDataSetChanged" method from the DialogFragment class, once the user has made their choice. I do know about communicating between two fragments through the underlying activity using an interface but I am not clear about how I would apply that technique in this particular case. I am not sure where I would define my interface and who would implement that interface. Please bear with me since I am a newbie Android Developer and also to this site, in terms of posting questing. Below is my modified code, only including relevant code:
public class ListFrag extends SherlockListFragment {
private ListFragListener myListener;
private DatabaseAdapter database;
private ArrayList<MyObject> listItems;
private View layout;
private Button confirmBtn;
private Button cancelBtn;
private boolean isDeleteActive;
private boolean isExportActive;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
layout = inflater.inflate(R.layout.list_frag, container, false);
return layout;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
database = DatabaseAdapter.getInstance(getActivity());
database.open();
listItems = database.getMyObjects();
database.close();
setHasOptionsMenu(true);
registerForContextMenu(this.getListView());
final MyAdapter listAdapter = new MyAdapter(getActivity(), listItems);
setListAdapter(listAdapter);
confirmBtn = (Button) layout.findViewById(R.id.itemDelete);
cancelBtn = (Button) layout.findViewById(R.id.itemCancel);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.listview_omenu, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
int itemId = item.getItemId();
if (itemId == R.id.listOMItem3) {
SortDialogFragment sortDialog = SortDialogFragment.newInstance(listItems);
sortDialog.show(getFragmentManager(), "Sort Dialog");
}
return true;
}
public static class SortDialogFragment extends SherlockDialogFragment {
public static SortDialogFragment newInstance(ArrayList<MyObject> objectsToSort) {
SortDialogFragment sortDialog = new SortDialogFragment();
Bundle args = new Bundle();
args.putParcelableArrayList("MyObjects to sort", objectsToSort);
sortDialog.setArguments(args);
return sortDialog;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final ArrayList<MyObjects> objectsToSort = getArguments().getParcelableArrayList("MyObjects to sort");
builder.setTitle("Sort objects by...");
builder.setSingleChoiceItems(R.array.sortDialogOptions, checkedItem, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
if (which == 0) {
Collections.sort(objectsToSort, new ComparatorOne());
} else if (which == 1) {
Collections.sort(objectsToSort, new ComparatorTwo());
} else {
Collections.sort(objectsToSort, new ComparatorThree());
}
?????????????????????????????????????
// This is where I am trying to refresh the list before dismissing the dialog but I
// I apparently do not have access to listview's adapter to call the "notifyDataSetChanged" method.
dialog.dismiss();
}
});
builder.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
return builder.create();
}
}
}
First to refresh your listview you have to call notifyDataSetChanged
on the adapter that you've set for your listview. Start by changing final MyAdapter listAdapter
to a field instead of a variable.
Next step is to pass the adapter to the constructor of your dialog and store it in a field. The header of your SortDialogFragment
should look like this:
public static class SortDialogFragment extends SherlockDialogFragment {
private MyAdapter mAdapter;
public static SortDialogFragment newInstance(ArrayList<MyObject> objectsToSort, MyAdapter adapter) {
SortDialogFragment sortDialog = new SortDialogFragment();
sortDialog.setAdapter(adapter);
Bundle args = new Bundle();
args.putParcelableArrayList("MyObjects to sort", objectsToSort);
sortDialog.setArguments(args);
return sortDialog;
}
public void setAdapter (MyAdapter adapter){
mAdapter = adapter;
}
(...)
Next pass the adapter when you create your SortDialogFragment. Should look like this:
SortDialogFragment sortDialog = SortDialogFragment.newInstance(listItems, listAdapter);
Finally you just have to call mAdapter.notifyDataSetChanged();
on ????????????????