成功插入数据库后的Java清除对象

时间:2014-11-10 11:11:48

标签: java android mysql dialog onclicklistener

我在尝试用Java做一些逻辑时遇到了一些困难。当我的地图单击时,我将创建一个对话框。如果用户从对话框中选择okay,我将调用另一个方法来设置object的值。以下是代码:

public void onSingleTap(float x, float y) {
                Event eventModelAdd = null;
                eventModelAdd = CreateEvent
                        .createEventDialog(context, point.getX(), point.getY());
                if (eventModelAdd != null) {
                    new MyAsyncTask().execute(eventModelAdd);
                }
        }

然后在CreateEvent类中:

static Event addEventModel;

public static Event createEventDialog(final Context context,final double x, final double y) {
    AlertDialog.Builder AddDialog = new AlertDialog.Builder(context);
    AddDialog.setTitle("Add Event");

    LayoutInflater li = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View dialogView = li.inflate(R.layout.create_event, null);
    txtEventName = (EditText) dialogView.findViewById(R.id.txtEventName);
    txtEventDesc = (EditText) dialogView.findViewById(R.id.txtEventDesc);
    radioDiscussion = (RadioButton) dialogView
            .findViewById(R.id.radioDiscussion);

    AddDialog.setView(dialogView);
    AddDialog.setPositiveButton("Ok",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    addEventModel =  new Event();
                    addEventModel = onConfirmAddEventClicked(context, x, y);
                    dialog.dismiss();
                }
            });

    AddDialog.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    dialog.dismiss();
                }
            });
    AddDialog.show();

    return addEventModel;
}

public static Event onConfirmAddEventClicked(Context context, double x , double y) {
    Event eventModel = new Event();
    // Set the value to Event object
    }
    return eventModel;
}

使用这些代码,它可以执行数据库插入而没有错误。但是,假设我已成功将记录插入数据库,当我在地图上选择另一个点时,弹出对话框,并选择取消,之前的对象记录将再次插入数据库。

我想知道我逻辑的哪一部分是错的。提前谢谢。

修改

public class MyAsyncTask extends AsyncTask<Event, Integer, Double> {
    @Override
    protected Double doInBackground(Event... params) {

        try {
            eventCtrl.retrieveEventJSON();
            if (params.length == 1) {
                eventCtrl.createEvent(params[0]);
                //Refresh map after successfully added event
                eventCtrl.retrieveEventJSON();
                eventCtrl.plotEventOnMap(context);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    protected void onPostExecute(Double result) {
    }

    protected void onProgressUpdate(Integer... progress) {
    }
}

1 个答案:

答案 0 :(得分:1)

如果可能,请移动此代码:

if (eventModelAdd != null) {
                    new MyAsyncTask().execute(eventModelAdd);
                }

dialog's正面按钮onClick,确保只有在用户点击肯定按钮时才会执行AsyncTask