从ListView的contextMenu中调用DialogFragment

时间:2014-11-20 11:15:48

标签: android listview android-listview

那里)在我的应用中,我使用单DialogFragment来存储ListView中存储的ListFragment添加编辑项。我还要提到我的对话框在某些EditText上使用语音输入

所以,问题是语音识别器活动将结果返回给它后,对话框不可见(嗯,实际上它在startActivityForResult()调用后立即变为不可见)。但对话仍然存在!我知道,因为当我旋转屏幕时,它会显示语音识别器活动返回的结果!仅当我从ListView项目的 contextMenu (到编辑项目)调用它时,它才会发生。当我从 optionsMenu (到添加项目)调用它时一切正常。

这是我的ProductDialogFragment.java

public class ProductDialogFragment extends DialogFragment
        implements
        OnClickListener,
        OnItemSelectedListener,
        OnLongClickListener {
    private CustomDialog dialog;

    private EditText mProductName;
    private EditText mProductAmount;
    private Spinner mMeasuresSpinner;
    private Button mBtnOk;
    private Button mBtnCncl;
    private String mPassedName;
    private String mPassedAmount;
    private long mPassedMeasureId;
    private static long mEditedProdId;

    private static DBController mDBController;

    public static final String ADDED_PRODUCT_NAME = "com.zulfigarov.shoppingassistant_added_product_name";
    public static final String ADDED_PRODUCT_AMOUNT = "com.zulfigarov.shoppingassistant_added_product_amount";
    public static final String ADDED_PRODUCT_MEASURE = "com.zulfigarov.shoppingassistant_added_product_measure";
    public static final String EDITED_PRODUCT_ID = "com.zulfigarov.shoppingassistant_edited_product_id";

    public static final int CREATE_NEW_PRODUCT_REQUEST = 999;
    public static final int EDIT_PRODUCT_REQUEST = 888;
    public static final int CALL_VOICE_RECOGNITION = 606;

    public static ProductDialogFragment newInstance() {
        ProductDialogFragment frag = new ProductDialogFragment();


        return frag;
    }

    public static ProductDialogFragment newInstance(long product_id, String product_name, String product_amount, long product_measure_id) {
        Bundle args = new Bundle();


        args.putString(ADDED_PRODUCT_NAME, product_name);
        args.putString(ADDED_PRODUCT_AMOUNT, product_amount);
        args.putLong(ADDED_PRODUCT_MEASURE, product_measure_id);

        ProductDialogFragment frag = new ProductDialogFragment();

        frag.setArguments(args);

        return frag;
    }

    public void setResult(int resultCode, int requestCode) {
        if ((getTargetFragment() == null)
                || (resultCode == Activity.RESULT_CANCELED))
            return;

        Intent data = new Intent();
        String prodName;
        float prodAmount;
        long measureId = mMeasuresSpinner.getSelectedItemId();

        prodName = mProductName.getText().toString();

        prodAmount = Float.valueOf((mProductAmount.getText().toString() + "f"));

        data.putExtra(ADDED_PRODUCT_NAME, prodName);
        data.putExtra(ADDED_PRODUCT_AMOUNT, prodAmount);
        data.putExtra(ADDED_PRODUCT_MEASURE, measureId);


        if (requestCode == EDIT_PRODUCT_REQUEST) {
            data.putExtra(EDITED_PRODUCT_ID, mEditedProdId);
            getTargetFragment().onActivityResult(EDIT_PRODUCT_REQUEST,
                    resultCode, data);
        } else {
            getTargetFragment().onActivityResult(CREATE_NEW_PRODUCT_REQUEST,
                    resultCode, data);
        }

    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {


        dialog = new CustomDialog(getActivity());
        dialog.setCustomView(R.layout.add_new_item, getActivity());
        dialog.setTitleColor("#FFFFFF");
        dialog.setDividerColor("#FFFFFF");
        dialog.setIcon(R.drawable.ic_action_new);

        View root = dialog.getCustomView();

        mProductName = (EditText) root.findViewById(R.id.product_name);
        mProductName.setOnLongClickListener(this);
        mProductAmount = (EditText) root.findViewById(R.id.product_amount);

        mBtnOk = (Button) root.findViewById(R.id.add_new_product_button);
        mBtnOk.setOnClickListener(this);

        mBtnCncl = (Button) root.findViewById(R.id.cancel_product_creation);
        mBtnCncl.setOnClickListener(this);

        mMeasuresSpinner = (Spinner) root.findViewById(R.id.measures_spinner);

        mDBController = DBController.getController(getActivity());

        String[] from = {ProductDBHelper.MeasuresTable.COLUMN_TITLE};
        int[] to = {android.R.id.text1};

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(),
                android.R.layout.simple_spinner_item, mDBController.getMeasuresCursor(), from, to);
        mMeasuresSpinner.setAdapter(adapter);
        mMeasuresSpinner.setOnItemSelectedListener(this);

        mMeasuresSpinner.setOnItemSelectedListener(this);

        Bundle args = getArguments();

        if (args != null) {
            dialog.setTitle("Edit Selected Product");
            mBtnOk.setText("Apply");

            mPassedName = args.getString(ADDED_PRODUCT_NAME);
            mPassedAmount = args.getString(ADDED_PRODUCT_AMOUNT);
            mPassedMeasureId = args.getLong(ADDED_PRODUCT_MEASURE);

            mProductName.setText(mPassedName);

            mMeasuresSpinner.setSelection((int) (mPassedMeasureId - 1));

        } else
            dialog.setTitle("Add New Product");

        setCancelable(false);

        return dialog;
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case (R.id.add_new_product_button): {
                if ((mProductName.getText().toString().equals(""))
                        || (mProductAmount.getText().toString().equals(""))) {
                    Toast.makeText(getActivity(),
                            "It seems you've forgot to write something!",
                            Toast.LENGTH_SHORT).show();
                } else {

                    if (getArguments() != null) {
                        setResult(Activity.RESULT_OK, EDIT_PRODUCT_REQUEST);
                    } else
                        setResult(Activity.RESULT_OK,
                                CREATE_NEW_PRODUCT_REQUEST);


                    dialog.dismiss();
                }
                break;
            }

            case (R.id.cancel_product_creation): {
                dialog.dismiss();
                break;
            }

        }

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        Log.d("shoppingAssistant", "onDismiss");
        super.onDismiss(dialog);
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position,
                               long id) {


        if (getArguments() != null) {
            mProductAmount.setText(mPassedAmount);
            mPassedAmount = "";
        } else
            mProductAmount.setText("");

        if (mDBController.checkIfMeasureDecimal(id)) {
            mProductAmount.setInputType(InputType.TYPE_CLASS_NUMBER
                    | InputType.TYPE_NUMBER_FLAG_DECIMAL);

        } else {
            mProductAmount.setInputType(InputType.TYPE_CLASS_NUMBER);
        }


    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == ProductDialogFragment.CALL_VOICE_RECOGNITION) {

            if (data != null) {
                String matches = data
                        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)
                        .get(0);
                mProductName.setText(matches);
            } else {
                Toast.makeText(getActivity(), "Failed to get your voice input",
                        Toast.LENGTH_SHORT).show();

            }

        }
    }

    @Override
    public boolean onLongClick(View v) {

        try
        {
            Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

            intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
                    "Tell me your product name:");
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                    RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
            intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);

            startActivityForResult(intent, ProductDialogFragment.CALL_VOICE_RECOGNITION);

        } catch (ActivityNotFoundException e)
        {
            Intent browserIntent = new Intent(
                    Intent.ACTION_VIEW,
                    Uri.parse("https://play.google.com/store/apps/details?id=com.google.android.voicesearch"));

            startActivity(browserIntent);
        }


        return false;
    }
}

这里的ListFragment(来自{{1>}的语音识别器来自项目上下文菜单使ProductDialogFragment不可见):< / p>

DialogFragment

我该怎么做才能解决这个问题?

0 个答案:

没有答案