java - 使用 JSON Parsing with volley 更新 ListView 的最佳方法

标签 java android json android-listview android-volley

你好,我是 android 的新手,所以不要判断得太强。我开发应用程序,我有 ListView ,我在其中获取 json 数据并使用 volley 解析它并存储在 ArrayList 中,所以我想知道是否有最好和最快的方法来更新 listivew 现在我有:

      List.clear();

    //volley request
    ...
   adapter.notifyDataSetChanged();`

但是如果有很多数据,它需要花费很多时间来更新有没有人知道如何更新像 facebook 等。我试图只使用 make volley request 和 adapter.notifyDataSetChanged() 但如果更新三,它会给我双倍的数据有时它再次提供副本数据如何解决这个问题?我对 Set 但我应该订购列表。注意:当发出 json 请求时,服务器中的某些数据可能会被删除,并且它必须在应用程序的 ListView 中删除请帮助

             JsonObjectRequest jsonReq = new JsonObjectRequest(Method.GET,
                URL_FEED, null, new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        VolleyLog.d(TAG, "Response: " + response.toString());
                        if (response != null) {
                            parseJsonFeed(response);
                        }
                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        VolleyLog.d(TAG, "Error: " + error.getMessage());
                    }
                });



    void parseJsonFeed(JSONObject response) {
    try {
        JSONArray feedArray = response.getJSONArray("feed");

        for (int i = 0; i < feedArray.length(); i++) {
            JSONObject feedObj = (JSONObject) feedArray.get(i);
          //addin response to views

        }

        // notify data changes to list adapater
        listAdapter.notifyDataSetChanged();
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

最佳答案

发出 Json Volley 请求,然后使用 boolean 值检查是否有此数据,如果是,则应继续,否则应添加到 ListView ,使用 adapter.notifyDataSetChanged(); 此链接可能对您有帮助 notifyDataSetChanged example

关于java - 使用 JSON Parsing with volley 更新 ListView 的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27884599/

相关文章:

java - 由于重复类中潜在的多Dex问题导致缺少gms.maps.GoogleMap

java - 这个常规代码行有什么问题?

java - 如何仅从 Firestore 获取最新更新的数据?

java - 如何调整输入图像的大小

android - 使用已完成的 Activity 和低 kb 图像获取 OutofMemoryError/InvocationTargetException?

android - CMake(Android 工具链)- 运行链接命令时出错 : No such file or directory

java - 接收具有相同参数的整数或整数列表

java - 为什么我的 Toast 不显示?

Javascript : array. 长度返回未定义

javascript - JSON 和 JavaScript - 如何使用 json 读取我的列表