【问题标题】:Loading more results at the end of ListView scroll在 ListView 滚动结束时加载更多结果
【发布时间】:2015-12-05 19:07:42
【问题描述】:

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

这是我目前创建的:

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

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**);
                    }
                }
            }
        });

这是我的 query() 方法:

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();
    }

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

希望你能帮助我,谢谢!

【问题讨论】:

    标签: android json android-listview


    【解决方案1】:

    在您的代码中,您是在覆盖而不是添加数据。

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

    // 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);
     }
    

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

    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();
    }
    

    【讨论】:

    • 你在data.add(resultArray.get(i));中使用的“数据”对象是什么?
    • 正如我所说的从顶部阅读,它是您从“doc”收到的 json 对象的ArrayList&lt;String&gt;
    • 尝试在我的 json 适配器i.imgur.com/fTRYUAP.png中使用您的构造函数时出现错误@
    • 你的适配器应该扩展 ArrayAdapter 而不是 BaseAdapter
    • 阅读本文以了解 ArrayAdapters:stackoverflow.com/questions/19079400/…
    【解决方案2】:

    你可以从 github 下载这个库并使用它。 https://github.com/nicolasjafelle/PagingListView。 它对我来说非常有用,我希望对你有帮助;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-30
      • 1970-01-01
      • 2013-12-25
      • 1970-01-01
      • 2014-03-14
      • 1970-01-01
      • 2014-10-17
      • 1970-01-01
      相关资源
      最近更新 更多