在ListView滚动结束时加载更多结果

时间:2015-09-09 17:31:45

标签: android json android-listview

我有一个ListView,它从我创建的JSON Adapter读取数据。由于我的API每页限制为10个结果,因此我希望在用户到达列表视图的末尾时显示更多结果。

这是我到目前为止所创造的:

以下代码创建列表并检查用户是否到达列表末尾。此代码位于我的片段中。

mainListView = (StickyListHeadersListView) v.findViewById(R.id.main_listview);
        mJSONAdapter = new JSONAdapter(getActivity(), inflater);
        mainListView.setAdapter(mJSONAdapter);

        mainListView.setOnScrollListener(new AbsListView.OnScrollListener() {

            public void onScrollStateChanged(AbsListView view, int scrollState) {


            }

// Check if we reached the end of the list

            public void onScroll(AbsListView view, int firstVisibleItem,
                                 int visibleItemCount, int totalItemCount) {

                if (firstVisibleItem + visibleItemCount == totalItemCount && totalItemCount != 0) {
                    searchDistance = sharedPrefs.getDistance(getActivity());
                    if (flag_loading == false) {
                        flag_loading = true;
                        query(param1, param2, param3, **PAGE NUMBER**);
                    }
                }
            }
        });

这是我的查询()方法:

private void query(double param1, double param2, int param3, int page) {

        // Create a client to perform networking
        AsyncHttpClient client = new AsyncHttpClient();

        // Show ProgressDialog to inform user that a task in the background is occurring
        mProgress.setVisibility(View.VISIBLE);

        // Have the client get a JSONArray of data
        // and define how to respond

        client.get(MY_API_URL + "?param1=" + param1 + "&param2=" + param2 + "&param3=" + param3 + "&page=" + page,
                new JsonHttpResponseHandler() {

                    @Override
                    public void onSuccess(JSONObject jsonObject) {

                        // 11. Dismiss the ProgressDialog
                        mProgress.setVisibility(View.GONE);

                        // update the data in your custom method.
                        mJSONAdapter.updateData(jsonObject.optJSONArray("docs"));
                    }

                    @Override
                    public void onFailure(int statusCode, Throwable throwable, JSONObject error) {

                        // 11. Dismiss the ProgressDialog
                        mProgress.setVisibility(View.GONE);

                        getActivity().setContentView(R.layout.bad_connection);
                    }
                });
    }

mJSONAdapter.updateData():

public void updateData(JSONArray jsonArray) {
        // update the adapter's dataset
        mJsonArray = jsonArray;
        notifyDataSetChanged();
    }

此实际代码更新整个列表并删除旧数据。我想要做的是在用户滚动到最后时添加新数据。

希望你能帮助我,谢谢!

1 个答案:

答案 0 :(得分:0)

在您的代码中,您过度编写数据而不是添加。

创建从json文件收到的ArrayList个对象。例如:

// as a global variable in your Fragment
ArrayList<String> data = new ArrayList<>(); 

..然后以这种方式初始化你的JSONAdapter

mJSONAdapter = new JSONAdapter(getActivity(), inflater, data);

也就是说,假设您为JSONAdapter创建了一个构造函数,如下所示

public JSONAdapter(Context c, LayoutInflater i, ArrayList<String> data) {
     super(context, 0, data);
 }

...然后在query()下的JsonHttpResponseHandler().onSuccess()方法中执行此操作:

client.get(MY_API_URL + "?param1=" + param1 + "&param2=" + param2 + "&param3=" + param3 + "&page=" + page,
            new JsonHttpResponseHandler() {

                @Override
                public void onSuccess(JSONObject jsonObject) {

                    // 11. Dismiss the ProgressDialog
                    mProgress.setVisibility(View.GONE);

                    // update the data in your custom method.
                    //mJSONAdapter.updateData(jsonObject.optJSONArray("docs"));
                    JSONArray resultArray = jsonObject.optJSONArray("docs");
                    for (int i=0; i<resultArray.length(); i++) {
                        // the global field data gets updated rather than over-written
                       data.add(resultArray.get(i));
                    }
                    mJSONAdapter.update();
                }
        // ...... rest of code is probably fine
}

也将您的JSONAdapter update()方法更改为此类

public void update() {
    notifyDataSetChanged();
}