`
sillycat
  • 浏览: 2490506 次
  • 性别: 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/
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics