【发布时间】:2023-04-05 18:29:01
【问题描述】:
你好,我是 android 的新手,所以不要判断太强。我开发应用程序,我有 listview,我在其中获取 json 数据并使用 volley 解析它并存储在 ArrayList 中,所以我想知道现在有没有最好和最快的方法来更新 listivew :
List.clear();
//volley request
...
adapter.notifyDataSetChanged();`
但是,如果有很多数据,更新需要很长时间次它再次提供副本数据如何解决这个问题?我知道 Set 但我应该订购列表。注意:当发出 json 请求时,服务器中的某些数据可能会被删除,并且必须在应用程序的列表视图中删除它请帮助
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();
}
}
【问题讨论】:
-
首先,我认为更改您的 Internet 库不会对您的情况产生很大影响,其次您应该考虑如何解析 json,例如使用 Jackson 或 Gson 等其他库,第三您应该发布如何您将数据添加到列表中。
标签: java android json android-listview android-volley