Search code examples
androidandroid-layoutandroid-studioandroid-recyclerviewandroid-studio-2.0

Recycler view showing only one item from the shared preference in android


I am having a problem Recycler View doesn't display all the item for my wish list. I get the product ID from Shared Preference, so i have three IDs in shared preference, but it's showing only one item in Recycler View.

Code:

 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_wishlist__items);
     powers = new ArrayList < String > ();
     recyleitems = (RecyclerView) findViewById(R.id.mywishitems);

     toolbar = (Toolbar) findViewById(R.id.toolbar);
     if (toolbar != null) {
         setSupportActionBar(toolbar);
         getSupportActionBar().setDisplayHomeAsUpEnabled(true);
         getSupportActionBar().setIcon(R.drawable.ds);
         getSupportActionBar().setDisplayShowTitleEnabled(false);
         shared = getSharedPreferences(MyPREFERENCES, MODE_PRIVATE);

         cartitemsdisplay();




     }

 }




 /********************************************************  start ***************************************************************/
 public void cartitemsdisplay() {
     String channel = (shared.getString(Constants.productid, "['']"));
     try {
         listitems = new JSONArray(channel);
     } catch (JSONException e) {
         e.printStackTrace();
     }


     if (listitems.length() > 0) {
         String items = String.valueOf(listitems);
         for (int i = 0; i < listitems.length(); i++)

         {

             try {
                 list = listitems.getString(i);
                 Log.d(list, "listnew");

                 if ("".equals(list)) {
                     Toast.makeText(getApplicationContext(), "empty null", Toast.LENGTH_LONG).show();
                 } else {

                     Wishproduct();



                 }

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



         }



     } else {
         Toast.makeText(getApplicationContext(), "No Items in the list", Toast.LENGTH_LONG).show();
     }


 }

 public void Wishproduct() {

     String wishlisturl = "http://192.168.0.33/cartwebsite3/qcrest1.0/?type=productDisplay&result=json&product_id=" + list;
     //  showpDialog();
     JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, wishlisturl, null, new Response.Listener < JSONObject > () {
         @Override
         public void onResponse(JSONObject jsonObject) {
             Log.d(TAG, jsonObject.toString());




             if (jsonObject != null) {
                 //   int status=jsonObject.optInt("status");
                 data = new ArrayList < CartitemModel > ();
                 String status = jsonObject.optString("status");
                 if (status.equalsIgnoreCase("200")) { //check the status 200 or not
                     try {
                         productpath = jsonObject.getString("productPath");
                     } catch (JSONException e) {
                         e.printStackTrace();
                     }

                     try {
                         JSONObject jsonobj1 = jsonObject.getJSONObject("response");
                         JSONArray jsonarr1 = jsonobj1.getJSONArray("data");
                         if (jsonarr1.length() > 0) {
                             for (int j = 0; j < jsonarr1.length(); j++) {
                                 JSONObject wishitems = jsonarr1.getJSONObject(j);
                                 String prodcutname = wishitems.getString("product_name");
                                 String product_alias = wishitems.getString("product_alias");
                                 String mrpprice = wishitems.getString("mrp_price");
                                 String selling_price = wishitems.getString("selling_price");
                                 String seller = wishitems.getString("seller_id");

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

                                 /************************************ end ***********************************************/

                                 JSONArray productimages = wishitems.getJSONArray("product_images");

                                 JSONObject images = productimages.getJSONObject(0);
                                 final String image = images.getString("original_res");
                                 String[] img2 = image.split("\\.");
                                 String imagone = productpath + seller + '/' + img2[0] + '(' + '2' + '0' + '0' + ')' + '.' + img2[1];

                                 data.add(new CartitemModel(prodcutname, product_alias, mrpprice, selling_price, offerprice, imagone))

                             }
                         }


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




                 } // condtion check the status 200
                 else // this is if status falied in runtime
                 {
                     Toast.makeText(wishlist_Items.this, "Status Failed in Banner Page check ur network connection", Toast.LENGTH_LONG).show();
                 }
             }




             LinearLayoutManager llm = new LinearLayoutManager(wishlist_Items.this);
             llm.setOrientation(LinearLayoutManager.VERTICAL);
             recyleitems.setLayoutManager(llm);
             wishadapter = new WishlistAdapter(data, wishlist_Items.this);
             recyleitems.setAdapter(wishadapter);
             wishadapter.notifyDataSetChanged();
         }

     }, new Response.ErrorListener() {
         @Override
         public void onErrorResponse(VolleyError error) {
             Log.e(TAG, "Server Error: " + error.getMessage());
             Toast.makeText(wishlist_Items.this, error.getMessage(), Toast.LENGTH_LONG).show();

             // stopping swipe refresh
             //   swipeRefreshLayout.setRefreshing(false);
         }
     });

     // Adding request to request queue
     AppController.getInstance().addToRequestQueue(jsObjRequest);



 }

Adapter

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

        private ArrayList < CartitemModel > WishListadp;
        DisplayImageOptions options;
        private Context context;

        public WishlistAdapter(ArrayList < CartitemModel > WishListadp, Context context) {

            this.WishListadp = WishListadp;
            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));


        }

        @Override
        public WishlistAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.wishitemsrow, viewGroup, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(WishlistAdapter.ViewHolder viewHolder, int i) {

            viewHolder.iteamname.setText(WishListadp.get(i).getCartitemname());
            viewHolder.itemalisname.setText(WishListadp.get(i).getAliasname());
            viewHolder.wishmrp.setText(WishListadp.get(i).getWishmrp());
            viewHolder.wishselling.setText(WishListadp.get(i).getWishsellingprice());
            viewHolder.wishoffer.setText(WishListadp.get(i).getWishoffer());
            viewHolder.wishratingcount.setText(WishListadp.get(i).getRatingcount());
            // viewHolder.wishrating.setText(WishListadp.get(i).getCartitemname());

            viewHolder.iteamname.setText(WishListadp.get(i).getCartitemname());
            YelloPage.imageLoader.displayImage(WishListadp.get(i).getImage(), viewHolder.wishitemimage, options);

        }

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

        public class ViewHolder extends RecyclerView.ViewHolder {
            private TextView iteamname, itemalisname, wishmrp, wishselling, wishoffer, wishratingcount;
            private ImageView wishitemimage;
            RatingBar wishrating;
            public ViewHolder(View view) {
                super(view);

                iteamname = (TextView) view.findViewById(R.id.itemname);
                itemalisname = (TextView) view.findViewById(R.id.aliasname);
                wishselling = (TextView) view.findViewById(R.id.wishselling);
                wishmrp = (TextView) view.findViewById(R.id.wishmrp);
                wishoffer = (TextView) view.findViewById(R.id.wishoffer);
                wishratingcount = (TextView) view.findViewById(R.id.ratingtxt);
                wishrating = (RatingBar) view.findViewById(R.id.rtbProductRating);
                wishitemimage = (ImageView) view.findViewById(R.id.pimage);




            }
        }

    }

Logcat

05-20 01:55:19.375 6365-6365/com.journaldev.navigationdrawer D/3: listnew
05-20 01:55:19.377 6365-6365/com.journaldev.navigationdrawer D/2: listnew
05-20 01:55:19.377 6365-6365/com.journaldev.navigationdrawer D/10: listnew
05-20 01:55:19.458 6365-6408/com.journaldev.navigationdrawer W/EGL_emulation: eglSurfaceAttrib not implemented
05-20 01:55:19.458 6365-6408/com.journaldev.navigationdrawer W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xdec24400, error=EGL_SUCCESS
05-20 01:55:19.499 6365-6365/com.journaldev.navigationdrawer E/RecyclerView: No adapter attached; skipping layout
05-20 01:55:19.546 6365-6408/com.journaldev.navigationdrawer W/EGL_emulation: eglSurfaceAttrib not implemented
05-20 01:55:19.546 6365-6408/com.journaldev.navigationdrawer W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xdef66540, error=EGL_SUCCESS
05-20 01:55:19.636 6365-6408/com.journaldev.navigationdrawer V/RenderScript: 0xdee76000 Launching thread(s), CPUs 4
05-20 01:55:19.662 6365-6365/com.journaldev.navigationdrawer D/wishlist_Items: {"status":"200","requestType":"productDisplay","basePath":"http:\/\/192.168.0.33\/cartwebsite3\/","bannerPath":"http:\/\/192.168.0.33\/cartwebsite3\/cdn-images\/banner\/","productPath":"http:\/\/192.168.0.33\/cartwebsite3\/cdn-images\/prd\/","response":{"data":[{"product_id":"3","seller_id":"1","product_active":"on","product_name":"Micromax Unite 3","product_alias":"(Blue, 8 GB)","product_sku":"21334","product_manufacturer_country":"India","product_manufacturer":"india","product_min_add":"1","product_max_add":"100","short_description":"Slim, compact and user-friendly, the Micromax Unite 3 is a good combination of powerful performance and nifty features for a great smartphone experience.","long_description":"Slim, compact and user-friendly, the Micromax Unite 3 is a good combination of powerful performance and nifty features for a great smartphone experience.","product_quantity":"0","quantity":"100","status":"In Stock","price_id":"4","mrp_price":"5900.0000","selling_price":"5099.0000","offer_percentage":"","product_weight":"130g","offer_selling_price":"2000.0000","offer_selling_percentage":"","offer_selling_start_date":"1461621600","offer_selling_end_date":"1469484000","shipping_cost":"0.0000","product_images":[{"image_id":"18","product_id":"3","image_small":"","image_medium":"","image_large":"","original_res":"micromax-unite-3-951452579749.jpeg","image_type":"jpeg","added_date":"1452579749","added_user":"1","sort":"0"},{"image_id":"19","product_id":"3","image_small":"","image_medium":"","image_large":"","original_res":"micromax-unite-3-241452579749.jpeg","image_type":"jpeg","added_date":"1452579749","added_user":"1","sort":"0"},{"image_id":"20","product_id":"3","image_small":"","image_medium":"","image_large":"","original_res":"micromax-unite-3-301452579749.jpeg","image_type":"jpeg","added_date":"1452579749","added_user":"1","sort":"0"}],"basic_info":[{"basic_id":"550","product_id":"3","option_name":"Features","option_description":"Android v5 OS","have_connected":"0"},{"basic_id":"551","product_id":"3","option_name":"Features","option_description":"8 MP Primary Camera","have_connected":"0"},{"basic_id":"552","product_id":"3","option_name":"Features","option_description":"2MP Secondary Camera","have_connected":"0"},{"basic_id":"553","product_id":"3","option_name":"Features","option_description":"Dual Sim (GSM   WCDMA)","have_connected":"0"}],"extra_info":[{"type":"textfield","main_header":"Micromax Unite 3(Blue, 8 GB)","field_option":[{"inside_single_title":"Specifications","basic_opt1":[{"option_name_extra":"In the Box","option_desc_extra":"Handset, USB Cable, Charger, User Guide, Warranty Card, Battery, Hands-free"},{"option_name_extra":"Sensors","option_desc_extra":"Light Sensor, Gravity Sensor, Proximity Sensor"},{"option_name_extra":"User Memory\t","option_desc_extra":"4.9 GB for Apps and Phone Storage"},{"option_name_extra":"Processor","option_desc_extra":"1.3 GHz   MTK 6582M, Quad Core"}]}]}],"category_list":[{"category_id":"46","category_name":"ELECTRONICS","category_desc":"","category_image":"","category_thumbnail":"","category_image_desc":"","category_meta_title":"","category_meta_desc":"","category_meta_keyword":"","category_show_hide":"0","category_vanity":"electronics"},{"category_id":"48","category_name":"Mobiles","category_desc":"","category_image":"","category_thumbnail":"","category_image_desc":"","category_meta_title":"","category_meta_desc":"","category_meta_keyword":"","category_show_hide":"0","category_vanity":"mobiles"},{"category_id":"54","category_name":"Micromax","category_desc":"","category_image":"","category_thumbnail":"","category_image_desc":"","category_meta_title":"","category_meta_desc":"","category_meta_keyword":"","category_show_hide":"0","category_vanity":"micromax"}]}]},"request":{"postData":[],"getData":{"type":"productDisplay","result":"json","product_id":"3"}}}
05-20 01:55:19.678 6365-6365/com.journaldev.navigationdrawer D/wishlist_Items: {"status":"200","requestType":"productDisplay","basePath":"http:\/\/192.168.0.33\/cartwebsite3\/","bannerPath":"http:\/\/192.168.0.33\/cartwebsite3\/cdn-images\/banner\/","productPath":"http:\/\/192.168.0.33\/cartwebsite3\/cdn-images\/prd\/","response":{"data":[{"product_id":"2","seller_id":"1","product_active":"on","product_name":"Lenovo A6000","product_alias":"(Black, 8 GB)","product_sku":"258148562","product_manufacturer_country":"India","product_manufacturer":"india","product_min_add":"1","product_max_add":"1000","short_description":"The Lenovo A6000 is a smartphone designed for multimedia and music lovers on a budget.","long_description":"The Lenovo A6000 is a smartphone designed for multimedia and music lovers on a budget. It features a 5 wide-view HD display that offers good resolution. It also has a Snapdragon quad core processor that delivers responsive system performance.","product_quantity":"0","quantity":"150","status":"In Stock","price_id":"3","mrp_price":"7500.0000","selling_price":"7099.0000","offer_percentage":"","product_weight":"146g","offer_selling_price":"0.0000","offer_selling_percentage":"","offer_selling_start_date":"","offer_selling_end_date":"","shipping_cost":"0.0000","product_images":[{"image_id":"12","product_id":"2","image_small":"","image_medium":"","image_large":"","original_res":"lenovo-a6000-171452579105.jpeg","image_type":"jpeg","added_date":"1452579106","added_user":"1","sort":"0"},{"image_id":"13","product_id":"2","image_small":"","image_medium":"","image_large":"","original_res":"lenovo-a6000-791452579106.jpeg","image_type":"jpeg","added_date":"1452579106","added_user":"1","sort":"0"},{"image_id":"14","product_id":"2","image_small":"","image_medium":"","image_large":"","original_res":"lenovo-a6000-971452579106.jpeg","image_type":"jpeg","added_date":"1452579106","added_user":"1","sort":"0"},{"image_id":"15","product_id":"2","image_small":"","image_medium":"","image_large":"","original_res":"lenovo-a6000-341452579106.jpeg","image_type":"jpeg","added_date":"1452579106","added_user":"1","sort":"0"},{"image_id":"16","product_id":"2","image_small":"","image_medium":"","image_large":"","original_res":"lenovo-a6000-891452579106.jpeg","image_type":"jpeg","added_date":"1452579106","added_user":"1","sort":"0"},{"image_id":"17","product_id":"2","image_small":"","image_medium":"","image_large":"","original_res":"lenovo-a6000-441452579106.jpeg","image_type":"jpeg","added_date":"1452579106","added_user":"1","sort":"0"}],"basic_info":[{"basic_id":"9","product_id":"2","option_name":"Features","option_description":"5 inch HD IPS Display","have_connected":"0"},{"basic_id":"10","product_id":"2","option_name":"Features","option_description":"Twin Dolby Speaker","have_connected":"0"},{"basic_id":"11","product_id":"2","option_name":"Features","option_description":"1.2 GHz Quad Core","have_connected":"0"},{"basic_id":"12","product_id":"2","option_name":"Features","option_description":"Dual SIM,4G LTE","have_connected":"0"}],"extra_info":[{"type":"textfield","main_header":"Lenovo A6000(Black, 8 GB)","field_option":[{"inside_single_title":"Specifications","basic_opt1":[{"option_name_extra":"Network Type","option_desc_extra":"4G, 3G"},{"option_name_extra":"Standby Time","option_desc_extra":"264 hrs (2G), 264 hrs (3G)"},{"option_name_extra":"OS","option_desc_extra":"Android v38081 (KitKat)"},{"option_name_extra":"Graphics","option_desc_extra":"Adreno 306; 400 MHz Speed"}]}]}],"category_list":[{"category_id":"46","category_name":"ELECTRONICS","category_desc":"","category_image":"","category_thumbnail":"","category_image_desc":"","category_meta_title":"","category_meta_desc":"","category_meta_keyword":"","category_show_hide":"0","category_vanity":"electronics"},{"category_id":"48","category_name":"Mobiles","category_desc":"","category_image":"","category_thumbnail":"","category_image_desc":"","category_meta_title":"","category_meta_desc":"","category_meta_keyword":"","category_show_hide":"0","category_vanity":"mobiles"},{"category_id":"51","category_name":"Lenovo","category_des
05-20 01:55:19.685 6365-6365/com.journaldev.navigationdrawer D/wishlist_Items: {"status":"200","requestType":"productDisplay","basePath":"http:\/\/192.168.0.33\/cartwebsite3\/","bannerPath":"http:\/\/192.168.0.33\/cartwebsite3\/cdn-images\/banner\/","productPath":"http:\/\/192.168.0.33\/cartwebsite3\/cdn-images\/prd\/","response":{"data":[{"product_id":"10","seller_id":"1","product_active":"on","product_name":"Micromax Canvas Nitro 2","product_alias":"(White  Gold, 16 GB)","product_sku":"143234354","product_manufacturer_country":"India","product_manufacturer":"micromax","product_min_add":"1","product_max_add":"100","short_description":"Watch a trending 9GAG video in HD while you check your text messages, or take breathtaking snaps of passing scenery from your car with this impressive smartphone.","long_description":"Watch a trending 9GAG video in HD while you check your text messages, or take breathtaking snaps of passing scenery from your car with this impressive smartphone.","product_quantity":"0","quantity":"1000","status":"In Stock","price_id":"11","mrp_price":"8500.0000","selling_price":"7800.0000","offer_percentage":"","product_weight":"150g","offer_selling_price":"0.0000","offer_selling_percentage":"","offer_selling_start_date":"1461621600","offer_selling_end_date":"1467064800","shipping_cost":"0.0000","product_images":[{"image_id":"21","product_id":"10","image_small":"","image_medium":"","image_large":"","original_res":"micromax-canvas-nitro-2-131452580405.jpeg","image_type":"jpeg","added_date":"1452580405","added_user":"1","sort":"0"},{"image_id":"22","product_id":"10","image_small":"","image_medium":"","image_large":"","original_res":"micromax-canvas-nitro-2-161452580405.jpeg","image_type":"jpeg","added_date":"1452580405","added_user":"1","sort":"0"}],"basic_info":[{"basic_id":"546","product_id":"10","option_name":"Features","option_description":"Android v4.4.2 OS","have_connected":"0"},{"basic_id":"547","product_id":"10","option_name":"Features","option_description":"13 MP Primary Camera","have_connected":"0"},{"basic_id":"548","product_id":"10","option_name":"Features","option_description":"5MP Secondary Camera","have_connected":"0"},{"basic_id":"549","product_id":"10","option_name":"Features","option_description":"Dual Sim (GSM   WCDMA)","have_connected":"0"}],"extra_info":[{"type":"textfield","main_header":"Micromax Canvas Nitro 2(White  Gold, 16 GB)","field_option":[{"inside_single_title":"Specifications ","basic_opt1":[{"option_name_extra":"Other Camera Features","option_desc_extra":"Auto Focus, Touch Focus, Face Detection, Smile Detection, Scene Modes, HDR, Face Beauty"},{"option_name_extra":"Additional Features","option_desc_extra":"Organizer, World Clock, To-do List, Reminders, Notes, Calendar, Calculator, Clock, Backup and Restore, Call Waiting, Multi-languages Supported, 5-pin Micro USB Port"},{"option_name_extra":"Resolution","option_desc_extra":"HD, 1280 x 720 Pixels"},{"option_name_extra":"Memory","option_desc_extra":"2 GB RAM, 16 GB ROM"}]}]}],"category_list":[{"category_id":"46","category_name":"ELECTRONICS","category_desc":"","category_image":"","category_thumbnail":"","category_image_desc":"","category_meta_title":"","category_meta_desc":"","category_meta_keyword":"","category_show_hide":"0","category_vanity":"electronics"},{"category_id":"48","category_name":"Mobiles","category_desc":"","category_image":"","category_thumbnail":"","category_image_desc":"","category_meta_title":"","category_meta_desc":"","category_meta_keyword":"","category_show_hide":"0","category_vanity":"mobiles"},{"category_id":"54","category_name":"Micromax","category_desc":"","category_image":"","category_thumbnail":"","category_image_desc":"","category_meta_title":"","category_meta_desc":"","category_meta_keyword":"","category_show_hide":"0","category_vanity":"micromax"}]}]},"request":{"postData":[],"getData":{"type":"productDisplay","result":"json","product_id":"10"}}}

Anyone solve my problem. Glad to appreciate Thanks.


Solution

  • Because in your for loop you are creating new instance of data on each loop

    for (int j = 0; j < jsonarr1.length(); j++) {
        //your code
        data = new ArrayList<CartitemModel>();
        data.add(new CartitemModel(prodcutname, product_alias, mrpprice, selling_price, offerprice, imagone));
    }
    

    and you need do like this

    data = new ArrayList<CartitemModel>();
    for (int j = 0; j < jsonarr1.length(); j++) {
        //your code
        data.add(new CartitemModel(prodcutname, product_alias, mrpprice, selling_price, offerprice, imagone));
    }