停留在Android中实现分页分页、停留在、Android

2023-09-05 05:07:48 作者:杀戮非我意

我实现分页的ListView控件中的Andr​​oid。我扩展BaseAdapater类自定义的ListView。

I am implementing pagination for ListView in Android . I am extending the BaseAdapater class for customising the ListView.

我已经有code做工精细的定制的ListView。 下面是新的要求。

Already I have the code working fine for the Customised ListView. Below is the new requirement.

1>我取6项来自服务器和放大器;显示它们。现在,当用户滚动到第6项(名单月底),我需要调用服务器以获取下一个6个项目和放大器;更新列表视图

1>I am fetching 6 items from server & displaying them . Now when the user scrolls to the 6th item(end of list) , I need to call the server to fetch the next 6 items & update the Listview

我已经重写方法

我>公共无效onScroll(AbsListView观点,诠释firstVisibleItem,诠释visibleItemCount,INT totalItemCount)

i>public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount)

II>公共无效onScrollStateChanged(AbsListView观点,诠释scrollState)

ii>public void onScrollStateChanged(AbsListView view, int scrollState)

在来自服务器的第一个反应,我得到了从服务器和放大器的总页无;每次我调用服务器时间,我得到的当前页面的值。

In the first response from the server, I get the total no of pages from the server & for each time I call the server , I get the current page value .

请为我提供的步骤/样品$ C $如何检查列表和放大器的最后一个项目C;更新列表。在code应该是反复的,因为我可能需要调用多次和放大器;从服务器上获取。

Kindly provide me the steps/sample code on how to check the last item of the list & update the list . The code should be iterative since I may need to call multiple times & fetch from server.

亲切问候,

CB

推荐答案

为什么不只是下载所有的背景,而不是每次都会有一个请求干什么呢?我有一个应用程序,我曾经考虑过做你正在做同样的事情,但决定使用一个后台线程来做到这一点吧。

Why not just download it all in the background rather than doing it every time there is a request? I have an app where I had considered doing the same thing you are doing but decided to use a background thread to do it instead.

下面是如何使用线程的一个很好的例子

Here is a great example of how to use threads

http://mindtherobot.com/blog / 159 / Android系统的胆量,介绍到活套和 - 处理器/

如何实现它,我检索可先记录的总数,然后开始一个新的线程为每一个页记录我要找回。

How I implemented it was that I retrieve the total count of records available first and then start a new thread for each "page" of records I want to retrieve.

// Create and launch the download thread
    downloadThread = new DownloadThread(this);
    downloadThread.start();

    // Create the Handler. It will implicitly bind to the Looper
    // that is internally created for this thread (since it is the UI
    // thread)
    handler = new Handler();

    resultCount = getResultCount(search).trim();
    resultsCount = Integer.parseInt(resultCount);
    page = 1;
    if (resultsCount > 0) {
        downloadThread
                .enqueueDownload(new DownloadTask(page));
    }

我说我的方法,从上面的例子检索记录DownloadTask.java和运行调用它(),我增加了一个公共的方法来检索结果一旦

I added my method to retrieve the records to DownloadTask.java from the example above and call it from run() and I added another public method to retrieve the results once the

我修改DownloadThread.java使以任务同步调用(DownloadTask)调用我的方法DownloadTask >> task.run(),并在最后{}我检索返回结果

I modified DownloadThread.java so that the synchronized call to task (DownloadTask) calls my method in DownloadTask >> task.run() and in finally{} I retrieve the returned results

public synchronized void enqueueDownload(final DownloadTask task) {
    // Wrap DownloadTask into another Runnable to track the statistics
    handler.post(new Runnable() {
        @Override
        public void run() {
            try {
                task.run();
            } finally {                 
                // register task completion
                synchronized (DownloadThread.this) {
                    results = task.getResults();
                    totalCompleted++;
                }
                // tell the listener something has happened
                signalUpdate();
            }               
        }
    });

然后,我增加了一个新的方法来DownloadThread.java,让我以检索的更新通知的结果,我的活动

Then I added a new method to DownloadThread.java which allows me to retrive the results from the update notification in my activity

public synchronized ArrayList<Result> getResults() {
    return results;
}

例如,如果有60个记录,每个页面包含6的记录,我启动一个线程来检索第一6然后当handleDownloadThreadUpdate()被调用我填充这些结果的阵列和在我的适配器调用一个方法所谓的update()调用notifyDataSetChanged()。

For example, if there are 60 records and each page contains 6 records, I start a thread to retrieve the first 6 and then when the handleDownloadThreadUpdate() is called I populate the array with those results and call a method in my Adapter called update() that calls notifyDataSetChanged().

public void handleDownloadThreadUpdate() {
    handler.post(new Runnable() {
        @Override
        public void run() {
            for (Result res : downloadThread.getResults()) {
                if (!results.contains(res)) {
                    results.add(intern(res));
                }
            }
            arrayAdapter.update();
            int pages = (int) Math.ceil(resultsCount / 6);
            if (page < pages) {
                page = page + 1;
                downloadThread.enqueueDownload(new DownloadTask(page));
            }
        }
    });
}

所以,递归下载网页和结果添加到我的清单。

So it recursively downloads the pages and adds the results to my list.

还有一件事我忘了提及。我有一类称为结果,认为每一个我保存在一个ArrayList结果。万一结果尝试使用过多内存,我缓存他们在一个WeakReference的HashMap的......你可以看到调用实习生()时,它的加入到上面的ArrayList的。

One more thing I forgot to mention. I have a class called Result which holds each result that I store in an ArrayList. Just in case the results try to use too much memory I am caching them in a WeakReference HashMap... as you can see the call to intern() when it's added to the ArrayList above.

private Map<Result, WeakReference<Result>> myInternMap = new HashMap<Result, WeakReference<Result>>();

public Result intern(Result value) {
    synchronized (myInternMap) {
        WeakReference<Result> curRef = myInternMap.get(value);
        Result curValue = ((curRef != null) ? curRef.get() : null);
        if (curValue != null) {
            return curValue;
        }

        myInternMap.put(value, new WeakReference<Result>(value));
        return value;
    }
}