1

I have an API response that I need to show on my listView. At the beginning It starts off with 15 items, and I want to add more as the user scrolls down and reaches the end of the list. What would be the best strategy to page through the API automatically with Retrofit, so that all available data is downloaded by default? Unfortunately I am a beginner and this is as far as I can get. Please help! Any help would be appreciated.

These are the parameters from the response I need to use:

"total": 181,
    "per_page": "15",
    "current_page": 1,
    "last_page": 13,
    "next_page_url": "URL",
    "prev_page_url": null,
    "from": 1,
    "to": 15,

MainActivity:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://nmc.no-ip.org:8888/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();


        StreamingOfferService service = retrofit.create(StreamingOfferService.class);
        Call<ResponseResult> response = service.getStreamingOffers();


        response.enqueue(new Callback<ResponseResult>() {
            @Override
            public void onResponse(Call<ResponseResult> result, Response<ResponseResult> response) {
                createList(response);
            }

            @Override
            public void onFailure(Call<ResponseResult> result, Throwable t) {

            }


        });

    }

    public void createList(final Response<ResponseResult> response)

    {

        final ListView listview = (ListView) findViewById(R.id.listview);
        final StreamingAdapter adapter = new StreamingAdapter(this,R.layout.listofusers,response);
        listview.setAdapter(adapter);
        listview.setOnScrollListener(new AbsListView.OnScrollListener() {


            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {


            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

                ResponseResult r = new ResponseResult();
                int currentPageNumber = r.getCurrentPage();
                boolean loading = true;
                String next_page_url = r.getNextPageUrl();

                if (listview.getLastVisiblePosition() == listview.getAdapter().getCount() - 1
                        && listview.getChildAt(listview.getChildCount() - 1).getBottom() <= listview.getHeight()) {
                    // r.getNextPageUrl();
                   // currentPageNumber++;

                    loading = true;
                    Retrofit retrofit = new Retrofit.Builder()
                            .baseUrl(r.getNextPageUrl())  // this is not working
                            .addConverterFactory(GsonConverterFactory.create())
                            .build();

                    StreamingOfferService service = retrofit.create(StreamingOfferService.class);
                    Call<ResponseResult> response = service.getStreamingOffers();

                    response.enqueue(new Callback<ResponseResult>() {
                        @Override
                        public void onResponse(Call<ResponseResult> result, Response<ResponseResult> response) {

                            // StreamingAdapter adapter = new StreamingAdapter(this,R.layout.listofusers, response);.
                        }

                        @Override
                        public void onFailure(Call<ResponseResult> result, Throwable t) {

                        }
                    });
                }}
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();


        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

The Adapter:

public class StreamingAdapter extends BaseAdapter {

    private Context context;
    private int resource;
    private LayoutInflater inflater;
    private List<StreamingOffer> streamingOfferList;





    public StreamingAdapter(Context context, int resource, Response<ResponseResult> response) {
        this.context=context;
        this.resource=resource;
        this.inflater=LayoutInflater.from(context);
        streamingOfferList = response.body().getData();

    }

    void addToList(List<ResponseResult> updatedlist)
    {
        this.streamingOfferList.addAll(updatedlist);    // how do I add the new response here
        this.notifyDataSetChanged();
    }


    public class Holder extends User {

        private TextView full_name;
        private TextView location;
        private TextView price;
        private TextView currency_symbol;
        private TextView total_streamed;

        private  ImageView offer;
        private  ImageView profile;
        private  ImageView iconUser;
        private  ImageView iconStar;
        private  ImageView iconStar1;
        private  ImageView iconStar2;
        private  ImageView iconStar3;
        private  ImageView iconStar4;

    }



    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        Holder holder = new Holder();
        View itemView = convertView;

        StreamingOffer streamingOffer = getItem(position);
        User u = streamingOffer.getUser();

        if(itemView==null) {
            itemView = inflater.inflate(resource, parent, false);

            holder.full_name = (TextView) itemView.findViewById(R.id.full_name);
            holder.full_name.setText(u.getFullName());

            holder.location = (TextView) itemView.findViewById(R.id.location);
            holder.location.setText(streamingOffer.getLocation());


            holder.price = (TextView) itemView.findViewById(R.id.price);
            holder.currency_symbol = (TextView) itemView.findViewById(R.id.currency_symbol);

            if(streamingOffer.getIs_free()==0)
            {
                holder.price.setText(streamingOffer.getPrice());
                holder.currency_symbol.setText(streamingOffer.getCurrencySymbol());
            }
            else
                holder.price.setText("FREE");


            holder.total_streamed = (TextView) itemView.findViewById(R.id.total_streamed);
            holder.total_streamed.setText(u.getTotalStreamed());

            holder.offer = (ImageView) itemView.findViewById(R.id.offer);
            holder.profile = (ImageView) itemView.findViewById(R.id.profile);
            holder.iconUser = (ImageView) itemView.findViewById(R.id.iconUser);
            holder.iconStar = (ImageView) itemView.findViewById(R.id.iconStar);
            holder.iconStar1 = (ImageView) itemView.findViewById(R.id.iconStar1);
            holder.iconStar2 = (ImageView) itemView.findViewById(R.id.iconStar2);
            holder.iconStar3 = (ImageView) itemView.findViewById(R.id.iconStar3);
            holder.iconStar4 = (ImageView) itemView.findViewById(R.id.iconStar4);




            Images im = new Images();
            final GsonBuilder builder = new GsonBuilder();
            final Gson gson = builder.create();
            String jsonRepresentation = gson.toJson(im);

            if(jsonRepresentation.contains("75x75")){
                Picasso.with(context).load(im.getImage_75x75()).into(holder.profile);

            }


           //Picasso.with(context).load(jsonRepresentation).into(holder.offer);
            Picasso.with(context).load(im.getImage_154x154()).into(holder.profile);
            Picasso.with(context).load(im.getImage_154x154()).into(holder.offer);


            Picasso.with(context)
                    .load(R.drawable.icon_red_star).into(holder.iconStar);
            Picasso.with(context)
                    .load(R.drawable.half_red_star).into(holder.iconStar1);
            Picasso.with(context)
                    .load(R.drawable.icon_black_star).into(holder.iconStar2);
            Picasso.with(context)
                    .load(R.drawable.icon_black_star).into(holder.iconStar3);
            Picasso.with(context)
                    .load(R.drawable.icon_black_star).into(holder.iconStar4);

           // Picasso.with(context)
           //         .load("http://nmc.no-ip.org:8888/assets/images/offer-2.png").into(holder.offer);
           // Picasso.with(context)
             //       .load("http://nmc.no-ip.org:8888/assets/images/profile-1.png").into(holder.profile);


            if(u.getIs_online()==1) {
                Picasso.with(context)
                        .load(R.drawable.icon_user_online).into(holder.iconUser);
            }
            else
                Picasso.with(context)
                        .load(R.drawable.icon_user_offline).into(holder.iconUser);}




            if(u.getRating()!=null) {
                if (u.getRating() == "1.0" && u.getRating() == "1.5") {
                    Picasso.with(context)
                            .load(R.drawable.icon_red_star).into(holder.iconStar);
                    Picasso.with(context)
                            .load(R.drawable.icon_black_star).into(holder.iconStar1);
                    Picasso.with(context)
                            .load(R.drawable.icon_black_star).into(holder.iconStar2);
                    Picasso.with(context)
                            .load(R.drawable.icon_black_star).into(holder.iconStar3);
                    Picasso.with(context)
                            .load(R.drawable.icon_black_star).into(holder.iconStar4);

                }

                if (u.getRating() == "2.0" && u.getRating() == "2.5") {
                    Picasso.with(context)
                            .load(R.drawable.icon_red_star).into(holder.iconStar);
                    Picasso.with(context)
                            .load(R.drawable.icon_red_star).into(holder.iconStar1);
                    Picasso.with(context)
                            .load(R.drawable.icon_black_star).into(holder.iconStar2);
                    Picasso.with(context)
                            .load(R.drawable.icon_black_star).into(holder.iconStar3);
                    Picasso.with(context)
                            .load(R.drawable.icon_black_star).into(holder.iconStar4);

                }
                if (u.getRating() == "3.0" && u.getRating() == "3.5") {
                    Picasso.with(context)
                            .load(R.drawable.icon_red_star).into(holder.iconStar);
                    Picasso.with(context)
                            .load(R.drawable.icon_red_star).into(holder.iconStar1);
                    Picasso.with(context)
                            .load(R.drawable.icon_red_star).into(holder.iconStar2);
                    Picasso.with(context)
                            .load(R.drawable.icon_black_star).into(holder.iconStar3);
                    Picasso.with(context)
                            .load(R.drawable.icon_black_star).into(holder.iconStar4);

                }
                if (u.getRating() == "4.0" && u.getRating() == "4.5") {
                    Picasso.with(context)
                            .load(R.drawable.icon_red_star).into(holder.iconStar);
                    Picasso.with(context)
                            .load(R.drawable.icon_red_star).into(holder.iconStar1);
                    Picasso.with(context)
                            .load(R.drawable.icon_red_star).into(holder.iconStar2);
                    Picasso.with(context)
                            .load(R.drawable.icon_red_star).into(holder.iconStar3);
                    Picasso.with(context)
                            .load(R.drawable.icon_black_star).into(holder.iconStar4);

                }
                if (u.getRating() == "5.0") {
                    Picasso.with(context)
                            .load(R.drawable.icon_red_star).into(holder.iconStar);
                    Picasso.with(context)
                            .load(R.drawable.icon_red_star).into(holder.iconStar1);
                    Picasso.with(context)
                            .load(R.drawable.icon_red_star).into(holder.iconStar2);
                    Picasso.with(context)
                            .load(R.drawable.icon_red_star).into(holder.iconStar3);
                    Picasso.with(context)
                            .load(R.drawable.icon_red_star).into(holder.iconStar4);

                }
            }

        itemView.setTag(holder);
        return itemView;
    }


    @Override
    public int getCount() {

        return streamingOfferList.size();
    }

    @Override
    public StreamingOffer getItem(int position) {
        return streamingOfferList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }



}
Katherina
  • 379
  • 1
  • 2
  • 15
  • register to listen scroll changes of your listview. when scrolling code triggered, you should check the last item is visible on the screen. if it is visible, then you can call your api with paging parameters. – okarakose Apr 20 '16 at 12:17
  • I think I do that with if (listview.getLastVisiblePosition() == listview.getAdapter().getCount() - 1 && listview.getChildAt(listview.getChildCount() - 1).getBottom() <= listview.getHeight()) – Katherina Apr 20 '16 at 13:05
  • I made an update, can you please check it? – Katherina Apr 20 '16 at 13:24
  • your approach can be right but I could not test it this time. if it is not working then can you check this url ( http://stackoverflow.com/questions/5123675/find-out-if-listview-is-scrolled-to-the-bottom ) to find last item of listview is visible or not. then you can call your api function. – okarakose Apr 20 '16 at 14:42
  • I don'r know how to change nextPageUrl dynamically, any idea? – Katherina Apr 21 '16 at 07:25
  • can you paste here example url value ? that should contain some fields ( like next_max_id = [some_value] ) you should parse that value and then add query parameter to retrofit list api call – okarakose Apr 21 '16 at 08:18
  • "next_page_url": "http://nmc.no-ip.org:8888/api/v1/offers?page=2" – Katherina Apr 21 '16 at 08:27
  • okay, you should send "page" parameter with value "2" your api call. – okarakose Apr 21 '16 at 08:46
  • I did this: @GET("/page") Call loadPageUrl(); I need the page to change on every new request, how do i do that – Katherina Apr 21 '16 at 08:53
  • in your service interface : @GET("/page") Call loadPageUrl(@Query("page") String page); in your retrofit client : apiService.loadPageUrl(pageId); // if is your first request, pass null to page parameter – okarakose Apr 21 '16 at 09:13
  • I did that but now it shows me only 15items, no pagination, can you check the adapter, i updated it, to see if I am correctly adding the items – Katherina Apr 21 '16 at 09:47

0 Answers0