Nandkishor mewara
Nandkishor mewara

Reputation: 2562

data load twice... means AsyncTask onPostExecute load same data twice?

data load twice... means AsyncTask onPostExecute load same data twice? my AsyncTask onPostExecute run twice it load same data... I am running async task to load results from a server. to show the data in gridview but the data show twice time..I am confused on why the AsyncTask outputs the data twice. Please Help me Here is my code:

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

   }
   }

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

this is OnCreate where I call AsyncTask

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

Upvotes: 2

Views: 527

Answers (2)

Rakesh Yadav
Rakesh Yadav

Reputation: 1985

I was facing the same problem. I found a small problem in my code that I was not dismissing the dialog which I opened in onPostExecute() of my AsyncTask to do some extra work after AsyncTask Work.

When I called myCustomDialog.dismiss() everything worked fine.

Upvotes: 0

PRIYA PARASHAR
PRIYA PARASHAR

Reputation: 777

You should use clear() method of Arraylist before intialising the JSONObject like this:

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

Upvotes: 1

Related Questions