我想在Recycler View适配器中检查“数量小于或等于零”,但无法在Android中使用

在Recycler View适配器中,必须检查``数量''是否大于或为零,是否从我发送给setter模型的Json URL中获取了数量字符串。物品。

适配器:

public class CartlistAdapter extends RecyclerView.Adapter < CartlistAdapter.ViewHolder > {

        private ArrayList < CartItemoriginal > cartlistadp;
        private ArrayList < Cartitemoringinaltwo > cartlistadp2;
        DisplayImageOptions options;
        private Context context;
        public static final String MyPREFERENCES = "MyPrefs";
        public static final String MYCARTPREFERENCE = "CartPrefs";
        public static final String MyCartQtyPreference = "Cartatyid";
        SharedPreferences.Editor editor;
        SharedPreferences shared,
        wishshared;
        SharedPreferences.Editor editors;
        String pos,
        qtyDelete;
        String date;
        String currentDateandTime;
        private static final int VIEW_TYPE_ONE = 1;
        private static final int VIEW_TYPE_TWO = 2;
        private static final int TYPE_HEADER = 0;
        private Double orderTotal = 0.00;
        DecimalFormat df = new DecimalFormat("0");
        Double extPrice;
        View layout,
        layouts;
        SharedPreferences sharedPreferences;
        SharedPreferences.Editor QutId;

        boolean flag = false;


        public CartlistAdapter() {

        }
        public CartlistAdapter(ArrayList < CartItemoriginal > cartlistadp, Context context) {

            this.cartlistadp = cartlistadp;
            this.cartlistadp2 = cartlistadp2;
            this.context = context;
            options = new DisplayImageOptions.Builder().cacheOnDisk(true).cacheInMemory(true).showImageOnLoading(R.drawable.b2)
                .showImageForEmptyUri(R.drawable.b2).build();
            if (YelloPage.imageLoader.isInited()) {
                YelloPage.imageLoader.destroy();
            }
            YelloPage.imageLoader.init(ImageLoaderConfiguration.createDefault(context));
        }


        public int getItemViewType(int position) {

            if (cartlistadp.size() == 0) {
                Toast.makeText(context, String.valueOf(cartlistadp), Toast.LENGTH_LONG).show();
                return VIEW_TYPE_TWO;
            }
            return VIEW_TYPE_ONE;


        }


        @Override
        public CartlistAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {

            ViewHolder viewHolder = null;
            switch (position) {
                case VIEW_TYPE_TWO:
                    View view2 = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.activity_cart, viewGroup, false);
                    viewHolder = new ViewHolder(view2, new MyTextWatcher(viewGroup, position));
                    // return view holder for your placeholder
                    break;
                case VIEW_TYPE_ONE:
                    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cartitemrow, viewGroup, false);
                    viewHolder = new ViewHolder(view, new MyTextWatcher(view, position));
                    // return view holder for your normal list item
                    break;
            }
            return viewHolder;

        }

        @Override
        public void onBindViewHolder(CartlistAdapter.ViewHolder viewHolder, int position) {
            viewHolder.productnames.setText(cartlistadp.get(position).getProductname());
            int totalqty = Integer.parseInt(cartlistadp.get(position).getTotalOutOfStockQuantity());
            Log.i("totaqty", String.valueOf(totalqty));
            String zero = "0";

            if (!cartlistadp.get(position).getTotalOutOfStockQuantity().equalsIgnoreCase(zero)) {
                viewHolder.cartalisname.setText(cartlistadp.get(position).getAliasname());
                viewHolder.cartprice.setText("Rs" + " " + (Integer.parseInt(cartlistadp.get(position).getPrice()) * cartlistadp.get(position).getQty()));
                viewHolder.cartdelivery.setText(cartlistadp.get(position).getDelivery());
                viewHolder.cartshippin.setText(cartlistadp.get(position).getShippincharge());
                viewHolder.cartsellername.setText(cartlistadp.get(position).getSellername());
                viewHolder.Error.setText(cartlistadp.get(position).getError());
                viewHolder.qty.setTag(cartlistadp.get(position));
                viewHolder.myTextWatcher.updatePosition(position);
                if (cartlistadp.get(position).getQty() != 0) {
                    viewHolder.qty.setText(String.valueOf(cartlistadp.get(position).getQty()));

                } else {
                    viewHolder.qty.setText("0");
                }
                // viewHolder.cartoutofstock.setVisibility(View.GONE);

            } else {
                viewHolder.cartoutofstock.setText("Out Of Stock");
                viewHolder.deliverylinear.setVisibility(View.GONE);
                viewHolder.qtylinear.setVisibility(View.GONE);
                viewHolder.sellerlinear.setVisibility(View.GONE);
                viewHolder.cartprice.setVisibility(View.GONE);
                viewHolder.cartalisname.setVisibility(View.GONE);
                viewHolder.cartoutofstock.setText("Out Of Stock");
                viewHolder.cartoutofstock.setVisibility(View.VISIBLE);
                viewHolder.bottomview.setVisibility(View.GONE);
                viewHolder.Error.setVisibility(View.GONE);
            }




            YelloPage.imageLoader.displayImage(cartlistadp.get(position).getProductimg(), viewHolder.cartitemimg, options);
        }

        @Override
        public int getItemCount() {
            return cartlistadp.size();
        }

        public long getItemId(int position) {
            return position;
        }
        public Object getItem(int position) {
            return cartlistadp.get(position);
        }


        public class ViewHolder extends RecyclerView.ViewHolder {
            private TextView productnames, cartalisname, cartprice, cartdelivery, cartshippin, cartsellername, Error, total, cartoutofstock;
            private ImageView cartitemimg;
            private ImageButton wishbtn, removebtn;
            private LinearLayout removecart, movewishlist;
            private CardView cd;
            private EditText qty;
            private ImageView WishImg;
            public MyTextWatcher myTextWatcher;
            public LinearLayout qtylinear, deliverylinear, sellerlinear;
            public View bottomview;

            public ViewHolder(final View view, MyTextWatcher myTextWatcher) {
                super(view);
                productnames = (TextView) view.findViewById(R.id.cartitemname);
                cartalisname = (TextView) view.findViewById(R.id.cartalias);
                cartprice = (TextView) view.findViewById(R.id.CartAmt);
                cartdelivery = (TextView) view.findViewById(R.id.cartdel);
                cartshippin = (TextView) view.findViewById(R.id.shippingcrg);
                cartsellername = (TextView) view.findViewById(R.id.cartSellerName);
                cartitemimg = (ImageView) view.findViewById(R.id.cartimg);
                Error = (TextView) view.findViewById(R.id.error);
                qtylinear = (LinearLayout) view.findViewById(R.id.qtylinear);
                deliverylinear = (LinearLayout) view.findViewById(R.id.deliverlinear);
                sellerlinear = (LinearLayout) view.findViewById(R.id.sellerlinear);
                cartoutofstock = (TextView) view.findViewById(R.id.CartOutofstock);
                bottomview = (View) view.findViewById(R.id.bottomview);
                this.myTextWatcher = myTextWatcher;

                removecart = (LinearLayout) view.findViewById(R.id.removecart);
                movewishlist = (LinearLayout) view.findViewById(R.id.movewishlist);

                WishImg = (ImageView) view.findViewById(R.id.wishimg);



                qty = (EditText) view.findViewById(R.id.quantity);
                qty.addTextChangedListener(myTextWatcher);

                String pid, qid;

                sharedPreferences = view.getContext().getSharedPreferences(MYCARTPREFERENCE, Context.MODE_PRIVATE);
                QutId = sharedPreferences.edit();
                Log.d("Position checking1 ---", String.valueOf(getAdapterPosition()));


                qty.setOnKeyListener(new View.OnKeyListener() {
                    @Override
                    public boolean onKey(View v, int keyCode, KeyEvent event) {
                        qty.setSelection(qty.getText().length());
                        return false;
                    }
                });

                wishshared = view.getContext().getSharedPreferences(MyPREFERENCES, context.MODE_PRIVATE);
                editors = view.getContext().getSharedPreferences(MyPREFERENCES, context.MODE_PRIVATE).edit();


                shared = view.getContext().getSharedPreferences(MYCARTPREFERENCE, context.MODE_PRIVATE);
                editor = view.getContext().getSharedPreferences(MYCARTPREFERENCE, context.MODE_PRIVATE).edit();


                cd = (CardView) view.findViewById(R.id.cv);
                productnames.setSingleLine(false);
                productnames.setEllipsize(TextUtils.TruncateAt.END);
                productnames.setMaxLines(2);


                view.setClickable(true);
                //  view.setFocusableInTouchMode(true);

                removecart.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (cartlistadp.size() == 1) {
                            Intent list = new Intent(v.getContext(), Cart.class);
                            context.startActivity(list);
                            ((Activity) context).finish();
                            removeAt(getAdapterPosition());
                            Log.i(String.valueOf(getPosition()), "item");
                            Toast.makeText(context, "All items deleted from your WishList", Toast.LENGTH_LONG).show();
                        } else {
                            removeAt(getAdapterPosition());
                        }
                    }
                });
                MovewishList();
                totalPrice();
                Checkout_Button();
            }

代码:

 public void servicecallsingle(String list, final int pin) {
     url = Constants.cart_Url + "&product_id=" + list + "&pincode=" + pin;
     pDialog.show();
     JsonObjectRequest request1 = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener < JSONObject > () {
         @Override
         public void onResponse(JSONObject response) {
             JSONObject response1 = response;
             if (response1 != null) {
                 //   int status=jsonObject.optInt("status");
                 String status = response1.optString("status");
                 if (status.equalsIgnoreCase("200")) { //check the status 200 or not
                     try {
                         productpath = response1.getString("productPath");
                     } catch (JSONException e) {
                         e.printStackTrace();
                     }
                     JSONObject responses = null;
                     try {
                         responses = response1.getJSONObject("response");

                         jsonarray = responses.getJSONArray(DATA);
                     } catch (JSONException e) {
                         e.printStackTrace();
                     }

                     if (jsonarray.length() > 0) {
                         // looping through json and adding to movies list
                         for (int i = 0; i < jsonarray.length(); i++) {
                             // Log.i(String.valueOf(jsonarray.length()) + i,"firstmsg");
                             item = new CartItemoriginal();
                             JSONObject product = null;
                             try {
                                 product = jsonarray.getJSONObject(i);
                                 qtyshared = itemsQty.getString(i + 1);
                                 if (qtyshared.equalsIgnoreCase("0")) {

                                 } else {
                                     cartpid = product.getString("product_id");
                                     cartproductname = product.getString("product_name");
                                     cartaliasname = product.getString("product_alias");
                                     cartprice = product.getString("mrp_price");
                                     String sp = product.getString("selling_price");
                                     String op = product.getString("offer_selling_price");
                                     sellerid = product.getString("seller_id");
                                     JSONArray pimg = product.getJSONArray("product_images");
                                     JSONObject firstimg = pimg.getJSONObject(0);
                                     cartimg = firstimg.getString("original_res");
                                     String[] img2 = cartimg.split("\\.");
                                     String imagone = productpath + sellerid + '/' + img2[0] + '(' + '2' + '0' + '0' + ')' + '.' + img2[1];
                                     String Quantity = product.getString("product_max_add");
                                     String minqty = product.getString("product_min_add");
                                     String OutOfStockQuantity = product.getString("quantity");
                                     int qty = Integer.parseInt(Quantity);

                                     /*  **  calculation  ***/
                                     Long tsLong = System.currentTimeMillis() / 1000;
                                     String ts = tsLong.toString();
                                     int ts1 = Integer.parseInt(ts);
                                     String startdate1 = product.getString("offer_selling_start_date");
                                     String endate1 = product.getString("offer_selling_end_date");

                                     if (("".equals(startdate1)) && ("".equals(endate1))) {
                                         // Toast.makeText(getActivity(),"wrong statemnt",Toast.LENGTH_LONG).show();
                                         if (cartprice.equalsIgnoreCase(sp)) {
                                             double d = Double.parseDouble(cartprice);
                                             int mrp = (int) d;
                                             price = String.valueOf(mrp);
                                         } else {
                                             double s = Double.parseDouble(sp);
                                             int sales = (int) s;
                                             price = String.valueOf(sales);

                                         }
                                     } else {

                                         int startdate = Integer.parseInt(startdate1);
                                         int endate2 = Integer.parseInt(endate1);

                                         if (ts1 > startdate && ts1 < endate2) {
                                             double offer = Double.parseDouble(op);
                                             int offers = (int) offer;
                                             price = String.valueOf(offers);
                                         } else {
                                             if (cartprice.equalsIgnoreCase(sp)) {
                                                 double d = Double.parseDouble(cartprice);
                                                 int mrp = (int) d;
                                                 price = String.valueOf(mrp);
                                             } else {
                                                 double s = Double.parseDouble(sp);
                                                 int sales = (int) s;
                                                 price = String.valueOf(sales);
                                             }
                                         }
                                     }
                                     item.setProductname(cartproductname);
                                     item.setProductimg(imagone);
                                     item.setMaxquantity(Quantity);
                                     item.setAliasname(cartaliasname);
                                     item.setPrice(price);
                                     item.setMinquantity(minqty);
                                     item.setProductid(cartpid);
                                     item.setQty(Integer.parseInt(qtyshared));
                                     item.setTotalOutOfStockQuantity(OutOfStockQuantity);
                                     Log.i("outofstcokquanitity", OutOfStockQuantity);
                                     JSONObject pincode = product.getJSONObject("seller");
                                     process = pincode.getString("process");
                                     Message = pincode.getString("message");
                                     if (process.equalsIgnoreCase("success") && Message.equalsIgnoreCase("success")) {
                                         cartdelivery = pincode.getString("delivery");
                                         cartshippingcharge = pincode.getString("shipping_charge");
                                         String pincodes = pincode.getString("pincode");

                                         /**************************calculation of shipping days**************************/
                                         int day = Integer.parseInt(cartdelivery);
                                         Calendar c = Calendar.getInstance();
                                         String dayNumberSuffix = getDayNumberSuffix(day);
                                         SimpleDateFormat sdf = new SimpleDateFormat(" MMM d'" + dayNumberSuffix + "', yyyy");
                                         String currentDateandTime = sdf.format(new Date());
                                         try {
                                             c.setTime(sdf.parse(currentDateandTime));
                                         } catch (ParseException e) {
                                             e.printStackTrace();
                                         }
                                         c.add(Calendar.DATE, day);
                                         Date resultdate = new Date(c.getTimeInMillis());
                                         currentDateandTime = sdf.format(resultdate);

                                         Log.d(String.valueOf(currentDateandTime) + ta, "shipping days");
                                         item.setDelivery("Standard delivery by" + " " + currentDateandTime);
                                         item.setShippincharge(cartshippingcharge);
                                         item.setSellername("richard feloboune");
                                     } else {
                                         item.setError("Seller doesn't deliver to this item to" + " " + String.valueOf(pinnum));
                                     }


                                     cart.add(item);
                                 }
                                 // cart.add(new CartItemoriginal(imagone,cartproductname,cartaliasname,1,Quantity,minqty,price,cartpid));
                             } catch (JSONException e) {
                                 e.printStackTrace();
                             }
                         }
                     }
                 } // condtion check the status 200
                 else // this is if status falied in runtime
                 {
                     Toast.makeText(CartItems.this, "Status Failed in Banner Page check ur network connection", Toast.LENGTH_LONG).show();
                 }
             } else {
                 Toast.makeText(CartItems.this, "Status Failed in Banner Page check ur network connection", Toast.LENGTH_LONG).show();
             }

             // llm = new LinearLayoutManager(CartItems.this);
             if (cart == null || cart.size() == 0) {
                 RelativeLayout item = (RelativeLayout) findViewById(R.id.container_Realtivelayout);
                 LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                 myView = inflater.inflate(R.layout.activity_cart, null);
                 item.addView(myView);
                 MainLinear.setVisibility(View.GONE);
             } else {
                 MainLinear.setVisibility(View.VISIBLE);
                 final CustomLinearLayoutManagercartpage layoutManager = new CustomLinearLayoutManagercartpage(CartItems.this, LinearLayoutManager.VERTICAL, false);
                 recyleitems.setHasFixedSize(false);
                 recyleitems.setLayoutManager(layoutManager);
                 cartadapter = new CartlistAdapter(cart, CartItems.this);
                 Log.i(String.valueOf(cartadapter), "cartadapter");
                 recyleitems.setAdapter(cartadapter);
                 recyleitems.setNestedScrollingEnabled(false);
                 myView.setVisibility(View.GONE);
                 cartadapter.notifyDataSetChanged();
             }
             pincheck();
             pDialog.hide();
         }

     }, new Response.ErrorListener() {
         @Override
         public void onErrorResponse(VolleyError error) {
             Log.i("onErrorResponse", error.toString());
             pDialog.hide();
         }
     });
     AppController.getInstance().addToRequestQueue(request1);

 }

我想显示一件商品,但它显示三件缺货。任何人都可以解决这个问题。

高兴地感谢,在此先感谢。

我得到了检查条件是否正确的解决方案,但是我为其他部分设置了很多视图的可见性,因此当我进入其他部分时进行循环时,时间线性布局的可见性消失了,因此下一次循环无法显示视图,因为已经存在可见性了布局。我现在解决了。谢谢

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我无法通过将Recycler View适配器与.get(position)一起使用来从Recycler View Java中的edittext获取文本

我无法通过将Recycler View适配器与.get(position)一起使用来从Recycler View Java中的edittext获取文本

我无法通过将Recycler View适配器与.get(position)一起使用来从Recycler View Java中的edittext获取文本

致命异常:适配器中的主要(Recycler View Adapter)

无法从Recycler View适配器的setOnClick侦听器中启动活动

我在GridLayout中遇到问题,我想在其中显示49个按钮,但无法在Android Studio中使用

双击Recycler View适配器的OnClickListener

我想在我的服务中使用 UsageStatsManager

我想在View端javascript中使用Controller中的数据

如何使用适配器中使用的同一行为Recycler View创建占位符微光布局?

我想在 JDK 8 中使用 `jshell`

我想在arrayformula中使用此查询

我想在反应中使用 jquery

我想在Android中扩展SharedPreferences类

我想在 android 中创建这个 UI

我想在 Android 中阅读 Gmail 邮件

我的查询中的除数等于零

我想在插入或更新mysql之前检查

Android:在我的自定义适配器中使用数据绑定时未分配值

我想在Android应用上放大视频

我想在表中合并相同的值

我想在 python 中替换而不重叠

我想在Python中绘制垂直向量

我想在子查询中返回多行

我想在Laravel中显示主页

我想在Swift中实现ASMediasFocusDelegate

我想在Excel中创建范围

我不想在php json中打印“”

我想在 Javascript 中对 JSON 进行排序