Skip to content Skip to sidebar Skip to footer

How To Show Json Objects Included In Json Array In Android Recyclerview

I have the following JSON data i.e 10 objects in one array. I am fetching objects in recyclerview wihtout any error. Now I need two things to do: I want to show next page data i.e

Solution 1:

SerializedName for "meta" object should be "meta" in your LeadModel class.

publicclassLeadModel {
     
    //@SerializedName("leadsMeta")@SerializedName("meta")
    @Expose
    private LeadsMeta leadsMeta;}

Solution 2:

  1. I want to show next page data

Please use recyclerView.addOnScrollListener on your recyclerview. When user scrolls and reaches the last item, you need to load more 10 items and append the list to existing list

publicclassLeadsFragmentextendsFragment {

    RecyclerView recyclerView;
    RecyclerView.LayoutManager layoutManager;
    privatestaticfinalStringurl="myurl";
    booleanisLoading=false; // For Tracking  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    ......
    ........
    recyclerView.addOnScrollListener(newRecyclerView.OnScrollListener() {
            @OverridepublicvoidonScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @OverridepublicvoidonScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                LinearLayoutManagerlinearLayoutManager= (LinearLayoutManager) recyclerView.getLayoutManager();

                if (!isLoading) {
                    if (linearLayoutManager != null && linearLayoutManager.findLastCompletelyVisibleItemPosition() == yourList.size() - 1) {
                        // Last item reached
                        loadMore(); // Note this method
                        isLoading = true;
                    }
                }

            }
        });

 privatevoidloadMore() {
        // Make http Request// append the new data to LeadsData// notifyAdapter items changed// set isLoading = false
    }
  1. I am facing "LeadsMeta.getTotalQuotes()' on a null object reference"

Its because your serialized name in LeadModel should be as per your json

@SerializedName("meta")

Solution 3:

For your first problem:

You need to check this code. In MainActivity.java class here is a method loadMore(). In your case you need to do your API call to load 10 more items and then notify your adapter that you have new items and it will load them in your list. Thats how you can implement your recyclerview with endless items.

For 2nd problem:

You should change leadsMeta to meta in your @SerializedName. Here is your edited class:

publicclassLeadModel{

    @SerializedName("meta")@Exposeprivate LeadsMeta leadsMeta;
    @SerializedName("data")@Exposeprivate List<LeadsData> data = null;

    public LeadsMeta getMeta() {
        return leadsMeta;
    }

    public void setMeta(LeadsMeta leadsMeta) {
        this.leadsMeta = leadsMeta;
    }

    public List<LeadsData> getData() {
        returndata;
    }

    public void setData(List<LeadsData> data) {
        this.data = data;
    }

}

Post a Comment for "How To Show Json Objects Included In Json Array In Android Recyclerview"