I'm trying to learn to implement an endlessscrolllistener for an assignment in a free Android course that I'm following on Coursera. The main problem I'm running into is that my Gridview doesn't update when I try to run notifyDataSetChanged(); in my adapter.
This is the Endlessscrollistener that I try to implement.
public abstract class EndlessScrollListener implements AbsListView.OnScrollListener {
// The minimum number of items to have below your current scroll position
// before loading more.
private int visibleThreshold = 5;
// The current offset index of data you have loaded
private int currentPage = 0;
// The total number of items in the dataset after the last load
private int previousTotalItemCount = 0;
// True if we are still waiting for the last set of data to load.
private boolean loading = true;
// Sets the starting page index
private int startingPageIndex = 0;
public EndlessScrollListener() {
}
public EndlessScrollListener(int visibleThreshold) {
this.visibleThreshold = visibleThreshold;
}
public EndlessScrollListener(int visibleThreshold, int startPage) {
this.visibleThreshold = visibleThreshold;
this.startingPageIndex = startPage;
this.currentPage = startPage;
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
{
// If the total item count is zero and the previous isn't, assume the
// list is invalidated and should be reset back to initial state
if (totalItemCount < previousTotalItemCount) {
this.currentPage = this.startingPageIndex;
this.previousTotalItemCount = totalItemCount;
if (totalItemCount == 0) { this.loading = true; }
}
// If it's still loading, we check to see if the dataset count has
// changed, if so we conclude it has finished loading and update the current page
// number and total item count.
if (loading && (totalItemCount > previousTotalItemCount)) {
loading = false;
previousTotalItemCount = totalItemCount;
currentPage++;
}
// If it isn't currently loading, we check to see if we have breached
// the visibleThreshold and need to reload more data.
// If we do need to reload some more data, we execute onLoadMore to fetch the data.
if (!loading && (firstVisibleItem + visibleItemCount + visibleThreshold) >= totalItemCount ) {
loading = onLoadMore(currentPage + 1, totalItemCount);
}
}
// Defines the process for actually loading more data based on page
// Returns true if more data is being loaded; returns false if there is no more data to load.
public abstract boolean onLoadMore(int page, int totalItemsCount);
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// Don't take any action on changed
}
}
In my main fragment I have implemented this by setting the scrolllistener of my Gridview in the following way:
gridview.setOnScrollListener(new EndlessScrollListener() {
@Override
public boolean onLoadMore(int page, int totalItemsCount) {
// Triggered only when new data needs to be appended to the list
// Add whatever code is needed to append new items to your AdapterView
loadNextDataFromApi(page);
// or loadNextDataFromApi(totalItemsCount);
return true; // ONLY if more data is actually being loaded; false otherwise.
}
});
That should run this function to create an Imageloader I use to get data from an API:
// Append the next page of data into the adapter
// This method probably sends out a network request and appends new data items to your adapter.
public void loadNextDataFromApi(int page) {
/* Check if there is an network connection available */
if(networkAvailable())
{
/* Load an images from network */
gridview.setVisibility(GridView.VISIBLE);
new ImageLoader(posterWidth, getContext(), page, gridadapter).execute();
}
else
{
gridview.setVisibility(GridView.GONE);
}
System.out.println("pagenumber: " + page);
}
This Imageloader uses the onPostExecute to call the addItems() function from my gridadapter. (This gridadapter is given to the ImageLoader from the main fragment)
@Override
protected void onPostExecute(ArrayList<String> result)
{
if(result!=null)
{
if (page == 1)
{
ImageAdapter adapter = new ImageAdapter(this.context,result, posterWidth);
gridview.setAdapter(adapter);
}
else
{
gridadapter.addItems(result);
}
}
}
In my adapter I have a function called addItems:
public void addItems(ArrayList<String> addedAPIPaths) {
apiPaths.addAll(addedAPIPaths);
notifyDataSetChanged();
}
The problem I have is that when I call this function to update the items in my gridview, I want my gridview to redraw itself. I tried a lot of different ways and I tought that notifyDataSetChanged(); should do this for me automatically, but for some reason it doesn't work for me.
I am using this getView in my adapter:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView iView;
if(convertView == null)
{
iView = new ImageView(context);
}
else
{
iView= (ImageView) convertView;
}
Drawable d = resizeDrawable(ContextCompat.getDrawable(context, R.drawable.noposter));
Picasso.with(context).load("http://image.tmdb.org/t/p/w200/" + apiPaths.get(position)).
resize(posterWidth, (int)(posterWidth*)).placeholder(d).into(iView);
return iView;
}
Does anyone see what I'm doing wrong? I'm working on this problem for many hours now. I tried searching on StackOverflow and there are many people who also have the problem that their GridView doesn't redraws, but I didn't find the solution to learn what I did wrong yet.