Android 将下一页从json数据加载到listview(无休止的滚动)
一切都很顺利,但当我滚动到列表视图的底部时,列表视图会随着下一页刷新,并且不会附加到列表视图的底部。如何将新数据置于当前数据之下 我的代码Android 将下一页从json数据加载到listview(无休止的滚动),android,endlessadapter,Android,Endlessadapter,一切都很顺利,但当我滚动到列表视图的底部时,列表视图会随着下一页刷新,并且不会附加到列表视图的底部。如何将新数据置于当前数据之下 我的代码 @Override public void onScrollStateChanged(AbsListView listView, int scrollState) { if (scrollState == SCROLL_STATE_IDLE) {
@Override
public void onScrollStateChanged(AbsListView listView, int scrollState) {
if (scrollState == SCROLL_STATE_IDLE) {
if (listView.getLastVisiblePosition() >= listView.getCount() - 1 - 1) {
CURRENT_PAGE_NUMBER = CURRENT_PAGE_NUMBER + 25;
Toast.makeText(getActivity(), CURRENT_PAGE_NUMBER + " - " + JSON_AFTER, Toast.LENGTH_SHORT).show();
GetBlogPost getBlogPost = new GetBlogPost();
getBlogPost.execute();
adapter.notifyDataSetChanged();
}
}
}
private class GetBlogPost extends AsyncTask<Object, Void, JSONObject> {
@Override
protected JSONObject doInBackground(Object... params) {
int responseCode;
JSONObject jsonResponse = null;
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.website.com/" + SUBS[Integer.parseInt(SUB)] + "/.json?count=" + CURRENT_PAGE_NUMBER + "&after=" + JSON_AFTER );
try {
HttpResponse response = client.execute(httpget);
StatusLine statusLine = response.getStatusLine();
responseCode = statusLine.getStatusCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while((line = reader.readLine()) != null){
builder.append(line);
}
jsonResponse = new JSONObject(builder.toString());
}
else {
Log.i(TAG, String.format("Unsuccessful HTTP response code: %d", responseCode));
}
}
catch (JSONException e) {
Log.v(TAG, "Exception caught!", e);
}
catch (Exception e) {
Log.v(TAG, "Exception caught!", e);
}
return jsonResponse;
}
@Override
protected void onPostExecute(JSONObject result) {
mBlogData = result;
handleAsyncResponse();
}
}
private void handleAsyncResponse() {
if (mBlogData != null) {
try {
JSONObject jsonData = mBlogData.getJSONObject("data");
JSONArray jsonChildren = jsonData.getJSONArray("children");
mPostTitles = new String[jsonChildren.length()];
for (int i = 0; i < jsonChildren.length(); i ++) {
JSONObject children = jsonChildren.getJSONObject(i);
JSONObject post = children.getJSONObject("data");
JSON_AFTER = jsonData.getString("after");
String title = post.getString("title");
title = Html.fromHtml(title).toString();
mPostTitles[i] = title;
Target.add(title);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, Target);
setListAdapter(adapter);
adapter = new ArrayAdapter<String>(
getActivity(),
android.R.layout.simple_list_item_1,
Target
);
setListAdapter(adapter);
}
} catch (JSONException e) {
Log.wtf(TAG, "Exception caught!", e);
}
} else {
Toast.makeText(
getActivity(),
"Nothing to display!",
Toast.LENGTH_SHORT).show();
}
}
@覆盖
公共无效onScrollStateChanged(AbsListView listView,int scrollState){
如果(滚动状态==滚动状态空闲){
如果(listView.getLastVisiblePosition()>=listView.getCount()-1-1){
当前页码=当前页码+25;
Toast.makeText(getActivity(),当前页面号+“-”+JSON\u后面,Toast.LENGTH\u SHORT).show();
GetBlogPost GetBlogPost=新建GetBlogPost();
getBlogPost.execute();
adapter.notifyDataSetChanged();
}
}
}
私有类GetBlogPost扩展了异步任务{
@凌驾
受保护的JSONObject doInBackground(对象…参数){
国际响应码;
JSONObject jsonResponse=null;
StringBuilder=新的StringBuilder();
HttpClient=new DefaultHttpClient();
HttpGet HttpGet=新的HttpGet(“http://www.website.com/“+SUBS[Integer.parseInt(SUB)]+”/.json?count=“+CURRENT_PAGE_NUMBER+”&after=“+json_after”);
试一试{
HttpResponse response=client.execute(httpget);
StatusLine StatusLine=response.getStatusLine();
responseCode=statusLine.getStatusCode();
if(responseCode==HttpURLConnection.HTTP\u确定){
HttpEntity=response.getEntity();
InputStream内容=entity.getContent();
BufferedReader=新的BufferedReader(新的InputStreamReader(内容));
弦线;
而((line=reader.readLine())!=null){
builder.append(行);
}
jsonResponse=newJSONObject(builder.toString());
}
否则{
Log.i(TAG,String.format(“不成功的HTTP响应代码:%d”,响应代码));
}
}
捕获(JSONException e){
Log.v(标记“捕获异常!”,e);
}
捕获(例外e){
Log.v(标记“捕获异常!”,e);
}
返回jsonResponse;
}
@凌驾
受保护的void onPostExecute(JSONObject结果){
mBlogData=结果;
handleasynresponse();
}
}
私有void handleasynresponse(){
if(mBlogData!=null){
试一试{
JSONObject jsonData=mBlogData.getJSONObject(“数据”);
JSONArray jsonChildren=jsonData.getJSONArray(“儿童”);
mPostTitles=新字符串[jsonChildren.length()];
for(int i=0;i问题来自这里
adapter = new ArrayAdapter<String>...
setListAdapter(adapter);
adapter=新阵列适配器。。。
setListAdapter(适配器);
加载新的json数据后,您应该将适配器放在某个地方,尝试将其附加到现有适配器,并请求使列表视图无效。请在下面的链接中尝试使用滚动更新列表视图方法
从JSON响应获取数据后,更新适配器
adapter.notifyDataSetChanged();
我该如何把适配器放在其他地方呢;ArrayList Target=新的ArrayList()@重写public void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);lstView=getListView();lstView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);lstView.setTextFilterEnabled(true);Target.add(“hi”);Target.add(“hello”);ArrayAdapter=new ArrayAdapter=new ArrayAdapter(这个,android.R.layout.simple_list_item_checked,Target);setListAdapter(adapter);Target.add(“myname”);adapter.notifyDataSetChanged();}}我替换了“HandleBlogResponse”类中的几行代码,但现在当我滚动到底部时,我得到一个空指针异常。知道为什么吗?私有适配器;loadListView(){initadapter()}dataincome(){pustDataAdapter(适配器,数据)适配器。notifyDataSetChanged();}这不只是检查是否也滚动到底部吗?不,你也可以设置项目的数量,我的意思是在剩下3个项目时更新列表视图,或者当你设置时,我已经尝试过了,这很好…在我的情况下,我在每30个项目加载后更新列表视图,当我到达列表中的第27个项目时添加新项目