Android分页机制句柄

时间:2013-11-24 12:14:37

标签: java android android-asynctask

如何处理分页机制?

我创建了一个用于创建数据分页的代码。但是当我指定一个已过期的限制时,应用程序将被损坏并强行关闭。

我的paging class

public class SetPaging {
    public static final String[] PAGING = new String[] {
        "0,5","5,5","10,5","15,5","20,5"
    };
}

我的listener

pullRefresh = ((PullAndLoadListView) listView);
    pullRefresh.setOnLoadMoreListener(new OnLoadMoreListener() {

    public void onLoadMore() {
        new LoadMoreData().execute();
    }
});

我的async code

private class LoadMoreData extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            page = page + 1;
            if (page > SetPaging.PAGING.length) {
                page = 0;
            }
        }

        protected Void doInBackground(Void... params) {

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }

            try {
                ArrayList<UtamaBean> NextUtama = new ArrayList<UtamaBean>();
                NextUtama = PushServer.getNextUtama(SetPaging.PAGING[page]);
                if (page > 0) {
                    utamabean.addAll(NextUtama);
                } else {
                    utamabean = PushServer.getNextUtama(SetPaging.PAGING[page]);
                }
            } catch (Exception e) {
                netError.setVisibility(View.VISIBLE);
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);

            listView.setVisibility(View.VISIBLE);
            adapter.setItem(utamabean);

            pullRefresh.onLoadMoreComplete();
        }

        @Override
        protected void onCancelled() {
            pullRefresh.onLoadMoreComplete();
        }
    }
一切顺利。只有当第六个分页应用程序(规则“20,5”之外)时,应用程序才会立即强行关闭。

如果完成分页,如何最好地执行第六个限制,应用程序将不会进入异步功能并请求数据?

由于

=== UPDATE ===

我的LogCat:

11-24 19:59:40.587: E/AndroidRuntime(16223): FATAL EXCEPTION: AsyncTask #1
11-24 19:59:40.587: E/AndroidRuntime(16223): java.lang.RuntimeException: An error occured while executing doInBackground()
11-24 19:59:40.587: E/AndroidRuntime(16223):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
11-24 19:59:40.587: E/AndroidRuntime(16223):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
11-24 19:59:40.587: E/AndroidRuntime(16223):    at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
11-24 19:59:40.587: E/AndroidRuntime(16223):    at java.util.concurrent.FutureTask.run(FutureTask.java:239)
11-24 19:59:40.587: E/AndroidRuntime(16223):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-24 19:59:40.587: E/AndroidRuntime(16223):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
11-24 19:59:40.587: E/AndroidRuntime(16223):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
11-24 19:59:40.587: E/AndroidRuntime(16223):    at java.lang.Thread.run(Thread.java:841)
11-24 19:59:40.587: E/AndroidRuntime(16223): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=5; index=5
11-24 19:59:40.587: E/AndroidRuntime(16223):    at com.joris.babe.F_Test$LoadMoreData.doInBackground(F_Test.java:156)
11-24 19:59:40.587: E/AndroidRuntime(16223):    at com.joris.babe.F_Test$LoadMoreData.doInBackground(F_Test.java:1)
11-24 19:59:40.587: E/AndroidRuntime(16223):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-24 19:59:40.587: E/AndroidRuntime(16223):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
11-24 19:59:40.587: E/AndroidRuntime(16223):    ... 4 more

1 个答案:

答案 0 :(得分:0)

你有这个

netError.setVisibility(View.VISIBLE);

在后台线程上调用从doInbackground更新ui。doInbackground

因此例外。在ui线程上更新ui。在doInbackground中返回一个值。根据返回的值,可以在onPostExecute中设置可见性。