1

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) {
    }
}

1 Answers1

1

If possible, move this code :

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

To your dialog's positive button's onClick to make sure the AsyncTask will only be executed when the user click the positive button.

Blaze Tama
  • 10,828
  • 13
  • 69
  • 129
  • MyAsyncTask class was located inside another class. SO when I moved it into CreateEvent, it threw me this error message: No enclosing instance of type ENeighbourhoodActivity is accessible. Must qualify the allocation with an enclosing instance of type ENeighbourhoodActivity (e.g. x.new A() where x is an instance of ENeighbourhoodActivity) –  Nov 10 '14 at 11:28
  • @IWasSoLost I think the main problem is the AsyncTask keeps getting called on onSingleTap, so no matter what user click (negative or positive) button, the AsyncTask will always be called – Blaze Tama Nov 10 '14 at 11:31
  • But do you have any ideas how to access MyAsyncTask from another class? –  Nov 10 '14 at 11:32
  • @IWasSoLost Maybe this will help you : http://stackoverflow.com/questions/19662851/how-to-call-method-in-activity-form-non-activity-class – Blaze Tama Nov 10 '14 at 11:35
  • @IWasSoLost or this : http://stackoverflow.com/questions/6931168/android-access-method-in-an-activity-from-another-activity – Blaze Tama Nov 10 '14 at 11:36
  • Sure thanks a lot! But then again, when I finish typing the information on dialog box, then I selected Okay. But the record will not be inserted straight away, instead, I have to single tap on map again to prompt the dialogue box, then it will be inserted. Do you have any ideas? –  Nov 10 '14 at 11:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/64625/discussion-between-blaze-tama-and-i-was-so-lost). – Blaze Tama Nov 10 '14 at 11:48
  • Yeah it's solved for now. But it has to be single tap once, fill in information, click okay. then single tap again to insert into database –  Nov 10 '14 at 11:49
  • Can you help me take a look at this: http://stackoverflow.com/questions/26865092/multiple-request-in-asynctask-android –  Nov 11 '14 at 12:39