你好,我是 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/