Android列表视图数组超出范围

时间:2015-09-11 14:35:18

标签: java android listview

当我尝试调用我的ArrayList.clear()方法时,我收到此错误。

  

java.lang.IndexOutOfBoundsException:索引3无效,大小为1

该方法在我的Async类的onPreExecute中调用。我该如何解决这个问题?

private class LoadMoreDataTask extends AsyncTask<Void, Void, Void>{
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        postList.clear();

    }
    @Override
    protected Void doInBackground(Void... params) {
        ParseQuery<ParseObject> likeQuery = ParseQuery.getQuery("Likes");
        likeQuery.whereEqualTo("username", ParseUser.getCurrentUser().getUsername());
        List<ParseObject> likeList;
        try {
            likeList = likeQuery.find();
            try {
                query = new ParseQuery<ParseObject>("Images");
                query.orderByDescending("createdAt");
                query.setLimit(limit += 20);
                ob = query.find();
                for (ParseObject num : ob) {
                    PostRow test1;
                    Like singleLike = new Like(true);
                    for (int i = 0; i < likeList.size(); i++) {
                        if (likeList.get(i).get("imgId").equals(num.getObjectId())) {
                            isLiked = true;
                            break;
                        } else {
                            isLiked = false;
                        }
                    }
                    singleLike.setLikeStatus(isLiked);
                    ParseFile img = (ParseFile) num.get("img");
                    test1 = new PostRow(img.getUrl().toString(), (String) num.get("username"), num.getObjectId(), singleLike, num.getInt("likeCount"));
                    postList.add(test1);
                }
            } catch (ParseException e1) {
                e1.printStackTrace();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        adapter.notifyDataSetChanged();
        int position = listViewPosts.getFirstVisiblePosition();
        View v = listViewPosts.getChildAt(0);
        int top = (v == null) ? 0 : v.getTop();
        listViewPosts.setSelectionFromTop(position, top);
    }
}

这是完整的例外:

09-11 10:38:20.738    4281-4281/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.hristijan.likeout, PID: 4281
    java.lang.IndexOutOfBoundsException: Invalid index 29, size is 1
            at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
            at java.util.ArrayList.get(ArrayList.java:308)
            at android.widget.HeaderViewListAdapter.isEnabled(HeaderViewListAdapter.java:164)
            at android.widget.ListView.dispatchDraw(ListView.java:3307)
            at android.view.View.draw(View.java:15234)
            at android.widget.AbsListView.draw(AbsListView.java:4110)
            at android.view.View.updateDisplayListIfDirty(View.java:14167)
            at android.view.View.getDisplayList(View.java:14189)
            at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
            at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
            at android.view.View.updateDisplayListIfDirty(View.java:14127)
            at android.view.View.getDisplayList(View.java:14189)
            at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
            at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
            at android.view.View.updateDisplayListIfDirty(View.java:14127)
            at android.view.View.getDisplayList(View.java:14189)
            at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
            at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
            at android.view.View.updateDisplayListIfDirty(View.java:14127)
            at android.view.View.getDisplayList(View.java:14189)
            at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
            at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
            at android.view.View.updateDisplayListIfDirty(View.java:14127)
            at android.view.View.getDisplayList(View.java:14189)
            at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
            at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
            at android.view.View.updateDisplayListIfDirty(View.java:14127)
            at android.view.View.getDisplayList(View.java:14189)
            at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
            at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
            at android.view.View.updateDisplayListIfDirty(View.java:14127)
            at android.view.View.getDisplayList(View.java:14189)
            at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
            at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
            at android.view.View.updateDisplayListIfDirty(View.java:14127)
            at android.view.View.getDisplayList(View.java:14189)
            at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:273)
            at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:279)
            at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:318)
            at android.view.ViewRootImpl.draw(ViewRootImpl.java:2530)
            at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2352)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1982)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
            at android.view.Choreographer.doCallbacks(Choreographer.java:580)
            at android.view.Choreographer.doFrame(Choreographer.java:550)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

1 个答案:

答案 0 :(得分:2)

尝试此操作后,您必须在清除列表后传递Adapter并在其上调用notifyDataSetChanged

@Override
protected void onPreExecute() {
    super.onPreExecute();
    postList.clear();
    yourAdapter.notifyDataSetChanged();
}

修改

替代方式

private class LoadMoreDataTask extends AsyncTask<Void, Void, List<PostRow>>{
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        //postList.clear();
        //do not clear
    }
    @Override
    protected List<PostRow> doInBackground(Void... params) {
        List<PostRow> tempList = new ArrayList<PostRow>()
        ...
                    //postList.add(test1);
                    //replace this with
                    tempList.add(test1);
        ...
        return tempList;
    }

    @Override
    protected void onPostExecute(List<PostRow> tempList) {     
        super.onPostExecute(tempList);
        postList.clear();
        postList.addAll(tempList);
        adapter.notifyDataSetChanged();
        int position = listViewPosts.getFirstVisiblePosition();
        View v = listViewPosts.getChildAt(0);
        int top = (v == null) ? 0 : v.getTop();
        listViewPosts.setSelectionFromTop(position, top);
    }
}