Question

I want code below razrada.show(); to be executing after i press Save on my AlertDialog, but it shows and closes this is my code

AlertDialog.Builder razradaPlacanja = new AlertDialog.Builder(NoviRacun.this);
            razradaPlacanja.setTitle("Način plaćanja");
            LayoutInflater inflater = getLayoutInflater();
            View vieww = inflater.inflate(R.layout.razrada, null);
            razradaPlacanja.setView(vieww);
final EditText gotovinaEdit = (EditText) vieww.findViewById(R.id.gotovinaEdit);
final EditText karticeEdit = (EditText) vieww.findViewById(R.id.karticeEdit);
razradaPlacanja.setPositiveButton("Save",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            try {
                                json.put("TotalCash", gotovinaEdit.getText().toString());
                                json.put("TotalCreditCards", karticeEdit.getText().toString());

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    });


            AlertDialog razrada = razradaPlacanja.create();
            razrada.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
            razrada.show();
            //this i want to execute after pressing Save 
            Racuni racun = getInsertResponse(requestInsert(base64EncodedCredentials, json, httpclient1));
            try {
...

I cannot put Racuni racun = getInsertResponse(requestInsert(base64EncodedCredentials, json, httpclient1)); inside the onClick block cause then racun is unreachable (and if declared outside, needs to be final and then i cannot asign value)

Thank you for any kind of help!

Was it helpful?

Solution

You can move the code you want to execute to a separate method:

public void someAction(JsonType json);
{
    Racuni racun = getInsertResponse(requestInsert(base64EncodedCredentials, json, httpclient1));
    // the rest of the code that uses racun, etc.
}

And then call it from your dialog's onClick() method:

YourActivity.this.someAction(json);

Note that any code after show() of AlertDialog is executed immediately. show() is not blocking the flow.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top