问题
I created the following DialogFragment deriving it from the Android documentation:
public class PayBillDialogFragment extends DialogFragment{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
final Bundle b = this.getArguments();
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Paga bollettino")
.setPositiveButton("Paga", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES!
}
})
.setNegativeButton("Cancella", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
From another Fragment (a ListFragment), when a row of the list is clicked the DialogFragment should be opened and after pressing the positive button of the DialogFragment I want to be able to remove the selected row of the ListFragment and also to call a method to perform a remote action associated to the removal. I implemented the ListFragment as follows:
public static class ListFragment extends android.support.v4.app.ListFragment {
ArrayList<String> listItems=new ArrayList<String>();
ArrayAdapter<String> adapter;
public static final String ARG_SECTION_NUMBER = "section_number";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.list_fragment_view,
container, false);
ListView lv = (ListView)rootView.findViewById(android.R.id.list);
}});
adapter=new ArrayAdapter<String>(this.getActivity(),
android.R.layout.simple_list_item_1,
listItems);
setListAdapter(adapter);
return rootView;
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
//opening the dialogfragment
}
}
}
What I don't know is how to handle the action after the click of the positive button of the DialogFragment. Can you help me?
EDIT: to clarify, this is the workflow: click on the list -> show the DialogFragment -> after click on DialogFragment remove the element from the list.
回答1:
This is how I handle communication between fragment and dialog fragment
Example fragment:
public class MainFragment extends Fragment {
private static final int REQ_CODE = 1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.main_fragment, container, false);
Button b = (Button) v.findViewById(R.id.button);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MyDialog dialog = new MyDialog();
dialog.setTargetFragment(MainFragment.this, REQ_CODE);
dialog.show(getFragmentManager(), "dialog");
}
});
return v;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Toast.makeText(getActivity(), "Result: " + resultCode,
Toast.LENGTH_SHORT).show();
super.onActivityResult(requestCode, resultCode, data);
}
}
Example DialogFragment:
public class MyDialog extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("My dialog message")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
notifyToTarget(Activity.RESULT_OK);
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
notifyToTarget(Activity.RESULT_CANCELED);
}
});
return builder.create();
}
private void notifyToTarget(int code) {
Fragment targetFragment = getTargetFragment();
if (targetFragment != null) {
targetFragment.onActivityResult(getTargetRequestCode(), code, null);
}
}
}
This is the only method I got working also when changing orientation.
回答2:
You've got two options to call the ListFragment
from PayBillDialogFragment
.
First one is recommended by the Android guidelines. All the communication goes through the hosting Activity
. That's how you get the hosting Activity
by calling ((HostingActivity)PayBillDialogFragment.getActivity()).deleteItem()
inside PayBillDialogFragment.setPositiveButton(onClick())
. In HostingActivity.deleteItem()
get the inflated PayBillDialogFragment
and call some delete method in it.
See http://developer.android.com/guide/components/fragments.html#EventCallbacks
Second. You can just DialogFragment.setTargetFragment()
when creating the DialogFragment object and then inside PayBillDialogFragment.setPositiveButton(onClick())
you can get PayBillDialogFragment
by DialogFragment.getTargetFragment()
and call the delete method there.
See Callback to a Fragment from a DialogFragment
回答3:
To call dialog you can use this:
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
if (fm.findFragmentByTag("PayBillDialogFragment") == null) {
PayBillDialogFragment payBillDialogFragment = new PayBillDialogFragment();
payBillDialogFragment.show(fm, "PayBillDialogFragment");
}
In your dialogFragment,
setPositiveButton("Paga", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//Add your code here that should execute
//when positive button is clicked
}
});
回答4:
The List Fragment would be using an Adapter to show elements. The adapter requires input in the form of some Collection. So, what you can do is, when the user presses say ok button on dialog fragment and you communicate that back to List Fragment, you can remove that particular element from the Collection and again set the Adapter of List Fragment with modified collection.
来源:https://stackoverflow.com/questions/15970196/how-to-execute-action-after-dialogfragment-positive-button-clicked