Android 单击DialogFragment正按钮后如何执行操作
我根据Android文档创建了以下DialogFragment: 公共类PayBillDialogFragment扩展了DialogFragment{Android 单击DialogFragment正按钮后如何执行操作,android,android-listfragment,android-dialogfragment,Android,Android Listfragment,Android Dialogfragment,我根据Android文档创建了以下DialogFragment: 公共类PayBillDialogFragment扩展了DialogFragment{ @Override public Dialog onCreateDialog(Bundle savedInstanceState){ final Bundle b = this.getArguments(); // Use the Builder class for convenient dial
@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
}
}
}
公共静态类ListFragment扩展了android.support.v4.app.ListFragment{
ArrayList listItems=新的ArrayList();
阵列适配器;
公共静态最终字符串ARG\u SECTION\u NUMBER=“SECTION\u NUMBER”;
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
最终视图根视图=充气机充气(R.layout.list\u fragment\u视图,
货柜(虚假);;
ListView lv=(ListView)rootView.findviewbyd(android.R.id.list);
}});
适配器=新的ArrayAdapter(this.getActivity(),
android.R.layout.simple\u list\u item\u 1,
清单项目);
setListAdapter(适配器);
返回rootView;
}
@凌驾
public void onListItemClick(列表视图l、视图v、整数位置、长id){
//打开对话框片段
}
}
}
我不知道的是在点击DialogFragment的肯定按钮后如何处理该操作。你能帮我吗
编辑:为了澄清,这是工作流程:单击列表->显示对话框片段->单击对话框片段后从列表中删除元素。要调用对话框,可以使用以下方法:
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
if (fm.findFragmentByTag("PayBillDialogFragment") == null) {
PayBillDialogFragment payBillDialogFragment = new PayBillDialogFragment();
payBillDialogFragment.show(fm, "PayBillDialogFragment");
}
在你的对话片段中
setPositiveButton("Paga", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//Add your code here that should execute
//when positive button is clicked
}
});
您有两个选项可以从
PayBillDialogFragment
调用ListFragment
首先安卓指南推荐一个。所有通信都通过托管活动进行。这就是通过调用((HostingActivity)PayBillDialogFragment.getActivity()).deleteItem()
内部PayBillDialogFragment.setPositiveButton(onClick())
来获取托管活动的方法。在HostingActivity.deleteItem()
中获取膨胀的PayBillDialogFragment
并在其中调用一些删除方法
看
秒。在创建DialogFragment对象时,您只需DialogFragment.setTargetFragment()
,然后在PayBillDialogFragment.setPositiveButton(onClick())
中,您可以通过DialogFragment.getTargetFragment()
获得PayBillDialogFragment
,并在那里调用delete方法
请参见这是我如何处理片段和对话框片段之间的通信
示例片段:
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);
}
}
对话框片段示例:
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);
}
}
}
这是我在更改方向时使用的唯一方法。列表片段将使用适配器显示元素。适配器需要某种集合形式的输入。所以,您可以做的是,当用户按下对话框片段上的“确定”按钮,然后您将其传回列表片段时,您可以从集合中删除该特定元素,然后再次使用修改后的集合设置列表片段的适配器。我不明白。如何从onClick方法中删除arraylist/arrayadapter中的元素?