-3

i already make user interface listview and progressbar in footerview reclerview and now i'm really stuck how to load more my data JSON?

this class get data json for first page :

private void firstPage() {

        final ProgressDialog progressDialog = new ProgressDialog(Feed_Activity.this);
        progressDialog.setTitle("Mengambil data");
        progressDialog.show();
        Log.d(Status.TAG, "dam login fungsi goOAuth");

        Call<ResponseBody> token_request;
        Retrofit retrofit;
        retrofit = new Retrofit.Builder()
                .baseUrl(Status.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        ApiService apiService = retrofit.create(ApiService.class);
        token_request = apiService.feed();

        token_request.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

                Log.d(Status.TAG, "dam  error code = " + response.code());


                if (response.code() == 200) {
                    try {

                        JSONObject responseObject = new JSONObject(response.body().string());
                        JSONArray arrayDocs =responseObject.getJSONArray("docs");


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

                         //parsing json proccess

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

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                t.printStackTrace();
                progressDialog.dismiss();
            }

        });
    }

and now what must i do in nextPage() function for load more?

private void nextPage() {

//what must i do in here for load more my json data 

}

and my api using endpoint like this

https://penawaran.8villages.com/activities/filter?challengeId=*********&limit=10&offset=0

page 1: limit=10&offset=0
page 2: limit=10&offset=10
page 3: limit=10&offset=20

please someone help me, im really stuck with my job :(

Megi Fernanda
  • 153
  • 1
  • 3
  • 12
  • May Helpful: https://stackoverflow.com/a/44449772/7874047 – Ronak Thakkar Jun 19 '17 at 05:58
  • Possible duplicate of [Endless RecyclerView with ProgressBar for pagination](https://stackoverflow.com/questions/34431734/endless-recyclerview-with-progressbar-for-pagination) – Dipali Shah Jun 19 '17 at 06:30

1 Answers1

1
ArrayList<JSONObject> jsonArrayList=new ArrayList<>(); 
   boolean isLoadMore;
    int offset=0;   
      @Override
        public void onBindViewHolder(ContactsAdapter.ViewHolder viewHolder, int position) {

        if(isLoadMore && position==jsonArrayList.size()-1)
    {
    offset+=10;
    isLoadMore =false;
    nextPage();//your logic here to hit web service
    }

        }
    ////////////////////////////

        private void firstPage() {

            final ProgressDialog progressDialog = new ProgressDialog(Feed_Activity.this);
            progressDialog.setTitle("Mengambil data");
            progressDialog.show();
            Log.d(Status.TAG, "dam login fungsi goOAuth");

            Call<ResponseBody> token_request;
            Retrofit retrofit;
            retrofit = new Retrofit.Builder()
                    .baseUrl(Status.BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();

            ApiService apiService = retrofit.create(ApiService.class);
            token_request = apiService.feed();

            token_request.enqueue(new Callback<ResponseBody>() {
                @Override
                public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

                    Log.d(Status.TAG, "dam  error code = " + response.code());


                    if (response.code() == 200) {
                        try {

                            JSONObject responseObject = new JSONObject(response.body().string());
                            JSONArray arrayDocs =responseObject.getJSONArray("docs");

    /*here if lenght of arrayDocs  will be more then or equal to limit i.e. 10 in your case then isLoadMore will be true*/

        isLoadMore=arrayDocs.length()>=10;//same condition you should be check in nextpage() method each time


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

                             //parsing json proccess 
                             /*add jsonObject into your jsonArrayList and notify your adapter with the same list*/
                             jsonArrayList.add(arrayDocs.get(i));

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

                @Override
                public void onFailure(Call<ResponseBody> call, Throwable t) {
                    t.printStackTrace();
                    progressDialog.dismiss();
                }

            });
        }
Mohd Saquib
  • 520
  • 5
  • 14
  • Your offset will be increase by 10 then simply hit your web service by passing offset via your api end point as you mentioned https://penawaran.8villages.com/activities/filter?challengeId=*********&limit=10&offset=offset ...... here offset is your int variable to which you have incremented in OnBindViewHolder as given in my previous answer. Thanks – Mohd Saquib Jul 06 '17 at 13:26