繁体   English   中英

在Android中显示第一个Json URL后如何更新Recylerview View项目

[英]How to update the Recylerview View items after displayed First Json URL In android

我在Recycler View中更新商品时遇到问题。我会清楚地说明我的问题。

我正在使用两个Json URL,第一个Json URL可以将项目发送到模型类。 完成此操作后,称为适配器,然后通过使用setter模型类使用适配器更新第二个Json URL项。

因此,这就是为什么调用adapter.notifyitemchanged的原因,但是下一次只能更新一次,而循环不会更新第二次显示为空的项目。

码:

 public void servicecallsingle(String list, final int pin) {
 url = Constants.singleproducturl + list;


 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();
                 }
                 try {
                     JSONObject responses = response1.getJSONObject("response");
                     jsonarray = responses.getJSONArray(DATA);

                     if (jsonarray.length() > 0) {
                         // looping through json and adding to movies list
                         for (int i = 0; i < jsonarray.length(); i++) {
                             item = new CartItemoriginal();
                             JSONObject product = jsonarray.getJSONObject(i);
                             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");
                             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.setQty(1);
                             item.setProductimg(imagone);
                             item.setMaxquantity(Quantity);
                             item.setAliasname(cartaliasname);
                             item.setPrice(price);
                             item.setMinquantity(minqty);
                             item.setProductid(cartpid);
                             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();
             }


             // llm = new LinearLayoutManager(CartItems.this);

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

         String id = cartpid;
         String selleid = sellerid;
         final int pinnum = pin;
         String pinurl = "http://192.168.0.33/sharpswebsite3/qcrest1.0/?type=pinCodeCheck&result=json&product_id=" + id + "&seller_id=" + selleid + "&pinCode=" + pinnum;
         JsonObjectRequest request2 = new JsonObjectRequest(Request.Method.GET, pinurl, null, new Response.Listener < JSONObject > () {
             @Override
             public void onResponse(JSONObject responsesecond) {
                 JSONObject response2 = responsesecond;
                 // do something with response1 & response here...
                 if (response2 != null) {
                     //   int status=jsonObject.optInt("status");
                     String status = response2.optString("status");
                     if (status.equalsIgnoreCase("200")) { //check the status 200 or not
                         try {
                             JSONObject responses = response2.getJSONObject("response");
                             jsonarray = responses.getJSONArray(DATA);
                             if (jsonarray.length() > 0) {
                                 Log.i(String.valueOf(jsonarray.length()), "message");
                                 // looping through json and adding to movies list
                                 for (int j = 0; j < jsonarray.length(); j++) {
                                     JSONObject product = jsonarray.getJSONObject(j);
                                     process = product.getString("process");
                                     Message = product.getString("message");
                                     if (process.equalsIgnoreCase("success") && Message.equalsIgnoreCase("success")) {
                                         cartdelivery = product.getString("delivery");
                                         cartshippingcharge = product.getString("shipping_charge");
                                         String pincode = product.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), "shipping days");
                                         cart.get(j).setDelivery("Standard delivery by" + " " + currentDateandTime);
                                         cart.get(j).setShippincharge(cartshippingcharge);
                                         cart.get(j).setSellername("richard feloboune");

                                         cartadapter.notifyItemChanged(j);
                                         cartadapter.notifyItemRangeChanged(j, cart.size());

                                     } else {
                                         // cart2.add(new Cartitemoringinaltwo("Seller doesn't deliver to this item to"+" "+ String.valueOf(pinnum)));
                                         cart.get(j).setError("Seller doesn't deliver to this item to" + " " + String.valueOf(pinnum));
                                         cartadapter.notifyItemChanged(j);
                                     }
                                 }
                             }

                         } catch (JSONException e) {
                             e.printStackTrace();
                         }
                         // stopping swipe refresh
                         // swipeRefreshLayout.setRefreshing(false);
                     } // 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();
                     }
                 }



                 pincheck();
             }

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

     }


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

}

注意:第一次循环时可以在setter模型类中设置项目,但第二次则不能将其设置为模型类。

任何解决此问题的人都将不胜感激。

提前致谢

问题是截击不等待请求完成。 因此,在几秒钟内拨打完第一个电话后,也会打另一个电话。 因此,您需要创建一个接口,该接口将在调用第一个Web服务时被调用,然后在接口中调用其他Web服务,而不是notifyDataSet。

     public void servicecallsingle(String list, final int pin) {
    url = Constants.singleproducturl + list;


  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();
             }
            parseJson(response1, new  WebServiceCallBack{
            public void getWebserviceCallBack(){
            // Call another webservice here
          String id = cartpid;
     String selleid = sellerid;
     final int pinnum = pin;
     String pinurl = "http://192.168.0.33/sharpswebsite3/qcrest1.0/?type=pinCodeCheck&result=json&product_id=" + id + "&seller_id=" + selleid + "&pinCode=" + pinnum;
     JsonObjectRequest request2 = new JsonObjectRequest(Request.Method.GET, pinurl, null, new Response.Listener < JSONObject > () {
         @Override
         public void onResponse(JSONObject responsesecond) {
             JSONObject response2 = responsesecond;
             // do something with response1 & response here...
             if (response2 != null) {
                 //   int status=jsonObject.optInt("status");
                 String status = response2.optString("status");
                 if (status.equalsIgnoreCase("200")) { //check the status 200 or not
                     try {
                         JSONObject responses = response2.getJSONObject("response");
                         jsonarray = responses.getJSONArray(DATA);
                         if (jsonarray.length() > 0) {
                             Log.i(String.valueOf(jsonarray.length()), "message");
                             // looping through json and adding to movies list
                             for (int j = 0; j < jsonarray.length(); j++) {
                                 JSONObject product = jsonarray.getJSONObject(j);
                                 process = product.getString("process");
                                 Message = product.getString("message");
                                 if (process.equalsIgnoreCase("success") && Message.equalsIgnoreCase("success")) {
                                     cartdelivery = product.getString("delivery");
                                     cartshippingcharge = product.getString("shipping_charge");
                                     String pincode = product.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), "shipping days");
                                     cart.get(j).setDelivery("Standard delivery by" + " " + currentDateandTime);
                                     cart.get(j).setShippincharge(cartshippingcharge);
                                     cart.get(j).setSellername("richard feloboune");

                                     cartadapter.notifyItemChanged(j);
                                     cartadapter.notifyItemRangeChanged(j, cart.size());

                                 } else {
                                     // cart2.add(new Cartitemoringinaltwo("Seller doesn't deliver to this item to"+" "+ String.valueOf(pinnum)));
                                     cart.get(j).setError("Seller doesn't deliver to this item to" + " " + String.valueOf(pinnum));
                                     cartadapter.notifyItemChanged(j);
                                 }
                             }
                         }

                     } catch (JSONException e) {
                         e.printStackTrace();
                     }
                     // stopping swipe refresh
                     // swipeRefreshLayout.setRefreshing(false);
                    } // 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();
                 }
             }



             pincheck();
         }

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

 }


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

            });
             }

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


         // llm = new LinearLayoutManager(CartItems.this);

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






      public void parseJson(JSONObject response1,  WebServiceCallBack       webserviceCallBack){
       try {
                 JSONObject responses = response1.getJSONObject("response");
                 jsonarray = responses.getJSONArray(DATA);

                 if (jsonarray.length() > 0) {
                     // looping through json and adding to movies list
                     for (int i = 0; i < jsonarray.length(); i++) {
                         item = new CartItemoriginal();
                         JSONObject product = jsonarray.getJSONObject(i);
                         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");
                         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.setQty(1);
                         item.setProductimg(imagone);
                         item.setMaxquantity(Quantity);
                         item.setAliasname(cartaliasname);
                         item.setPrice(price);
                         item.setMinquantity(minqty);
                         item.setProductid(cartpid);
                         cart.add(item);


                         // cart.add(new       CartItemoriginal(imagone,cartproductname,cartaliasname,1,Quantity,minqty,price,cartpid));

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


                 webserviceCallBack.getWebserviceCallBack();

                 }



                 public interface WebServiceCallBack{
        public void getWebserviceCallBack()
     }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM