androidretrofit2rx-java2

How to get limited no. of data in recycler view


I am fetching data using API. For that I am using Retrofit2 and RxJava2. The data is fetching successfully but I don't want to show whole items. I just want to show 5 items in a list.

Below is my code:

 Retrofit retrofit = RetrofitClient.getInstance();
     ApiService myApi = retrofit.create(ApiService.class);
     myApi.getHindiNews(data).subscribeOn(Schedulers.io())
                             .observeOn(AndroidSchedulers.mainThread())
                             .subscribe(new Observer<List<HomeHindiModel>>() {
                                 @Override
                                 public void onSubscribe(Disposable d) {

                                 }

                                 @Override
                                 public void onNext(List<HomeHindiModel> homeHindiModels) {

                                     if(homeHindiModels.size() > 0){

                                             homeHindiList.addAll(homeHindiModels);
                                             homeHindiAdapter = new HomeHindiAdapter(homeHindiList,getActivity());
                                             hindiRecycler.setAdapter(homeHindiAdapter);
                                        
                                     }
                                 }

                                 @Override
                                 public void onError(Throwable e) {

                                     Toast.makeText(getActivity(),e.getMessage(),Toast.LENGTH_SHORT).show();
                                 }

                                 @Override
                                 public void onComplete() {

                                 }
                             });

How can I get desired output?


Solution

  • into RecyclerView Adapter class

    @Override
    public int getItemCount() {
        if(homeHindiList != null) {
            if(homeHindiList.size() > 5) {
                return 5;
            } else 
                return homeHindiList.size();
        } else 
            return 0;
    }