当从服务器更新数据时快速滚动列表视图时,数组索引超出范围

时间:2016-01-08 07:14:48

标签: android listview scroll indexoutofboundsexception

我有一个列表视图,它使用来自服务器的项目进行更新。

在此列表视图中,我已加载更多功能并下拉以刷新两者。

一切都运转正常,但问题是当我下拉刷新并快速滚动列表视图时,它会崩溃我做错了我不理解。

这是我的代码

lv.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            //what is the bottom iten that is visible
            int lastInScreen = firstVisibleItem + visibleItemCount;
            if (lastInScreen != 0) {
                if ((lastInScreen == totalItemCount) && !(isLoading)) {// && !(loadingMore)){
                    if (newsList.size() < total) {
                        if (isNetworkAvailable()) {
                            if (!isLoading) {
                                lv.addFooterView(footerView);
                                getNews();
                            }
                        }
                    } else {
                        lv.removeFooterView(footerView);
                    }
                }
            }
        }
    });

这是我的OnRefresh方法

@Override
public void onRefresh() {
    super.onRefresh();
    if (!isLoading) {           // no query to server is executing
        if (isNetworkAvailable()) {
            isLoading = true;
            clearList = true;
            pageNumber = 0;
            getNews();
        } else {
            toast(getString(R.string.no_internet));
            swipeRefreshLayout.setRefreshing(false);
        }
    } else {// a query is already server is executing
        swipeRefreshLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                swipeRefreshLayout.setRefreshing(false);
            }
        }, 1000);
    }
}


private void getNews() {
    pageNumber += 1;
    isLoading = true;
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
            urlNews + pageNumber, null,
            this, this);
    applicationInstance.addToRequestQueue(jsonObjReq, "News");
}


 @Override
public void onResponse(JSONObject jsonObject) {
    super.onResponse(jsonObject);
    new SaveData().execute(jsonObject);
}

public class SaveData extends AsyncTask<JSONObject, Void, Void> {

    @Override
    protected Void doInBackground(JSONObject... params) {
        JSONObject jsonObject = params[0];
        try {
            if (jsonObject.getInt("success") == 1) {
                if (clearList) {
                    newsList.clear();
                }
                JSONArray newsArray = jsonObject.getJSONArray("rooms");
                total = jsonObject.getJSONObject("paging").getInt("total");
                for (int a = 0; a < newsArray.length(); a++) {
                    JSONObject obj = newsArray.getJSONObject(a);
                    int roomID = obj.getInt("id");
                    String title = obj.getString("name");
                    String descr = obj.getString("descr");
                    RoomsData room = Select.from(RoomsData.class).where(Condition.prop("room_id").eq(roomID)).first();
                    if (room == null) {
                        room = new RoomsData(roomID, title, descr);
                        room.save();
                    } else {
                        room.name = title;
                        room.description = descr;
                        room.save();
                    }

                    JSONArray imagesArray = obj.getJSONArray("images");
                    for (int b = 0; b < imagesArray.length(); b++) {

                        JSONObject imageObj = imagesArray.getJSONObject(b);
                        String Imagename = imageObj.getString("name");
                        String url = imageObj.getString("url");
                        HotelImages.deleteAll(HotelImages.class, "room_id = ? and name = ? and url = ? ", roomID + "", Imagename, url);
                        HotelImages image = new HotelImages(0, roomID, 0, 0, 0, 0, 0, Imagename, url);
                        image.save();
                        room.imageList.add(image);

                    }
                    // tempList.add(room);
                     newsList.add(room);
                }
                store.setString(Constants.FACILITIES_AVAILABLE, jsonObject.getJSONArray("facilities").toString());
                store.setString(Constants.BOOKING_EMAIL, jsonObject.getJSONObject("reservation").getString("email"));
            }
        } catch (Exception e) {

        } finally {
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                setListView();
                isLoading = false;
                swipeRefreshLayout.setRefreshing(false);
            }
        });
    }
}


private void setListView() {
    if (newsList.size() == 1) {
        Intent it = new Intent(RoomsActivity.this, RoomDetailActivity.class);
        RoomsData pro = newsList.get(0);
        it.putExtra("RoomID", pro.roomID);
        //it.putExtra("rat",rat);
        startActivity(it);
        finish();
    } else {
        if (clearList) {
            clearList = false;
            adapterRooms = new AdapterRooms(this, newsList);
            lv.setAdapter(adapterRooms);
            if (adapterRooms == null) {
                adapterRooms = new AdapterRooms(this, newsList);
                lv.setAdapter(adapterRooms);
            } else {
                adapterRooms.notifyDataSetChanged();
            }
        } else {
            adapterRooms.notifyDataSetChanged();
        }
    }
    swipeRefreshLayout.setRefreshing(false);
    lv.removeFooterView(footerView);
}

这是我得到的例外

01-08 12:45:08.377    2373-2373/hotels.soulvant E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: hotels.soulvant, PID: 2373
java.lang.IndexOutOfBoundsException: Invalid index 5, size is 0
        at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
        at java.util.ArrayList.get(ArrayList.java:308)
        at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:225)
        at android.widget.AbsListView.obtainView(AbsListView.java:2346)
        at android.widget.ListView.makeAndAddView(ListView.java:1875)
        at android.widget.ListView.fillDown(ListView.java:702)
        at android.widget.ListView.fillGap(ListView.java:666)
        at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5029)
        at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:4577)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
        at android.view.Choreographer.doCallbacks(Choreographer.java:670)
        at android.view.Choreographer.doFrame(Choreographer.java:603)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

0 个答案:

没有答案