如何从Android中的DialogFragment获取结果?

时间:2017-10-02 07:26:45

标签: android dialogfragment

我的Dialog片段如下面的代码。

$query = "SELECT * FROM urls WHERE member_id='$mem_id'";
$query_run = mysql_query($query);

$totalclicks= 0;
while ($num = mysql_fetch_assoc ($query_run)) {
    $totalclicks += $num['clicks'];
}
echo $totalclicks;

在这里,如果我按下提交按钮,那么我必须在我的MainFragment类中获得结果。

我调用了DialogFragment这样调用

public class PromoCodeDialogFragment extends BaseDialog implements                                           View.OnClickListener {
private Test mBinding;

private String mHeader;
private String mVehicleNumber;

public static PromoCodeDialogFragment newInstance(String mHeader, String mVehicleNumber) {
    PromoCodeDialogFragment dialog = new PromoCodeDialogFragment();
    dialog.mHeader = mHeader;
    dialog.mVehicleNumber = mVehicleNumber;
    return dialog;
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);

    getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    getDialog().getWindow().setBackgroundDrawableResource(android.R.color.transparent);

    mBinding = DataBindingUtil.inflate(inflater, R.layout.test, container, false);

    mBinding.title.setText(mHeader);
    setCancelable(true);

    mBinding.submit.setOnClickListener(this);
    mBinding.cancel.setOnClickListener(this);

    return mBinding.getRoot();
}

@Override
public void onStart() {
    super.onStart();
    getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
}

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.submit:
            submit();

            break;

        case R.id.cancel:
            dismiss();
            break;
    }
}

现在,当我点击PromoCodeDialogFragment中的提交按钮时,我需要在片段中获取响应。

2 个答案:

答案 0 :(得分:1)

片段类:

public class MyFragment extends Fragment {
int mStackLevel = 0;
public static final int DIALOG_FRAGMENT = 1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (savedInstanceState != null) {
        mStackLevel = savedInstanceState.getInt("level");
    }
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("level", mStackLevel);
}

void showDialog(int type) {

    mStackLevel++;

    FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
    Fragment prev = getActivity().getFragmentManager().findFragmentByTag("dialog");
    if (prev != null) {
        ft.remove(prev);
    }
    ft.addToBackStack(null);

    switch (type) {

        case DIALOG_FRAGMENT:

            DialogFragment dialogFrag = MyDialogFragment.newInstance(123);
            dialogFrag.setTargetFragment(this, DIALOG_FRAGMENT);
            dialogFrag.show(getFragmentManager().beginTransaction(), "dialog");

            break;
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch(requestCode) {
            case DIALOG_FRAGMENT:

                if (resultCode == Activity.RESULT_OK) {
                    // After Ok code.
                } else if (resultCode == Activity.RESULT_CANCELED){
                    // After Cancel code.
                }

                break;
        }
    }
}

}

DialogFragment类:

public class MyDialogFragment extends DialogFragment {

public static MyDialogFragment newInstance(int num){

    MyDialogFragment dialogFragment = new MyDialogFragment();
    Bundle bundle = new Bundle();
    bundle.putInt("num", num);
    dialogFragment.setArguments(bundle);

    return dialogFragment;

}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    return new AlertDialog.Builder(getActivity())
            .setTitle(R.string.ERROR)
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setPositiveButton(R.string.ok_button,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, getActivity().getIntent());
                        }
                    }
            )
            .setNegativeButton(R.string.cancel_button, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_CANCELED, getActivity().getIntent());
                }
            })
            .create();
}
}

我希望它能得到充分的帮助!

答案 1 :(得分:1)

您应该创建一个接口:

public interface OnSubmitClickListener {
    void onSubmitClicked(WhateverInfo info);
}

之后,在对话框中添加OnSubmitClickListener并将其添加到构造函数中。

这样你的对话框类似:

private String mHeader;
private String mVehicleNumber;
private OnSubmitClickListener listener;

public static PromoCodeDialogFragment newInstance(String mHeader, String mVehicleNumber, OnSubmitClickListener listener) {
    PromoCodeDialogFragment dialog = new PromoCodeDialogFragment(listener);
    dialog.mHeader = mHeader;
    dialog.mVehicleNumber = mVehicleNumber;
    return dialog;
}

public PromoCodeDialogFragment (OnSubmitClickListener listener) {
    this.listener= listener;
}

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.submit:
            listener.onSubmitClicked(infoSomething);

            break;

        case R.id.cancel:
            dismiss();
            break;
    }

你的片段看起来像:

PromoCodeDialogFragment.newInstance(promotionDetail.promotionOffer, mBinding.vehicleId.getText().toString(), new OnSubmitClickListener(){
        @Override
        public void onSubmitClicked(WhateverInfo info) {
            //get the info from here
        }
    }).show(getActivity().getSupportFragmentManager(), "promo");