gpt4 book ai didi

java - 使用不同的参数从同一个 asynctask 类获取不同的值 android

转载 作者:行者123 更新时间:2023-12-01 10:16:57 25 4
gpt4 key购买 nike

我试图从同一个 AsyncTask 类获取不同的值,但在执行第二行之后

bestsellerResult = new getProductsTask().execute("4");

两个数组将具有相同的值。

public class Splash extends Activity {

String bestsellerORoccassions;
ArrayList<productDetails> categoryProductsData;
ArrayList<productDetails> bestsellereData;
ArrayList<productDetails> occassionsData;

ArrayList<productDetails> bestsellerResult;
ArrayList<productDetails> occassionResult;


boolean internetConnection;
private productAdapter productAdapter;
GridView gridView;
ArrayList<productDetails> productDetailsArr = new ArrayList<productDetails>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_splash);

bestsellerORoccassions = "";
categoryProductsData = new ArrayList<>();
bestsellereData = new ArrayList<>();
occassionsData = new ArrayList<>();

occassionResult = new ArrayList<>();
bestsellerResult = new ArrayList<>();

internetConnection = UtilityClass.isInternetAvailable(getApplicationContext());


bestsellerORoccassions = "bestsellers";

if (internetConnection) {

occassionResult = new getProductsTask().execute("41");
bestsellerResult = new getProductsTask().execute("4");
} else {
Intent internet = new Intent(com.ecommerce.dell.ecommerce.Splash.this, NointernetConnection.class);
startActivity(internet);
finish();
}
}


public class getProductsTask extends AsyncTask<String, Void, ArrayList<productDetails>> {


JSONObject result = new JSONObject();
JSONObject result1 = new JSONObject();
String error = "";
JsonFunctions jsonFunctions = new JsonFunctions();
ArrayList<ArrayList<String>> return_products = new ArrayList<ArrayList<String>>();

private ArrayList<productDetails> getProductsDataFromJSON(JSONObject productsJSONObj) throws JSONException {

final String product_image = "image";
final String product_name = "name";
final String product_id = "id";
final String productRealPrice = "price";
final String getProductDiscount = "discount";
final String getProductDescreiption = "description";
final String getProductPercentage = "percentage";

JSONArray productsArray = productsJSONObj.getJSONArray("products");

for (int i = 0; i < productsArray.length(); i++) {
/*get JSON Object represent product info(Image-Name-Real Price - Discount)*/
JSONObject getProductInfo = productsArray.getJSONObject(i);
String pImage = getProductInfo.getString(product_image);
String pName = getProductInfo.getString(product_name);
String pID = getProductInfo.getString(product_id);
String pRealPrice = getProductInfo.getString(productRealPrice);
String pDiscount = getProductInfo.getString(getProductDiscount);
String pDescreiption = getProductInfo.getString(getProductDescreiption);
String pPercentage = getProductInfo.getString(getProductPercentage);
productDetailsArr.add(i, new productDetails(pName, pID, pRealPrice, pDiscount, pDescreiption, pImage, pPercentage));
}
Log.d("ProductDetails", "Product Details: ");
for (int i = 0; i < productDetailsArr.size(); i++) {
Log.d("ProductDetails", "Product Details: " + productDetailsArr.get(i).getProductID());
}

return productDetailsArr;
}

@Override
protected void onPreExecute() {
super.onPreExecute();
// progressDialog.show();
Log.d("getProductsTask", "getProductsTask");
}

@Override
protected ArrayList<productDetails> doInBackground(String... params) {
result = jsonFunctions.getCategoryProducts(params[0], String.valueOf(0));


if (result == null) {
error = "null json object";
return null;
}
try {
if (result.getInt("success") == 1) {
return getProductsDataFromJSON(result);
}
} catch (Exception ex) {
error = ex.toString();
Log.d("getProductsTask", error);

}
return null;

}

@Override
protected void onPostExecute(ArrayList<productDetails> products) {
super.onPostExecute(products);




}


}


@Override
protected void onStop() {
super.onStop();


}
}

最佳答案

您的AsyncTask更改并返回 productDetailsArr ,它始终是同一个实例,因为您将其声明为 Splash 的成员类并在那里实例化一次。所以occassionResultbestsellerResultproductDetailsArr 是相同的实例。

要解决此问题,只需删除 productDetailsArr来自Splash类并添加ArrayList<productDetails> productDetailsArr = new ArrayList<productDetails>();getProductsDataFromJSON()方法。

这应该有效:

public class Splash extends Activity {

String bestsellerORoccassions;
ArrayList<productDetails> categoryProductsData;
ArrayList<productDetails> bestsellereData;
ArrayList<productDetails> occassionsData;

ArrayList<productDetails> bestsellerResult;
ArrayList<productDetails> occassionResult;


boolean internetConnection;
private productAdapter productAdapter;
GridView gridView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_splash);

bestsellerORoccassions = "";
categoryProductsData = new ArrayList<>();
bestsellereData = new ArrayList<>();
occassionsData = new ArrayList<>();

occassionResult = new ArrayList<>();
bestsellerResult = new ArrayList<>();

internetConnection = UtilityClass.isInternetAvailable(getApplicationContext());


bestsellerORoccassions = "bestsellers";

if (internetConnection) {

occassionResult = new getProductsTask().execute("41");
bestsellerResult = new getProductsTask().execute("4");
} else {
Intent internet = new Intent(com.ecommerce.dell.ecommerce.Splash.this, NointernetConnection.class);
startActivity(internet);
finish();
}
}


public class getProductsTask extends AsyncTask<String, Void, ArrayList<productDetails>> {


JSONObject result = new JSONObject();
JSONObject result1 = new JSONObject();
String error = "";
JsonFunctions jsonFunctions = new JsonFunctions();
ArrayList<ArrayList<String>> return_products = new ArrayList<ArrayList<String>>();

private ArrayList<productDetails> getProductsDataFromJSON(JSONObject productsJSONObj) throws JSONException {

final String product_image = "image";
final String product_name = "name";
final String product_id = "id";
final String productRealPrice = "price";
final String getProductDiscount = "discount";
final String getProductDescreiption = "description";
final String getProductPercentage = "percentage";

// ********* instantiate the list here ***********
ArrayList<productDetails> productDetailsArr = new ArrayList<productDetails>();
JSONArray productsArray = productsJSONObj.getJSONArray("products");

for (int i = 0; i < productsArray.length(); i++) {
/*get JSON Object represent product info(Image-Name-Real Price - Discount)*/
JSONObject getProductInfo = productsArray.getJSONObject(i);
String pImage = getProductInfo.getString(product_image);
String pName = getProductInfo.getString(product_name);
String pID = getProductInfo.getString(product_id);
String pRealPrice = getProductInfo.getString(productRealPrice);
String pDiscount = getProductInfo.getString(getProductDiscount);
String pDescreiption = getProductInfo.getString(getProductDescreiption);
String pPercentage = getProductInfo.getString(getProductPercentage);
productDetailsArr.add(i, new productDetails(pName, pID, pRealPrice, pDiscount, pDescreiption, pImage, pPercentage));
}
Log.d("ProductDetails", "Product Details: ");
for (int i = 0; i < productDetailsArr.size(); i++) {
Log.d("ProductDetails", "Product Details: " + productDetailsArr.get(i).getProductID());
}

return productDetailsArr;
}

@Override
protected void onPreExecute() {
super.onPreExecute();
// progressDialog.show();
Log.d("getProductsTask", "getProductsTask");
}

@Override
protected ArrayList<productDetails> doInBackground(String... params) {
result = jsonFunctions.getCategoryProducts(params[0], String.valueOf(0));


if (result == null) {
error = "null json object";
return null;
}
try {
if (result.getInt("success") == 1) {
return getProductsDataFromJSON(result);
}
} catch (Exception ex) {
error = ex.toString();
Log.d("getProductsTask", error);

}
return null;

}

@Override
protected void onPostExecute(ArrayList<productDetails> products) {
super.onPostExecute(products);




}


}


@Override
protected void onStop() {
super.onStop();


}
}

关于java - 使用不同的参数从同一个 asynctask 类获取不同的值 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35839211/

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