lundi 27 juin 2016

For loop inside the if condition, but until complete the for loop if condition else part have to stop in android

I am having a problem in for loop inside the if condition, first I want to complete the for loop first check the if condition by looping after complete the for loop else part intent have to work.

Code:

 for (int k = 0; k < cartlistadp.size(); k++) {
    // custom dialog
    if (cartlistadp.get(k).getTotalOutOfStockQuantity().equalsIgnoreCase("0")) {
        item = true;
        final Dialog dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.cartcustomdialog);

        // set the custom dialog components - text, image and button
        TextView name = (TextView) dialog.findViewById(R.id.name);
        TextView count = (TextView) dialog.findViewById(R.id.countofstock);
        ImageView img = (ImageView) dialog.findViewById(R.id.img);
        TextView cancel = (TextView) dialog.findViewById(R.id.cancel);
        TextView submit = (TextView) dialog.findViewById(R.id.submit);
        count.setText(k + " " + "Item out of Stock");
        name.setText(cartlistadp.get(k).getProductname());
        YelloPage.imageLoader.displayImage(cartlistadp.get(k).getProductimg(), img, options);
        pos = cartlistadp.get(k).getProductid();
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (!flag) {
                    //    wishlist.setBackgroundResource(R.drawable.wishnew);
                    flag = true;

                    String channel = wishshared.getString(Constants.productid, "['']");
                    JSONArray items;
                    String wishitem;
                    if (TextUtils.isEmpty(channel)) {
                        items = new JSONArray();
                        items.put(String.valueOf(pos));
                        wishitem = String.valueOf(items);
                        editors.putString(Constants.productid, wishitem);
                        editors.apply();
                        removeoutofstock();
                        Toast.makeText(context, "cartItems", Toast.LENGTH_LONG).show();
                        flag = false;

                    } else {

                        try {

                            Boolean found = false;
                            items = new JSONArray(channel);
                            for (int x = 0; x < items.length(); x++) {
                                if (pos.equalsIgnoreCase(items.getString(x))) {
                                    found = true;
                                    removeoutofstock();
                                }
                            }
                            if (!found) {
                                items.put(String.valueOf(pos));
                                wishitem = String.valueOf(items);
                                editors.putString(Constants.productid, wishitem);
                                removeoutofstock();
                                Log.i(Constants.productid, "wishitems");
                            }
                            editors.apply();
                            flag = false;

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                } else {
                    removeoutofstock();
                }

                Intent intent = ((Activity) context).getIntent();
                ((Activity) context).finish();
                context.startActivity(intent);
            }
        });
        dialog.show();
    } else {


        Intent i = new Intent(context, Delivery_Activity.class);
        context.startActivity(i);
        ((Activity) context).finish();
        // break;
    }

}

I know it's not essential question here, but little logical missing.

Anyone solve this problem.

Thanks in advance

Aucun commentaire:

Enregistrer un commentaire