gpt4 book ai didi

java - 如何保持第二个进程直到完成 Android 中的第一个进程?

转载 作者:行者123 更新时间:2023-11-29 23:05:40 25 4
gpt4 key购买 nike

我在保持第二个过程直到完成第一个过程时遇到问题。我正在使用 AsyncTask

我从 MySql 服务器获取数据。有时获取数据需要更多时间,因为我使用了共享服务器。

因此,有时第二个进程在第一个进程之前运行。

这是代码。

这是我的 ListView 代码

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
MainItemListNotes invoiceNotes = (MainItemListNotes) parent.getItemAtPosition(position);if (shareactivitycode.equals("Quote"))
{
if (invoiceNotes.getItemType().equals("Labels"))
{
getLabelQualityID(invoiceNotes.getItemID());

getLabelPrice(labelQualityID);

if (labelNoOfColour.equals("0"))
{
double a = Double.parseDouble(lPurchasePrice);
double b = Double.parseDouble(lLowRate);
double c = Double.parseDouble(lSellRate);

double d = Double.parseDouble(labelWidth);
double e = Double.parseDouble(labelHeight);

Double f = ((d * e) / 625.0000) * a;
Double g = Double.parseDouble(String.format("%.4f", f));
Log.e("0purchaseP", String.valueOf(g));
Log.e("0purchaseP", labelWidth + " * " + labelHeight + " / " + "625" + " * " + lPurchasePrice);

Double h = Double.parseDouble(String.format("%.4f", ((d * e) / 625.0000) * b));
Log.e("0LowP", String.valueOf(h));
Log.e("0LowP", labelWidth + " * " + labelHeight + " / " + "625" + " * " + lLowRate);

Double i = Double.parseDouble(String.format("%.4f", ((d * e) / 625.0000) * c));
Log.e("0SellP", String.valueOf(i));
Log.e("0SellP", labelWidth + " * " + labelHeight + " / " + "625" + " * " + lSellRate);

Intent intent = new Intent(ItemList.this, NewQuotation.class);
intent.putExtra("itemid", invoiceNotes.getItemID());
intent.putExtra("itemname", invoiceNotes.getItemName());
intent.putExtra("hsncode", invoiceNotes.getHSNCode());
intent.putExtra("itemtype", invoiceNotes.getItemType());
intent.putExtra("labelwidth", labelWidth);
intent.putExtra("labelheight", labelHeight);
intent.putExtra("packing", labelPacking);
intent.putExtra("purchaserate", String.valueOf(g));
intent.putExtra("lowrate", String.valueOf(h));
intent.putExtra("sellrate", String.valueOf(i));
setResult(3, intent);
finish();
}
}
}
}
});

这是getLabelQualityID(invoiceNotes.getItemID());代码

private void getLabelQualityID(String itemid) {
String URL = "myserverurl.com";
class VoucherDetails extends AsyncTask<Void, Void, String> {

@Override
protected void onPreExecute() {
super.onPreExecute();
}

@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);

try {
JSONObject obj = new JSONObject(s);

if (!obj.getBoolean("error")) {

JSONObject product = obj.getJSONObject("voucher");

labelQualityID = product.getString("id");
labelWidth = product.getString("width");
labelHeight = product.getString("height");
labelNoOfColour = product.getString("noofcolor");
labelPacking = product.getString("labelpacking");

} else {
Toast.makeText(getApplicationContext(), "Invalid Voucher ID", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e)
{
e.printStackTrace();
Log.e("Sum Error ", e.toString());
Toast.makeText(getApplicationContext(), "Error : "+e.toString(), Toast.LENGTH_SHORT).show();
}
}

@Override
protected String doInBackground(Void... voids) {
RequestHandler requestHandler = new RequestHandler();

HashMap<String, String> params = new HashMap<>();
params.put("itemid", itemid);

return requestHandler.sendPostRequest(URL, params);
}
}

VoucherDetails ul = new VoucherDetails();
ul.execute();
}

这是getLabelPrice(labelQualityID);代码

private void getLabelPrice(String qualityid) {
String URL = "myserverurl.com";
class VoucherDetails extends AsyncTask<Void, Void, String> {

@Override
protected void onPreExecute() {
super.onPreExecute();
}

@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);

try {
JSONObject obj = new JSONObject(s);

if (!obj.getBoolean("error")) {

JSONObject product = obj.getJSONObject("voucher");

lPurchasePrice = product.getString("purchaserate");
lLowRate = product.getString("lowrate");
lSellRate = product.getString("sellrate");

} else {
Toast.makeText(getApplicationContext(), "Invalid Voucher ID", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e)
{
e.printStackTrace();
Log.e("Sum Error ", e.toString());
Toast.makeText(getApplicationContext(), "Error : "+e.toString(), Toast.LENGTH_SHORT).show();
}
}

@Override
protected String doInBackground(Void... voids) {
RequestHandler requestHandler = new RequestHandler();

HashMap<String, String> params = new HashMap<>();
params.put("id", qualityid);

return requestHandler.sendPostRequest(URL, params);
}
}

VoucherDetails ul = new VoucherDetails();
ul.execute();
}

labelNoOfColour 来自 getLabelQualityID()& lPurchasePrice, lLowRate, lSellRate, labelWidth, labelHeight 来自getLabelPrice() 函数。

有时 getLabelQualityID()getLabelPrice() 需要时间来获取数据。 & if (labelNoOfColour.equals("0")),进程在获取数据之前执行。

有时 getLabelPrice() 过程在 getLabelQualityID() 完成之前运行。

请指导我如何在第一个过程完成之前保持第二个过程。

最佳答案

当您运行多个 AsycTask 时,一个线程可能先于另一个线程执行,这很常见。因此,为确保顺序处理,您需要为您的 AsyncTask 设置一个监听器,以跟踪线程是否已完成。我想建议以下实现。

创建一个 interface 类,如下所示。

public interface ThreadFinishListener {
void onThreadFinished();
}

现在为您的第一个线程创建一个监听器变量和一个构造函数,我想这是 VoucherDetails

private void getLabelQualityID(String itemid) {

String URL = "myserverurl.com";

class VoucherDetails extends AsyncTask<Void, Void, String> {

// Create a listener
ThreadFinishListener listener;

// Create a constructor
public VoucherDetails(ThreadFinishListener listener) {
this.listener = listener;
}

@Override
protected void onPreExecute() {
super.onPreExecute();
}

@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);

try {
JSONObject obj = new JSONObject(s);

if (!obj.getBoolean("error")) {

JSONObject product = obj.getJSONObject("voucher");

labelQualityID = product.getString("id");
labelWidth = product.getString("width");
labelHeight = product.getString("height");
labelNoOfColour = product.getString("noofcolor");
labelPacking = product.getString("labelpacking");

// invoke the listener method here when you are done with your first thread
listener.onThreadFinished();

} else {
Toast.makeText(getApplicationContext(), "Invalid Voucher ID", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
Log.e("Sum Error ", e.toString());
Toast.makeText(getApplicationContext(), "Error : "+e.toString(), Toast.LENGTH_SHORT).show();
}
}

@Override
protected String doInBackground(Void... voids) {
RequestHandler requestHandler = new RequestHandler();

HashMap<String, String> params = new HashMap<>();
params.put("itemid", itemid);

return requestHandler.sendPostRequest(URL, params);
}
}

VoucherDetails ul = new VoucherDetails(new ThreadFinishListener() {
@Override
public void onThreadFinished() {
// Call the second thread here
}
});

ul.execute();
}

希望对您有所帮助!

关于java - 如何保持第二个进程直到完成 Android 中的第一个进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56588803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com