数据加载两次...意味着AsyncTask onPostExecute加载相同的数据两次? 我的 AsyncTask onPostExecute 运行两次加载相同的数据...... 我正在运行异步任务来从服务器加载结果。在gridview中显示数据,但数据显示两次...我很困惑为什么AsyncTask输出数据两次。 请帮帮我 这是我的代码:
<form method='post' class="form-inline">
<div class="form-group">
<div id="result_table">
<!-- dynamic load -->
</div>
</div>
</form>
这是parseJsonResponse
public class InfoAccountsascyn extends AsyncTask <String, String, String>{
String result;
String s;
private Activity activity;
private String url;
private String user_id;
private ProgressDialog pDialog;
JSONObject json;
JSONParser jsonParser = new JSONParser();
private static final String TAG_SUCCESS = "Success";
int Success;
public InfoAccountsascyn(Activity activity, String url,String user_id) {
super();
this.activity = activity;
this.url = url;
this.user_id=user_id;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(activity);
pDialog.setMessage("Loading Items...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.setCanceledOnTouchOutside(false);
pDialog.show();
}
protected String doInBackground(String... args) {
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("customer_id", user_id));
//if(Last_id!=null){
// params.add(new BasicNameValuePair("last_product_id", Last_id));
// }
json = jsonParser.makeHttpRequest(url, "POST", params);
Success = json.getInt(TAG_SUCCESS);
} catch (JSONException e) {
e.printStackTrace();
}
if (Success == 1) {
result = json.toString();
}else{
try {
result = json.getString("CartData");
//s=result;
} catch (JSONException e) {
e.printStackTrace();
}
}
return result;
}
protected void onPostExecute(String result) {
((DetaildAddress) activity).parseJsonResponse(result);
pDialog.dismiss();
}
}
这是OnCreate,我称之为AsyncTask
public void parseJsonResponse(String result) {
String res = result;
if (res !=null) {
try {
JSONObject jObjects = new JSONObject(result);
JAS = jObjects.getJSONArray("Data");
for (int i = 0; i < JAS.length(); i++) {
JSONObject c = JAS.getJSONObject(i);
DetailedAddressInfo productinfo = new DetailedAddressInfo();
productinfo.setFirstname(c.optString("firstname"));
productinfo.setLastname(c.optString("lastname"));
productinfo.setStreet(c.optString("street"));
productinfo.setCity(c.optString("city"));
productinfo.setRegion(c.optString("region"));
productinfo.setPostcode(c.optString("postcode"));
productinfo.setTelephone(c.optString("telephone"));
productinfo.setFax(c.optString("fax"));
productinfo.setAddId(c.optString("customer_address_id"));
productinfo.setBilling(c.optString("is_default_billing"));
productinfo.setShipping(c.optString("is_default_shipping"));
products.add(productinfo);
if (i == JAS.length() - 1) {
Last_id = c.optString("product_id");
}
}
adapter = new DetailedAddressAdapter(DetaildAddress.this, R.layout.deshbord_address, products);
gridView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
else {
new AlertDialog.Builder(DetaildAddress.this)
.setTitle("No Address Available")
.setMessage("You Want to Add New Address")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent=new Intent(DetaildAddress.this,NewAddress.class);
String ok = "0";
intent.putExtra("ok", ok);
startActivity(intent);
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
} if (pDialog != null) {
pDialog.dismiss();
}
}
答案 0 :(得分:1)
你应该在初始化JSONObject之前使用Arraylist的clear()方法,如下所示:
if(products.size()>0)
products.clear();
答案 1 :(得分:0)
我遇到了同样的问题。我在我的代码中发现了一个小问题我没有忽略我在AsyncTask的onPostExecute()中打开的对话框,以便在AsyncTask工作之后做一些额外的工作。
当我打电话给myCustomDialog.dismiss()
时,一切正常。