在我的列表中,我想实现无穷无尽的列表。现在,当我向下滚动并接收到第五项列表时,新数据加载但数据未添加到列表末尾,它清除列表中的所有项目然后显示。
如何将新项目附加到列表末尾。 请注意,我使用的是adapter.notifyDataSetChanged();但仍然不适合我。
这里是我的EndlessScrollListener类:
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();
}
}
}
和我的updateDisplay函数:
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);
}
我的requestData()函数:
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类:
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;
}
}
}
注意requestData()函数使用volley获取新项目。
任何想法如何实施它?
答案 0 :(得分:1)
我不确定我是否完全理解你的问题,但是在尝试创建一个无尽的列表时#34;我一直发现使用的最佳结构是ArrayList,它具有用于添加和删除我发现非常有用的数据的预设功能。 http://developer.android.com/reference/java/util/ArrayList.html
答案 1 :(得分:0)
更新以下解决方案创建一个新的ArrayList
变量,暂时保存新数据,然后将此ArrayList添加到原来的使用ArrayList everwhere 强>
首先声明变量postList,确保它是这样的
ArrayList<Post> postList=new ArrayList<Post>(); //and make this variable global
你的parseFeed函数中的第二个
public class PostJSONParser {
public static ArrayList<Post> parseFeed(String content){
JSONArray ar = null;
try {
ar = new JSONArray(content);
ArrayList<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;
}
}
}
最后在requestData()函数中
private void requestData(String uri) {
swipeRefreshLayout.setRefreshing(true);
StringRequest request = new StringRequest(uri,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
ArrayList<Post> tempList=new ArrayList<Post>();
tempList = PostJSONParser.parseFeed(response);
postList.addAll(tempList);
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);
}
确保您始终使用ArrayList