Java Android listview数组超出范围

Java Android listview数组超出范围,java,android,listview,Java,Android,Listview,当我尝试调用我的ArrayList.clear()方法时,我得到了这个错误 java.lang.IndexOutOfBoundsException:索引3无效,大小为1 该方法在异步类的onPreExecute中调用。。我怎样才能解决这个问题 private class LoadMoreDataTask extends AsyncTask<Void, Void, Void>{ @Override protected void onPreExecute() {

当我尝试调用我的ArrayList.clear()方法时,我得到了这个错误

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

该方法在异步类的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);
    }
}

尝试此操作,您必须传入
适配器
,并在清除列表后在其上调用
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);
    }
}
私有类LoadMoreDataTask扩展了AsyncTask{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
//postList.clear();
//不清楚
}
@凌驾
受保护列表doInBackground(无效…参数){
List templast=new ArrayList()
...
//add(test1);
//替换为
添加(test1);
...
返回圣殿骑士;
}
@凌驾
受保护的void onPostExecute(列表模板列表){
super.onPostExecute(圣殿骑士);
postList.clear();
addAll(圣殿骑士);
adapter.notifyDataSetChanged();
int position=listViewPosts.getFirstVisiblePosition();
视图v=listViewPosts.getChildAt(0);
int top=(v==null)?0:v.getTop();
listViewPosts.setSelectionFromTop(位置,顶部);
}
}

你了解什么是IndexOutofBoundsCeptionies了吗,我知道那是什么,我只是想不出在我的例子中是什么原因。.错误标记的是哪一行?日志中没有指定行,但是当我删除postList.clear()时;从preExecute方法来看,没有错误。我已经在上面的问题中添加了完整的错误日志。
postlist
是如何定义的?如何调用
AsyncTask
?这很有帮助,但问题是当我调用适配器.notifyDataSetChanged()时,我的listview会滚动到顶部。我想这是因为有一个时刻listview是空的,没有滚动条。你知道怎么解决这个问题吗?你是对的。为了防止出现这种情况,您可以将列表保留在此处而不清除它,并且只有在获得新项目时才能一起清除和更新新列表。如果我在获得新项目后清除postlist,我将删除新项目。。。在doInBackground方法中,我用新项目填充postList。在此之前,我必须清除postlist。您应该先使用另一个变量来存储该变量,仅在OnPostExecute中更新
postlist
。您的意思是创建另一个ArrayList postListForRefresh;并在此数组中添加新项。而不是在我的postExecute方法中执行此操作:postList.clear();对于(int i=0;i
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);
    }
}