【发布时间】:2017-06-25 13:56:48
【问题描述】:
我使用 Volley 从服务器加载数据,但是如果我尝试刷新(重新加载)数据,我的 Listview 会出现双倍的问题。如果我快速点击 Button 5 次,我会得到 5 次数据。
如何在开始新的刷新之前停止第一次加载?
这就是我有时会收到此错误的原因: 致命例外:主要 进程:com.project1.proj,PID:19495 java.lang.IndexOutOfBoundsException:索引:25,大小:0
public void Load_data(View view) {
Load_data();
}
public void Load_data() {
URL="http://myweb.com/data.php;
listItems_001.clear(); // empty the old data
requestQueue = Volley.newRequestQueue(this);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, URL,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// textView.setText("");
try {
JSONArray jsonArray = response.getJSONArray("all_data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject respons = jsonArray.getJSONObject(i);
// String id = respons.getString("id") + "*";
String id = respons.getString("id");
name = respons.getString("name");
listItems_001.add(new listItems(id, name));
int request_id = Integer.valueOf(id);
if(request_id > id_last_request){
id_request_now=request_id;
}
}
} catch (JSONException e) {
e.printStackTrace();
}
listAllIteme_001(); // step 10
if (listView.getAdapter().getCount() == 0) {
textView.setVisibility(View.VISIBLE);
listView.setEmptyView(textView);
Toast.makeText(Ordered_Taxi_Driver.this,"No Message found ", Toast.LENGTH_LONG).show();
ly_progressbar_load.setVisibility(View.GONE);
}else{
Toast.makeText(Ordered_Taxi_Driver.this,"Total Items = "+listView.getAdapter().getCount(), Toast.LENGTH_LONG).show();
textView.setVisibility(View.GONE);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY", "ERROR");
}
}
);
requestQueue.add(jsonObjectRequest);
}
【问题讨论】:
-
您的代码中 IndexOutOfBoundsException-Line 的位置在哪里?
标签: listview android-volley refresh