`
sillycat
  • 浏览: 2551314 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Android Tutorial(2)Endless Pagination for ListView

 
阅读更多
Android Tutorial(2)Endless Pagination for ListView

We have 2 options to load more information in ListView.
1. Add a button or link to the footer of the view, when we press it the system will load more content.
2. Check if the last item is visible and load more content when it is.

First of all, I plan to Viewing Android Source Code in Eclipse. That will help me a lot to study the codes.

1. Get the Android Source Code Viewing
After API Level 14, we have this 'Sources for Android SDK'. But for the previous versions, we need to install a plugin named "Android Sources".
https://code.google.com/p/adt-addons/

I install the plugin with URL
http://adt-addons.googlecode.com/svn/trunk/installer/com.android.ide.eclipse.installer.update/

But got Error Message:
Cannot complete the install because one or more required items could not be found.
Software being installed: Android SDK Installer 0.9.5.201012121703 (com.android.ide.eclipse.installer.feature.feature.group 0.9.5.201012121703)
Missing requirement: Android SDK Installer 0.9.5.201012121703 (com.android.ide.eclipse.installer.feature.feature.group 0.9.5.201012121703) requires 'org.eclipse.gef.all.feature.group 0.0.0' but it could not be found

Solution:
Change to root user and use the latest link:
http://adt-addons.googlecode.com/svn/trunk/source/com.android.ide.eclipse.source.update/

After that I got the source codes of android.

But I need to just use the latest package, do not use root user. That will cause problem if I build from maven. NO root User.

2. Make the Pagination ListView

Some times I got this Error Message when I start my emulator from snapshot.
Starting emulator for AVD 'CARL'
emulator: ERROR: Unable to load VM from snapshot. The snapshot has been saved for a different hardware configuration.

Solution:
Android Virtual Device Manager -> delete AVD -> new AVD, that solved the problem.

Change the adapter not extends from BaseAdapter, but from ArrayAdapter.

package com.sillycat.easyrestclientandroid.adapter;

import java.util.List;

import android.content.Context;
import android.widget.ArrayAdapter;

public abstract class AbstractBaseItemListAdapter<T> extends ArrayAdapter<T> {

     public AbstractBaseItemListAdapter(Context context, int textViewResourceId,
               List<T> objects) {
          super(context, textViewResourceId, objects);
     }

}

Almost no changes in the Adapter implementation.
public class ProductsListAdapter extends AbstractBaseItemListAdapter<Product> {


     privatefinal LayoutInflater _layoutInflater;


     public ProductsListAdapter(Context context, int textViewResourceId,List<Product> objects) {
          super(context, textViewResourceId, objects);
          this._layoutInflater = LayoutInflater.from(context);
     }
…snip…


Most of the changes will happen in the activity.
package com.sillycat.easyrestclientandroid.activity.impl;

import java.util.ArrayList;
import java.util.List;

import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;

import com.sillycat.easyrestclientandroid.activity.AbstractAsyncListActivity;
import com.sillycat.easyrestclientandroid.adapter.impl.ProductsListAdapter;
import com.sillycat.easyrestclientandroid.dao.ProductDAO;
import com.sillycat.easyrestclientandroid.dao.mock.ProductMockDAOImpl;
import com.sillycat.easyrestclientandroid.model.Product;

public class ProductsListActivity extends AbstractAsyncListActivity {

     protected static final String TAG = ProductsListActivity.class
               .getSimpleName();

     int pageSize = 5;

     int currentPage = 1;

     boolean loadingMore = false;

     ProductsListAdapter adapter;

     List<Product> items;

     public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);

          items = new ArrayList<Product>();

          adapter = new ProductsListAdapter(this,
                    android.R.layout.simple_list_item_1, items);

          setListAdapter(adapter);

     }

     public void onStart() {
          super.onStart();
          new DownloadStatesTask().execute(currentPage);
     }

     public void refreshStates(List<Product> items) {

          if (items == null || items.isEmpty()) {
               return;
          }

          for (int i = 0; i < items.size(); i++) {
               adapter.add(items.get(i));
          }
          setTitle("Products List with " + String.valueOf(adapter.getCount())
                    + " items");

          adapter.notifyDataSetChanged();

          this.getListView().setOnScrollListener(new OnScrollListener() {

               public void onScrollStateChanged(AbsListView view, int scrollState) {
               }

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

                    int lastInScreen = firstVisibleItem + visibleItemCount;

                    Log.d(TAG, "firstVisibleItem = " + firstVisibleItem + " visibleItemCount = " + visibleItemCount + " totalItemCount = " + totalItemCount);
                    if ((lastInScreen == totalItemCount) && !(loadingMore)) {
                         currentPage = currentPage + 1;
                         new DownloadStatesTask().execute(currentPage);
                    }
               }
          });
          loadingMore = false;
     }

     private class DownloadStatesTask extends
               AsyncTask<Integer, Void, List<Product>> {
          @Override
          protected void onPreExecute() {
               loadingMore = true;
               showLoadingProgressDialog();
          }

          @Override
          protected List<Product> doInBackground(Integer... params) {
               try {
                    Log.d(TAG, "Hitting the current page params = " + params[0]);
                    ProductDAO dao = new ProductMockDAOImpl();
                    try {
                         Thread.sleep(2000);
                    } catch (InterruptedException e) {
                    }
                    return dao.pagination(params[0], pageSize);
               } catch (Exception e) {
                    Log.e(TAG, e.getMessage(), e);
               }
               return null;
          }

          @Override
          protected void onPostExecute(List<Product> result) {
               dismissProgressDialog();
               refreshStates(result);
          }
     }

}


References:
http://p-xr.com/android-tutorial-dynamicaly-load-more-items-to-the-listview-never-ending-list/

Source Plugin
http://manski.net/2011/11/android-source-code-in-eclipse/
http://adt-addons.googlecode.com/svn/trunk/source/com.android.ide.eclipse.source.update/

Customer ArrayAdatper
http://www.ezzylearning.com/tutorial.aspx?tid=1763429
http://devtut.wordpress.com/2011/06/09/custom-arrayadapter-for-a-listview-android/
http://sogacity.com/how-to-make-a-custom-arrayadapter-for-listview/
分享到:
评论

相关推荐

    OpenGL ES Tutorial for Android.zip

    OpenGL ES Tutorial for Android – Part I – Setting up the view OpenGL ES Tutorial for Android – Part II – Building a polygon OpenGL ES Tutorial for Android – Part III – Transformations OpenGL ES ...

    Sigrity-T2B Tutorial for Spectre Models.rar

    Sigrity-T2B Tutorial for Spectre Models.rar 信号晶体管到行为模型的转换 概述 信号晶体管到行为(T2B)模型转换是一种将IO晶体管模型转换为IBIS模型的工具。部分算法利用了北卡罗来纳州立大学(NCSU)开发的S2...

    Android Development Tutorial

    2. Android项目组件及示例应用: - 创建一个空的Android“Hello World”项目。 - 理解Android项目的组成部分,包括布局文件、Java类、资源文件等。 - 示例项目,如带标签页的应用程序,帮助开发者学习如何组织和...

    Android代码-Android

    Android Tutorial1 Android Tutorial2 官网中文教程 Design framework 2016.04.15 navigate draw Task1 2016.04.17 Task2 2016.04.19 Task3 2016.04.20 Figure out hybrid ARQ strategy Task4 2016.04.21...

    Tutorial on OpenCV for Android Setup

    ### OpenCV for Android安装教程详解 #### 引言 随着移动设备处理能力的不断增强,越来越多的应用开始集成图像处理和计算机视觉功能。OpenCV(开源计算机视觉库)作为一款强大的计算机视觉库,在Android平台上也...

    roman10-android-tutorial

    "roman10-android-tutorial" 是一个专门为Android初学者准备的教程项目,它包含了完整的源代码,可以帮助开发者深入了解Android应用开发的基本概念和技术。这个教程可能包括了从搭建环境到实现功能的各种实例,覆盖...

    android tutorial hello world

    这是我自己学习android的作品. 共享一下, 需要的朋友可拿去参考一下. Android中的tutorials比较详细,但是没有提供实例. 如果操作中有什么地方搞错了,比较难查问题,特别是初学者. 我提供这个下载也是方便大家在学习时...

    android tutorial

    **2. 应用程序框架** Android应用程序框架提供了一系列服务和接口,使得开发者能够构建复杂的交互式应用。Activity、Service、BroadcastReceiver、ContentProvider和Intent是其核心组件。学习如何灵活运用它们,可以...

    FLUENT 2020R2 tutorial guide PDF及案例源文件

    FLUENT 2020R2 tutorial guide PDF及案例源文件 1.What’s In This Manual The ANSYS Fluent Tutorial Guide contains a number of tutorials that teach you how to use ANSYS Flu- ent to solve different types ...

    Docker Docker Tutorial for Beginners Build Ship and Run azw3

    Docker Docker Tutorial for Beginners Build Ship and Run 英文azw3 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书

    tutorial_cut down for running.R

    tutorial_cut down for running.R

    android_ffmpeg_tutorial01

    本教程"android_ffmpeg_tutorial01"旨在教你如何将FFmpeg移植到Android应用中,并实现基本的图片显示功能。 首先,我们需要了解如何在Android Studio项目中集成FFmpeg。这通常涉及以下几个步骤: 1. **添加FFmpeg...

    Android代码-android-unit-testing-tutorial

    Code project corresponding to a serials of tutorial posts for android unit testing 本文是一系列发表在我的个人博客,关于安卓单元测试的文章的实例代码。 代码与文章的对应关系如下: Android单元测试: 首先,...

    Android sensors tutorial by touchquode

    首先,文件标题“Android sensors tutorial by touchquode”表明该文档是一份由***提供的Android传感器教程。这个教程很可能是介绍Android系统中如何使用各种传感器的应用和编程方法。同时,文件描述中的“***”...

    63.[开源][安卓]roman10-android-tutorial-master

    63.[开源][安卓]roman10-android-tutorial-master roman10-android-tutorial包含了android tutorial的所有源代码。

    tutorial-android

    在Android开发领域,"tutorial-android"很可能是一个针对初学者的教程集合,旨在帮助新手逐步掌握Android应用开发的基础知识。这个教程可能涵盖了从安装开发环境到编写第一个应用的全过程,包括了各种关键概念、工具...

    Webdynpro for ABAP tutorial 7

    Webdynpro for ABAP tutorial

    AndroidTutorialOosePdfFreeDownload.pdf 英文原版

    Android Tutorial Oose, Pdf Free Download

Global site tag (gtag.js) - Google Analytics