Dipak
Dipak

Reputation: 1

How to get different max id from database at a time

hello i am creating an hotel app in that when i am inserting order into database at that time i am fetching max orderid from database and inserting order and orderid in database. but my problem is that when i am installing that apps in two different devices at that time when i am placing order at time it gets same orderid to different table and insert into database and order is inserted in one table so please give me any solution

    private void openAlert(View view) {
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(DisplayActivity.this);
    new AsyncLoadOrderId().execute();
    alertDialogBuilder.setTitle("Restrosoft");
    alertDialogBuilder.setMessage("Are you sure?");
    // set positive button: Yes message
    alertDialogBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            // go to a new activity of the app

            for (int i = 0; i < user_fName.size(); i++) {
                System.out.println(user_fName.get(i)); //prints element i
                menulists = user_fName.get(i);
                if (tabletypes.equals("A/C")) {

                    getAcrate(menulists);
                    getTotalamount(menulists, convertedvaluefororderid);
                    getmenuidofperticular(menulists);
                    getButtonfinalordertodb();
                    String conquantity = String.valueOf(quantity);
                    new AsyncInsertIntoTemp().execute(menulists, conquantity, OrderIdFromService, WaiterName, str1);
                } else if (tabletypes.equals("Non A/C")) {

                    getProfilesCount(menulists);
                    getTotalamount(menulists, convertedvaluefororderid);
                    getmenuidofperticular(menulists);
                    getButtonfinalordertodb();
                    String conquantity = String.valueOf(quantity);
                    new AsyncInsertIntoTemp().execute(menulists, conquantity, OrderIdFromService, WaiterName, str1);
                } else {
                    Toast.makeText(DisplayActivity.this, "Closed", Toast.LENGTH_SHORT).show();
                }
            }

            Insertingintotableorder inserttableorder = new Insertingintotableorder(OrderIdFromService, str1, currentDateTimeString, intentfromTwolistwid, ordertype, tablestatustblord, discvaluefortblord, discamount);
            new AsyncCreatetableOrder().execute(inserttableorder);
            new AsyncCreateTemporder().execute(OrderIdFromService, KOTIDFromTwolist, str1, "", "", WaiterName);
            ForupdateStatus updatestatushere = new ForupdateStatus(str1);
            new AsyncupdateStatus().execute(updatestatushere);
            Intent intents = new Intent(DisplayActivity.this, MainActivity.class);
            intents.putExtra("Uname", WaiterName);
            intents.putExtra("ForButton", cntforb);
            startActivity(intents);
            mHelper.remove();
            Toast.makeText(DisplayActivity.this, "Successfully inserted...", Toast.LENGTH_LONG).show();
        }
    });
    // set negative button: No message
    alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            // cancel the alert box and put a Toast to the user
            dialog.cancel();

        }
    });
    // set neutral button: Exit the app message
    alertDialogBuilder.setNeutralButton("Exit the app", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            // exit the app and go to the HOME
            DisplayActivity.this.finish();
        }
    });

    AlertDialog alertDialog = alertDialogBuilder.create();
    // show alert
    alertDialog.show();
}

Here is async task:

 protected class AsyncLoadOrderId extends
            AsyncTask<String, String, JSONObject> {
        JSONObject jsonObj = null;
        //ArrayList<UserOrder> subTablea = null;

        @Override
        protected JSONObject doInBackground(String... args) {
            // TODO Auto-generated method stub

            RestAPI api = new RestAPI();
            try {

                jsonObj = api.GetOrdertopid();
                JSONParser parser = new JSONParser();


                //subTablea = parser.parsOrderid(jsonObj);


            } catch (Exception e) {
                // TODO Auto-generated catch block
                Log.d("AsyncLoadDeptDetails", e.getMessage());

            }
            return jsonObj;


        }

        @Override
        protected void onPostExecute(JSONObject json) {
            // TODO Auto-generated method stub
            try {
                JSONArray jsonArray = json.getJSONArray("Value");

                for (int i = 0; i < jsonArray.length(); i++) {
                    jsonObj = jsonArray.getJSONObject(i);

                }
                OrderIdFromService = jsonObj.getString("order_id");
                convertedorderid = Integer.parseInt(OrderIdFromService);
                orderidfortable.setText(OrderIdFromService);

                if (intentresponse.equals("Yes")) {

                     serveroid = String.valueOf(orderid);
                    orderidfortable.setText(serveroid);

                }
            } catch (Exception e) {

                e.printStackTrace();


            }


        }

Upvotes: 0

Views: 57

Answers (1)

Andrey Danilov
Andrey Danilov

Reputation: 6602

Your Async work is completely wrong

For example if I call

new AsyncLoadOrderId().execute();

And then immidiatly click "Yes" button of your AlertDialog I will get wrong results because OrderIdFromService can have previous value. In other words OrderIdFromService gets value after AsyncLoadOrderId() will do its work but other actions are synchronious. With slow/bad internet connection that will not work at all.

So this line

new AsyncInsertIntoTemp().execute(menulists, conquantity, OrderIdFromService, WaiterName, str1);

Doesnt do anything because OrderIdFromService probably has wrong value.

So you should do it in chain like

  1. AsyncLoadOrderId()
  2. wait for its done
  3. get value and run new AsyncCreatetableOrder() inside AsyncLoadOrderId().onPostExecute();
  4. wait until AsyncCreatetableOrder() is done
  5. get value and run new AsyncCreateTemporder() inside AsyncCreatetableOrder().onPostExecute()
  6. etc

Its the one way to do it with your AsyncTasks.

Usually developers use something like rxJava for same functional

Upvotes: 1

Related Questions