I was having some difficulty when trying to do some logic in Java. When my map is on single tap, I will create a dialog box. If the user select okay from dialog box, I will then call another method to set value for object. Here are the codes:
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);
}
}
Then in the CreateEvent class:
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;
}
With these codes, it can perform DB insertion with no errors. But let's say I've successfully inserted a record into database, when I select another point on the map, and the dialog box pop up, and I select Cancel, the previous object record will be inserted into database again.
I wonder which part of my logic was wrong. Thanks in advance.
Edit
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) {
}
}