简体   繁体   中英

Loosing recyclerview data when going again on same activity

I have a cart activity.when i am hitting add to cart api for the first time, It's showing correct data for the first time but when i am going on CartActivity next time,it's showing blank data. my addtocart api is working fine. again when i start app,it showing correct data but only one time and again i lost the data i don't understand the problem. It's happening with my 3 activities. This is my code

rccart = findViewById(R.id.cart_rcview);
rccart.setVisibility(View.VISIBLE);
rccart.setLayoutManager(new LinearLayoutManager(CartActivity.this));
rccart.setHasFixedSize(true);
rccart.addItemDecoration(new DividerItemDecoration(CartActivity.this,
            DividerItemDecoration.VERTICAL));
rccart.setAdapter(cartAdapter);
cartArrayList= new ArrayList<>();
cartAdapter = new CartAdapter(cartArrayList, this, CartActivity.this);
CartData();
}
    public void CartData(){
    pd = new ProgressDialog(CartActivity.this);
    pd.setMessage("Loading...");
    pd.show();

    String url="https://salwartales.com/rests2/api_61.php?user_id=";
    String url2=url+userid;

    Log.i("urllll",""+url2);

    StringRequest stringRequest = new StringRequest(Request.Method.GET, url2,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    pd.dismiss();
                    //hiding the progressbar after completion
                    Log.d("Responsecart", response.toString());
                //    Toast.makeText(CartActivity.this, "responce"+response.toString(), Toast.LENGTH_SHORT).show();
                    try {
                        JSONObject jsono = new JSONObject(response);
                        if (jsono.getString("status").equals("success")) {
                            JSONArray jarray = jsono.getJSONArray("data");
                            for (int i = 0; i < jarray.length(); i++) {
                                JSONObject object = jarray.getJSONObject(i);
                                jarray = jsono.getJSONArray("data");
                                JSONArray jarray1 = object.getJSONArray("product_description");
                                for (int j = 0; j < jarray1.length(); j++) {
                                    JSONObject object1 = jarray1.getJSONObject(j);
                                    JSONArray  jarray2 = object1.getJSONArray("data");
                                    for (int k = 0; k < jarray2.length(); k++) {
                                        JSONObject object2 = jarray2.getJSONObject(k);
                                        String  Name = object2.getString("product_name");
                                         String Image = object2.getString("product_image");
                                        String    Price = object2.getString("product_price");
                                        String   Qty = object2.getString("product_qty");
                                        String   sku = object2.getString("product_sku");
                                        String   ProId = object2.getString("product_id");
                                        Cart cart= new Cart();
                                        cart.setProname(Name);
                                        cart.setProprice(Price);
                                        cart.setIdCart(ProId);
                                        cart.setProimage(Image);
                                        cartArrayList.add(cart);
                                    }
                                }
                            }
                            rccart.setAdapter(cartAdapter);
                            cartAdapter.notifyDataSetChanged();
                        } else {
                            Toast.makeText(getApplicationContext(), "Something went wrong...", Toast.LENGTH_SHORT).show();
                        }
                    } catch (Exception ex) {
                        System.out.println("EXCPTION IN SUCCESS OF LOGIN REQUEST : " + ex.toString());
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    //displaying the error in toast if occurrs
                    pd.dismiss();

                }
            });
    RequestQueue requestQueue = Volley.newRequestQueue(CartActivity.this);
    requestQueue.add(stringRequest);

}

in your code after get new data you do not send it to your adapter so you must do it every time!

use this code

public void CartData(){
pd = new ProgressDialog(CartActivity.this);
pd.setMessage("Loading...");
pd.show();

String url="https://salwartales.com/rests2/api_61.php?user_id=";
String url2=url+userid;

Log.i("urllll",""+url2);

StringRequest stringRequest = new StringRequest(Request.Method.GET, url2,
        new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                pd.dismiss();
                //hiding the progressbar after completion
                Log.d("Responsecart", response.toString());
            //    Toast.makeText(CartActivity.this, "responce"+response.toString(), Toast.LENGTH_SHORT).show();
                try {
                    JSONObject jsono = new JSONObject(response);
                    if (jsono.getString("status").equals("success")) {
                        JSONArray jarray = jsono.getJSONArray("data");
                        for (int i = 0; i < jarray.length(); i++) {
                            JSONObject object = jarray.getJSONObject(i);
                            jarray = jsono.getJSONArray("data");
                            JSONArray jarray1 = object.getJSONArray("product_description");
                            for (int j = 0; j < jarray1.length(); j++) {
                                JSONObject object1 = jarray1.getJSONObject(j);
                                JSONArray  jarray2 = object1.getJSONArray("data");
                                for (int k = 0; k < jarray2.length(); k++) {
                                    JSONObject object2 = jarray2.getJSONObject(k);
                                    String  Name = object2.getString("product_name");
                                     String Image = object2.getString("product_image");
                                    String    Price = object2.getString("product_price");
                                    String   Qty = object2.getString("product_qty");
                                    String   sku = object2.getString("product_sku");
                                    String   ProId = object2.getString("product_id");
                                    Cart cart= new Cart();
                                    cart.setProname(Name);
                                    cart.setProprice(Price);
                                    cart.setIdCart(ProId);
                                    cart.setProimage(Image);
                                    cartArrayList.add(cart);
                                }
                            }
                        }
                 cartAdapter = new CartAdapter(cartArrayList, this, CartActivity.this);
                        rccart.setAdapter(cartAdapter);
                        cartAdapter.notifyDataSetChanged();
                    } else {
                        Toast.makeText(getApplicationContext(), "Something went wrong...", Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception ex) {
                    System.out.println("EXCPTION IN SUCCESS OF LOGIN REQUEST : " + ex.toString());
                }
            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //displaying the error in toast if occurrs
                pd.dismiss();

            }
        });
RequestQueue requestQueue = Volley.newRequestQueue(CartActivity.this);
requestQueue.add(stringRequest);

              }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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