0

我有一个 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();
    }

}

4

2 回答 2

2

尝试使用getActivity();而不是getApplicationContext();

像这样

Toast.makeText(getActivity(), "Your Message", Toast.LENGTH_SHORT).show();
于 2013-10-02T09:55:29.950 回答
1

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

于 2013-10-02T09:57:15.380 回答