-3

I want to show list of data seperated by pages that is first i want to show 5 details and then when user scrolls down then it loads 5 more by again calling the api now i have been able to show first 5 details but when scrolling it replaces previous 5 with new 5 details it is not adding data instead replacing it.

MainFile in which i want to show the data

package com.example.vinod.lcoportal;


import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;


/**
 * A simple {@link Fragment} subclass.
 */
public class StbDetailsFragment extends Fragment {

    RecyclerView recycler_view_stb_details;

    ArrayList<StbDetails> listitems_stb = new ArrayList<>();
    String[] name = new String[50];
    String[] address = new String[100];
    String[] vc_stb = new String[100];
    String[] city = new String[100];
    String[] current_plan = new String[200];
    String[] status = new String[50];
    String[] csi = new String[300];

    int currentpage = 0;

    int pagesize = 5;

    ProgressDialog pDialog;

    JSONArray ja;
    JSONObject jo;

    String custcsi, custcity, custname, currentplan, custstatus, vc_no;

    String stb_url = "http://lco.denonline.in/wapp/Service1.svc/Dashboard";


    public StbDetailsFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_stb_details, container, false);

        currentpage = currentpage + 1;
        GetStb getStb= new GetStb(currentpage, pagesize);
        getStb.execute(stb_url);

        return view;

    }

    public class MyAdapterStb extends RecyclerView.Adapter<MyAdapterStb.MyViewHolderStb> {
        private ArrayList<StbDetails> list;

        public MyAdapterStb(ArrayList<StbDetails> Data) {
            list = Data;
        }

        @Override
        public MyViewHolderStb onCreateViewHolder(ViewGroup parent, int viewType) {
            // create a new view
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.stb_details_cardview, parent, false);
            MyViewHolderStb holder = new MyViewHolderStb(view);
            return holder;
        }

        @Override
        public void onBindViewHolder(final MyViewHolderStb holder, int position) {

             holder.cust_name.setText(list.get(position).getName());
            holder.cust_address.setText(list.get(position).getAddress());
            holder.cust_vc.setText(list.get(position).getVc_stb_no());
            holder.cust_city.setText(list.get(position).getCity());
            holder.cust_plan.setText(list.get(position).getCurrentPlan());
            holder.cust_status.setText(list.get(position).getStatus());
            //  holder.events_imageview.setImageResource(list.get(position).getImageResourceId());
            // holder.events_imageview.setTag(list.get(position).getImageResourceId());
            // Glide.with(getActivity()).load(list.get(position).getImageResourceId_notices()).into(holder.notices_imageview);
        }

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

        public class MyViewHolderStb extends RecyclerView.ViewHolder {

            TextView cust_name, cust_address, cust_vc, cust_city, cust_plan, cust_status ;

            public MyViewHolderStb(View v) {
                super(v);
                 cust_name = (TextView) v.findViewById(R.id.name_stb_details);
                 cust_address = (TextView) v.findViewById(R.id.address_stb_details);
                cust_vc = (TextView) v.findViewById(R.id.vc_stb_no_stb_details);
                cust_city = (TextView) v.findViewById(R.id.city_stb_details);
                cust_plan = (TextView) v.findViewById(R.id.current_plan_stb_details);
                cust_status = (TextView) v.findViewById(R.id.status_stb_details);

                //  v.setOnClickListener(new View.OnClickListener() {
                //   @Override
                // public void onClick(View v) {
                //     Intent i = new Intent(getActivity(),NoticesViewActivity.class);
                //     i.putExtra("notice_url",pdf[getAdapterPosition()]);
                //     i.putExtra("notice_title",title_notices[getAdapterPosition()]);
                //     startActivity(i);
                //  }
                // });

            }
        }
    }

    public void initializeList() {
        listitems_stb.clear();

        for (int i = 0; i < ja.length(); i++) {

            StbDetails item = new StbDetails();
            item.setName(name[i]);
            item.setAddress(address[i]);
            item.setVc_stb_no(vc_stb[i]);
            item.setCity(city[i]);
            item.setCurrentPlan(current_plan[i]);
            item.setStatus(status[i]);
            listitems_stb.add(item);



        }


    }

    public class GetStb extends AsyncTask<String,String,String> {

        HttpURLConnection httpURLConnection;

        int CurrentPage, PageSize;

        public GetStb(int currentpage, int pagesize) {
            CurrentPage = currentpage;
            PageSize = pagesize;
        }


        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(getActivity());
            pDialog.setMessage("Fetching Data...Please Wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            //pDialog.show();
           // progress_dialog.setVisibility(View.VISIBLE);
        }

        @Override
        protected String doInBackground(String... params) {

            try {
                URL url = new URL(params[0]);
                Log.d("DoInBackground:URL", url.toString());
                //Send Post Data request
                httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("GET");
                httpURLConnection.addRequestProperty("api_key", "XBoGycClZkJrXDVphgpN5c9Bb82fcKQ4");
                httpURLConnection.addRequestProperty("gulco", "fe96632f-5173-e611-942d-005056bb1e58");
                httpURLConnection.addRequestProperty("currentpage", String.valueOf(CurrentPage));
                httpURLConnection.addRequestProperty("pagesize", String.valueOf(PageSize));

                //)httpURLConnection.setDoOutput(true);
                httpURLConnection.setUseCaches(false);
                httpURLConnection.setDoInput(true);
                httpURLConnection.connect();

                //Get the Server Response
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
                String response = "";
                String line = "";

                while ((line = bufferedReader.readLine()) != null) {
                    response += line;
                }
                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
                Log.d("DoInBackground", "Response:" + response);
                return response;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String response) {

            GridLayoutManager MyLayoutManager = new GridLayoutManager(getActivity(),1);
            MyLayoutManager.setOrientation(GridLayoutManager.VERTICAL);

            recycler_view_stb_details = (RecyclerView) getActivity().findViewById(R.id.recycler_view_stb_details);
            recycler_view_stb_details.setHasFixedSize(true);

            try {
                ja = new JSONArray(response);
                //ja = jo.getJSONArray("notices");
                //images = new String[ja.length()];
                //title = new String[ja.length()];
                for (int i=0;i<ja.length();i++) {
                    JSONObject json = ja.getJSONObject(i);
                    custcsi = json.getString("CSI");
                    custcity = json.getString("City");
                    custname = json.getString("CustName");
                    currentplan = json.getString("Plan");
                    custstatus = json.getString("Status");
                    vc_no = json.getString("VCNO");

                    name[i] = custname;
                    csi[i] = custcsi;
                    city[i] = custcity;
                    current_plan[i] = currentplan;
                    vc_stb[i] = vc_no;
                    status[i] = custstatus;

                    initializeList();
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
            //progress_dialog.setVisibility(View.GONE);

            //pDialog.dismiss();

            if (listitems_stb.size() > 0 & recycler_view_stb_details != null) {
                recycler_view_stb_details.setAdapter(new MyAdapterStb(listitems_stb));
            }
            recycler_view_stb_details.setLayoutManager(MyLayoutManager);

            //pDialog.dismiss();
        }
    }
}
Anand Singh
  • 59
  • 1
  • 7

1 Answers1

0

I think your problem is that every time in the public void initializeList() you call first of all listitems_stb.clear(); so your list will be cleared every time and you will lose all the previous data.

So if you wanna add other elements to the list you shouldn't clear it. Just try to delete listitems_stb.clear();.

Then for pagination see this answer here.

Hope this helps

Community
  • 1
  • 1
Angelo Parente
  • 796
  • 2
  • 6
  • 15
  • okay now, after removing that line. I am getting my first five details but after scrolling it again comes to the top and i am also gettings null details in between my genuine details. and getting first five details at last after many null cards. – Anand Singh May 02 '17 at 09:12
  • for pagination you should see this link : http://stackoverflow.com/questions/26543131/how-to-implement-endless-list-with-recyclerview – Angelo Parente May 02 '17 at 10:02
  • I tried using this code according to me but still i can't get it to work. – Anand Singh May 02 '17 at 10:45