In my list I wanna Implement endless list. for now when I scroll down and receive to fifth item of list, new data load but data not add to end of list, it clear all item of list and then show.
How append new item to end of list. Notice that I'm using adapter.notifyDataSetChanged(); but still don't work for me.
here my EndlessScrollListener class:
public class EndlessScrollListener implements AbsListView.OnScrollListener {
private int visibleThreshold = 5;
// private int currentPage = 0;
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == SCROLL_STATE_IDLE) {
if (myList.getLastVisiblePosition() >= myList.getCount() - visibleThreshold) {
cpage++;
new sendpage(cpage).execute();
if (isOnline()) {
requestData("http://192.168.1.3/android_login_api/include/get_post.php");
} else {
Toast.makeText(MainActivity.this, "Network isn't available", Toast.LENGTH_LONG).show();
}
updateDisplay();
adapter.notifyDataSetChanged();
}
}
}
and my updateDisplay function:
protected void updateDisplay() {
adapter = new MyCustommAdapter(MainActivity.this, R.layout.list_item, postList);
// adapter = new MyCustommAdapter(MainActivity.this, R.layout.list_item, postList);
adapter.notifyDataSetChanged();
myList.setAdapter(adapter);
}
my requestData() function:
private void requestData(String uri) {
swipeRefreshLayout.setRefreshing(true);
StringRequest request = new StringRequest(uri,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
postList = PostJSONParser.parseFeed(response);
updateDisplay();
adapter.notifyDataSetChanged();
swipeRefreshLayout.setRefreshing(false);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError ex) {
Toast.makeText(MainActivity.this, ex.getMessage(), Toast.LENGTH_LONG).show();
swipeRefreshLayout.setRefreshing(false);
}
});
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(request);
}
PostJSONParser class:
public class PostJSONParser {
public static List<Post> parseFeed(String content){
JSONArray ar = null;
try {
ar = new JSONArray(content);
List<Post> postList = new ArrayList<Post>();
for (int j =0; j<ar.length(); j++){
JSONObject obj = ar.getJSONObject(j);
Post post = new Post();
post.setId(obj.getInt("id"));
post.setTitle(obj.getString("title"));
post.setContent(obj.getString("content"));
post.setCreated_at(obj.getString("created_at"));
post.setUrl_image(obj.getString("url_image"));
postList.add(post);
}
return postList;
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}
}
notice that requestData() function get new item using volley.
Any Idea how Implement it?