你好我正在创建一个酒店应用程序,因为当我在数据库中插入订单时,我从数据库中获取max orderid并在数据库中插入order和orderid。但我的问题是,当我在两个不同的设备上安装那些应用程序的时候,当我下订单时它将相同的orderid到不同的表并插入数据库并且订单被插入一个表中所以请给我任何解决方案< / p>
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();
}
这是异步任务:
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();
}
}
答案 0 :(得分:1)
您的异步工作完全错误
例如,如果我打电话
new AsyncLoadOrderId().execute();
然后通过点击AlertDialog
的“是”按钮,我会得到错误的结果,因为OrderIdFromService
可以有之前的值。换句话说,OrderIdFromService
在 AsyncLoadOrderId()
完成其工作后获得值,但其他操作是同步的。使用慢/坏的互联网连接根本不起作用。
所以这一行
new AsyncInsertIntoTemp().execute(menulists, conquantity, OrderIdFromService, WaiterName, str1);
没有做任何事情,因为OrderIdFromService
可能有错误的值。
所以你应该在像
这样的链中做到这一点AsyncLoadOrderId()
new AsyncCreatetableOrder()
AsyncLoadOrderId().onPostExecute()
; AsyncCreatetableOrder()
完成new AsyncCreateTemporder()
AsyncCreatetableOrder().onPostExecute()
这是使用AsyncTasks
进行此操作的唯一方法。
通常开发人员使用类似rxJava
的内容来实现相同的功能