我创建了以下从 Android 文档派生的 DialogFragment:
公共类 PayBillDialogFragment 扩展 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();
}
}
从另一个片段(ListFragment)中,当单击列表的一行时,应该打开 DialogFragment 并且在按下 DialogFragment 的肯定按钮后,我希望能够删除 ListFragment 的选定行并调用一个方法来执行与删除相关的远程操作。我实现了 ListFragment 如下:
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
}
}
}
我不知道的是如何处理DialogFragment的正按钮点击后的动作。你能帮助我吗?
编辑:澄清一下,这是工作流程:单击列表-> 显示 DialogFragment -> 单击 DialogFragment 后从列表中删除元素。