如何在对话框中调用setAction?

时间:2018-12-15 18:56:07

标签: android android-fragments android-dialogfragment android-dialog

我想用弹出对话框创建添加数据到SQL Lite,在对话框中只有一个textedit和buttom保存。在Snackbar中通常使用setAction()。但是在对话框中,我不知道如何调用“ AddData”。

    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View view = inflater.inflate(R.layout.list_category_expanses, container, false);

        mTextMessage = (TextView) view.findViewById(R.id.message);

        textSubcategoryExpanses = (AppCompatTextView) view.findViewById(R.id.textSubcategoryExpanses);
        recyclerViewCategoryExpanses = (RecyclerView) view.findViewById(R.id.recyclerViewCategoryExpanses);

        Button btn1 = view.findViewById(R.id.buttonListCategoryExpanses);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final Dialog fbDialogue = new Dialog(getActivity());
                fbDialogue.setContentView(R.layout.layout_add_category_expanses);
                fbDialogue.setCancelable(true);
                fbDialogue.show();

            }
        });
        initObjects();
        return view;
    }

我想将此代码AddData调用到onCreate,此代码将文本保存在sqlLite中。

public View.OnClickListener AddData = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            View customLayoutView = View.inflate(getActivity(), R.layout.layout_add_category_expanses, null);
            final EditText edNim = customLayoutView.findViewById(R.id.inputcategoryexpanses);
            final AlertDialog.Builder builder;
            builder = new AlertDialog.Builder(getActivity());
            builder.setCancelable(true);
            builder.setTitle(R.string.strTitleAlert);
            builder.setView(customLayoutView);
            builder.setPositiveButton(R.string.btnKlikstr, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    String nim = edNim.getText().toString();
                    if (databaseHelper.addCategoryExpanses(new CategoryExpanses(nim))) {
                        getDataFromSQLite();
                        Toast.makeText(getActivity(), "Data berhasil disimpan", Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(getActivity(), "Data gagal disimpan", Toast.LENGTH_LONG).show();
                    }
                    dialogInterface.dismiss();
                }
            });
            AlertDialog alertDialog = builder.create();
            alertDialog.show();
        }
    };

请帮助我解决此问题。谢谢。

1 个答案:

答案 0 :(得分:0)

您必须执行以下操作:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.list_category_expanses, container, false);

    mTextMessage = (TextView) view.findViewById(R.id.message);

    textSubcategoryExpanses = (AppCompatTextView) view.findViewById(R.id.textSubcategoryExpanses);
    recyclerViewCategoryExpanses = (RecyclerView) view.findViewById(R.id.recyclerViewCategoryExpanses);

    Button btn1 = view.findViewById(R.id.buttonListCategoryExpanses);
    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            View customLayoutView = View.inflate(getActivity(), R.layout.layout_add_category_expanses, null);
            final EditText edNim = customLayoutView.findViewById(R.id.inputcategoryexpanses);
            final AlertDialog.Builder builder;
            builder = new AlertDialog.Builder(getActivity());
            builder.setCancelable(true);
            builder.setTitle(R.string.strTitleAlert);
            builder.setView(customLayoutView);
            builder.setPositiveButton(R.string.btnKlikstr, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    String nim = edNim.getText().toString();
                    if (databaseHelper.addCategoryExpanses(new CategoryExpanses(nim))) {
                        getDataFromSQLite();
                        Toast.makeText(getActivity(), "Data berhasil disimpan", Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(getActivity(), "Data gagal disimpan", Toast.LENGTH_LONG).show();
                    }
                    dialogInterface.dismiss();
                }
            });
            AlertDialog alertDialog = builder.create();
            alertDialog.show();

        }
    });
    initObjects();
    return view;
}