Julia
Julia

Reputation: 369

How to get all GitHub API results?

I use GitHub search API for repositories searching. I know how I can result from the 3rd, 4th and etc pages. (https://api.github.com/search/repositories?q=java&page=2&per_page=30>) . For this in code I use this method:


      private void pagesPagination() {
            recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
                    if (!recyclerView.canScrollVertically(1) && dy != 0) {
                        if (layoutManager.findLastCompletelyVisibleItemPosition() ==
                                adapter.getItemCount() - 1) {
                          page++;
                     itemViewModel.searchItems(q, page, resultsPerPage);
                        itemViewModel.getNewItem().observe(MainActivity.this, responseObject -> {
                            adapter.addItems(responseObject.get(0).getItems());
                        });
                        }
                    }
                }
            });
        }
    }

 public class ItemViewModel extends AndroidViewModel {

    private ItemRepository itemRepository;
    private LiveData<Root> itemsResponseLiveData;
    private List<Root> items;
    private MutableLiveData<List<Root>> addedItems;

    public ItemViewModel(@NonNull Application application) {
        super(application);
    }

    public void init() {
        itemRepository = new ItemRepository();
        itemsResponseLiveData = itemRepository.getItemsResponseLiveData();
        items = new ArrayList<>();
        addedItems = new MutableLiveData<List<Root>>();
    }

    public Root addItem() {
     return itemsResponseLiveData.getValue();
    }

    public void searchItems(String q, int page, int resultsPerPage) {
        itemRepository.fetchData(q, page, resultsPerPage);
        items.add(addItem());
        addedItems.setValue(items);
    }

    public LiveData<Root> getItemsResponseLiveData() {
        return itemsResponseLiveData;
    }
    public MutableLiveData<List<Root>> getNewItem(){
        return addedItems;
    }
}

in adapter class:

public void addItems(List<Item> newItem ) {
    items.addAll(newItem);
    notifyDataSetChanged();
}

but if I scroll down this new list, I don't see previous results. Could I do that I see all results from all previous pages?

Upvotes: 1

Views: 170

Answers (1)

AmrDeveloper
AmrDeveloper

Reputation: 4712

Your problem may be in how you used the LiveData inside the ViewModel,

If you use liveData.setValue(newList) this will not append the new items, instead, it will just set the value to be the new list only,

In your case you need to save the old items maybe in ArrayList and once you got new items append them first in the ArrayList, then use liveData.setValue(arrayList) to put all the items in the liveData and the adapter will receive all the items from it

Check this answer you will find more information and code about this solution

Notify Observer when item is added to List of LiveData

Upvotes: 1

Related Questions