Reputation: 91
i'm doing an android application. I retrive data with GSON and display 10 elements each time i do a request. My problem is that i don't know how to add the new elements, to the elements before, in the bottom of the list. I use onScroll,so when i reach at the end of the list with 10 elements ,i make a request on the server. I'm trying to integrate some libraries from github ,but it doesn't work. Here is my code: First is my method that populate the main list:
public void popullateMainList(final String url,final long expirationTime) {
//requestData(MAIN_LIST_URL);
new AsyncTask<Void, Void, Void>() {
ProgressBar pb = (ProgressBar)findViewById(R.id.progressBar);
@Override
protected void onPreExecute() {
pb.setVisibility(View.VISIBLE);
}
@Override
protected Void doInBackground(Void... voids) {
try {
elements = new Elements(url, expirationTime);
} catch (Exception e) {
e.printStackTrace();
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(final Void result) {
pb.setVisibility(View.GONE);
updateDisplay();
mainElementAdapter.notifyDataSetChanged();
}
}.execute();
}
And the method updateDisplay has the following code:
protected void updateDisplay() {
eList = (ListView) findViewById(R.id.mainView);
mainElementAdapter = new ElementListAdapter(this, elements.getElements());
// mainElementAdapter.add(elements.getElements());
footerView = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.loading_footer, null, false);
eList.addFooterView(footerView);
eList.setAdapter(mainElementAdapter);
eList.removeFooterView(footerView);
eList.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == SCROLL_STATE_IDLE) {
if (eList.getLastVisiblePosition() >= eList.getCount() - visibleThreshold) {
currentPage++;
actualUrl = Constants.MAIN_LIST_URL + "?pageNumber=" + currentPage;
popullateMainList(actualUrl,1);
}
}}
public boolean canChildScrollUp() {
//your condition to check scrollview reached at top while scrolling
if (swipeLayout.getScrollY() == 0.0)
return true;
else
return false;
}
@Override
public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount)
{
if (firstVisibleItem == 0)
swipeLayout.setEnabled(true);
else
swipeLayout.setEnabled(false);
}
});
}
I hope that anybody help me. Thank you in advance!
Upvotes: 0
Views: 125
Reputation: 405
Try save old.
when you use
elements = new Elements(url, expirationTime);
you override your Elements object and when you use
mainElementAdapter = new ElementListAdapter(this, elements.getElements());
you override your old data in adapter
added new method in your class Elemets
something like that
if(elements == null) {
elements = new Elements(url, expirationTime);
} else {
elements.add(new Elements(url, expirationTime).getElements());
}
Upvotes: 1