在同一活动上再次运行时,会丢失Recyclerview数据

时间:2018-02-16 10:48:37

标签: android json android-layout android-recyclerview

我有一个购物车活动。当我第一次点击添加到购物车api时,它第一次显示正确的数据,但是当我下次进入CartActivity时,它显示空白数据。我的addtocart api工作正常。再次当我启动应用程序,它显示正确的数据,但只有一次又一次我丢失了数据,我不明白这个问题。我的3项活动正在发生。 这是我的代码

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

}

1 个答案:

答案 0 :(得分:0)

获取新数据后,在您的代码中

您不会将其发送到您的适配器,所以您必须每次都这样做!

使用此代码

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

              }