从DialogFragment访问数据库并烘烤消息

时间:2013-10-02 09:52:38

标签: android multithreading android-dialogfragment

我有一个DialogFragment,显示一个简单的是/否问题。 当用户按下“是”时,我执行数据库请求(基本删除条目)。 然后,我向用户报告消息以报告成功或失败。

我尽量避免从UI线程调用数据库,所以我创建了一个删除该条目的线程,并从该线程调用DialogFragment中的处理程序来显示toast消息。

我的问题是当用户按下按钮时,线程启动并关闭对话框。线程启动后,数据将从数据库中删除。但是当我从处理程序向我的消息干杯时,DialogFragment已经与父Activity分离,所以我没有上下文来调用Toast.makeText()。

我的问题是我该如何干杯?

我知道我可以创建一个服务来处理数据库操作,但这不是太麻烦吗? 有更简单的方法吗?

谢谢!

编辑:这是我的代码,以帮助您理解我的问题:

public class EraseHistoryDialogFragment extends DialogFragment {
    private HistoryDatabaseHandler mHistoryDbHandler;

    private final static int MSG_NOTIFY_EMPTYDB = 1;
    private final static int MSG_NOTIFY_DELENTRY = 2;
    private final static int MSG_NOTIFY_NODELETION = 3;
    private Context mContext;


    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case MSG_NOTIFY_DELENTRY:
                Toast.makeText(mContext,
                        getS    tring(R.string.historytab_erased_entry),
                        Toast.LENGTH_SHORT).show();
                break;
            case MSG_NOTIFY_EMPTYDB:
                Toast.makeText(mContext,
                        getS    tring(R.string.historytab_history_cleared),
                        Toast.LENGTH_SHORT).show();
                break;
            case MSG_NOTIFY_NODELETION:
                Toast.makeText(mContext,
                        getS    tring(R.string.historytab_erase_failed),
                        Toast.LENGTH_SHORT).show();
                break;
            }
        };
    };
    private Runnable mEraseHistoryRunnable = new Runnable() {

        @Override
        public void run() {
            if (mHistoryDbHandler.clearAllTables()) {
                mHandler.sendEmptyMessage(MSG_NOTIFY_EMPTYDB);
            } else {
                mHandler.sendEmptyMessage(MSG_NOTIFY_NODELETION);
            }
        }
    };

    private class EraseEntryRunnable implements Runnable {
        private String mEntryId;

        public EraseEntryRunnable(String entryID) {
            mEntryId = entryID;
        }

        @Override
        public void run() {
            if (mHistoryDbHandler.deleteEntry(mEntryId)) {
                mHandler.sendEmptyMessage(MSG_NOTIFY_DELENTRY);
            } else {
                mHandler.sendEmptyMessage(MSG_NOTIFY_NODELETION);
            }
        }
    };

    public static EraseHistoryDialogFragment newInstance(String message,
            String entryID, boolean eraseAll) {
        EraseHistoryDialogFragment frag = new EraseHistoryDialogFragment();
        Bundle args = new Bundle();
        args.putString("message", message);
        args.putString("entryid", entryID);
        args.putBoolean("eraseall", eraseAll);
        frag.setArguments(args);
        return frag;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mHistoryDbHandler = HistoryDatabaseHandler.getInstance(getActivity());
        mContext = getActivity().getApplicationContext();
        String message = getArguments().getString("message");
        final String entryID = getArguments().getString("entryid");
        final boolean eraseAll = getArguments().getBoolean("eraseall");
        return new AlertDialog.Builder(getActivity())
                .setMessage(message)
                .setPositiveButton(R.string.groupstab_yes,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int whichButton) {
                                if (eraseAll) {    
                                    Thread emptyDbT = new Thread(
                                            mEraseHistoryRunnable);
                                    emptyDbT.start();
                                } else {
                                    Thread deleteEntryT = new Thread(
                                            new EraseEntryRunnable(entryID));
                                    deleteEntryT.start();
                                }
                            }
                        })
                .setNegativeButton(R.string.groupstab_no,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int whichButton) {
                                getDialog().dismiss();
                            }
                        }).create();
    }

}

2 个答案:

答案 0 :(得分:2)

尝试使用getActivity();代替getApplicationContext();

像这样

Toast.makeText(getActivity(), "Your Message", Toast.LENGTH_SHORT).show();

答案 1 :(得分:1)

尝试使用getActivity().getApplicationContext()获取ApplicationContext