gpt4 book ai didi

android - 数据加载两次...意味着 AsyncTask onPostExecute 加载相同的数据两次?

转载 作者:行者123 更新时间:2023-11-29 01:22:37 28 4
gpt4 key购买 nike

数据加载两次...意味着 AsyncTask onPostExecute 加载相同的数据两次?我的 AsyncTask onPostExecute 运行两次它加载相同的数据...我正在运行异步任务以从服务器加载结果。在 gridview 中显示数据,但数据显示两次。我对为什么 AsyncTask 输出数据两次感到困惑。请帮助我这是我的代码:

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();

}
}

这是 parseJsonResponse

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();
}
}

这是我调用 AsyncTask 的 OnCreate

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detaild_address);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
stub = (ViewStub) findViewById(R.id.layout_stub);
stub.setLayoutResource(R.layout.content_detaild_address);
View inflated = stub.inflate();

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
String check = "0";
String chEck;
DBHandler db = new DBHandler(DetaildAddress.this);
List<DBconnector> Contacts = db.getAllContacts();
for (DBconnector cn : Contacts) {
chEck = cn.getActive();
if(chEck != null){
if (chEck.equals("1")){
check = chEck;
}
}
}
if (check.equals("1")) {
List<DBconnector> contacts = db.getActiveData();

for (DBconnector c : contacts) {
Customer_id = c.getCustomer_id();
//new CheckAddress().execute();
boolean connected = false;
ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
if(connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED ||
connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) {
//we are connected to a network
new InfoAccountsascyn(DetaildAddress.this,url_address,Customer_id).execute();
}
else
{
Toast.makeText(DetaildAddress.this,"Internet Connection is not Available",Toast.LENGTH_LONG).show();
Intent intent=new Intent(DetaildAddress.this,HomeActivity.class);
startActivity(intent);
connected = false;
}

}

}
else{
Toast.makeText(DetaildAddress.this, "Please login First", Toast.LENGTH_LONG).show();
Intent intent = new Intent(DetaildAddress.this, UserLogin.class);
startActivity(intent);
}
gridView = (GridView) findViewById(R.id.gridView1);
myLastVisiblePos = gridView.getFirstVisiblePosition();
products = new ArrayList<DetailedAddressInfo>();
//gridView.setAdapter(new DetailedAddressAdapter(DetaildAddress.this, R.layout.deshbord_address, products));

TextView newaddress=(TextView)findViewById(R.id.newaddress);
newaddress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(DetaildAddress.this,NewAddress.class);
String ok = "0";
intent.putExtra("ok", ok);
startActivity(intent);
}
});
}

最佳答案

在像这样初始化 JSONObject 之前,您应该使用 Arraylist 的 clear() 方法:

if(products.size()>0)
products.clear();

关于android - 数据加载两次...意味着 AsyncTask onPostExecute 加载相同的数据两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35909050/

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