1

For eg:- if i load first 5 items in a list then on scrolling down load next five items

and as it is a endlessadapter this procedure must repeat endlessly this is how i tried -->foliowing is my demoadapter code:-

   public class MyDemoAdapter extends EndlessAdapter {

private static int cutting_int =5;
public static int batch_repeat = 0;
public int check_batch = 0;
private Context mcontxt;
private RotateAnimation rotate = null;
ArrayList<DownloadOffersClass> tempList = new ArrayList<DownloadOffersClass>();
private static int mLastOffset = 0;
private ArrayList<DownloadOffersClass> list = new ArrayList<DownloadOffersClass>();
private int LIST_SIZE;


public MyDemoAdapter(Context context, int textViewResourceId,
        ArrayList<DownloadOffersClass> list, int lIST_SIZE) {
    super(new DownloadedOffersAdapter(context,
            R.layout.offer_listview_item, list));
    this.mcontxt = context;
    this.list = list;
    this.LIST_SIZE = lIST_SIZE;
    rotate = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF,
            0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    rotate.setDuration(600);
    rotate.setRepeatMode(Animation.RESTART);
    rotate.setRepeatCount(Animation.INFINITE);
}

@Override
protected View getPendingView(ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) mcontxt
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View row = inflater.inflate(R.layout.row, null);

    View child = row.findViewById(android.R.id.text1);

    child.setVisibility(View.GONE);

    child = row.findViewById(R.id.throbber);
    child.setVisibility(View.VISIBLE);
    child.startAnimation(rotate);

    return (row);
}

@Override
protected boolean cacheInBackground() {
    SystemClock.sleep(2000);

    if (check_batch != batch_repeat) {
        tempList.clear();
        int lastOffset = getLastOffset();
        if (lastOffset < LIST_SIZE) {
            int limit = lastOffset + cutting_int;
            for (int i = (lastOffset + 1); (i <= limit && i < LIST_SIZE); i++) {
                tempList.add(list.get(i));
            }
            setLastOffset(limit);

            if (limit < 50) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    } else {
        return false;
    }
}

@Override
protected void appendCachedData() {
    if (getWrappedAdapter().getCount() < 50) {
        @SuppressWarnings("unchecked")
        DownloadedOffersAdapter a = (DownloadedOffersAdapter) getWrappedAdapter();

        check_batch = check_batch + 1;
        Log.v("Check", " " + check_batch);
        for (int i = cutting_int; i < cutting_int + cutting_int; i++) {
            a.add(list.get(i));
        }
    }
}

public static void setLastOffset(int i) {
    mLastOffset = i; 
}

private int getLastOffset() {
    return mLastOffset;
}

}

Sagar G.
  • 504
  • 7
  • 15

3 Answers3

3

Modify your cacheInBackground()

if (check_batch != batch_repeat) {
            tempList.clear();
            int lastOffset = getLastOffset();
            if (lastOffset < LIST_SIZE) {
                int limit = lastOffset + cutting_int;
                for (int i = (lastOffset + 1); (i <= limit && i <                                           LIST_SIZE); i++) {
                    tempList.add(list.get(i));
                }
                setLastOffset(limit);

                if (limit <= 50) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            if (!firstTry) {// use this boolean to do it only once
                tempList.clear();
                int notDivisible = LIST_SIZE % cutting_int;
                if (!(notDivisible == 0)) {
                    firstTry = true;
                    return true;
                } else {
                    return false;
                }
            }
            return false;
        }

And replace your appendCachedData() method with this:

  protected void appendCachedData() {

        DownloadedOffersAdapter a = (DownloadedOffersAdapter) getWrappedAdapter();
        if (check_batch != batch_repeat) {
            if (getWrappedAdapter().getCount() <= 50) {
                check_batch = check_batch + 1;
                Log.v("Check", "append chk_batch " + check_batch);
                for (int i = cutting_int; i < cutting_int + cutting_int; i++) {
                    a.add(list.get(i));
                }
            }
        } else {// Append extra entries to list i.e less than cutting_int
            int notDivisible = LIST_SIZE % cutting_int;
            for (int i = (LIST_SIZE - notDivisible); i < LIST_SIZE - 1; i++) {
                a.add(list.get(i));
            }
            return;
        }
    }
Sagar G.
  • 504
  • 7
  • 15
patric_cena
  • 1,986
  • 1
  • 12
  • 17
0

You will create the object for ImageLoader on your listadapter.

ImageLoader lazyload = new ImageLoader(context);

public ImageLoader(Context context) {
        FileCache fileCache = new FileCache(context);
        ExecutorService executorService = Executors.newFixedThreadPool(5);
    }

Then set images to your adapter getView() like this.

  ImageView image = (ImageView) view.findViewById(R.id.yourImage);

    lazyload.displayImage(image);

load your Images

public void displayImage(String url, ImageView imageView) {
    if (bitmap != null)
        imageView.setImageBitmap(bitmap);
vinoth
  • 225
  • 3
  • 9
  • thanks @vinoth but this only works if i have to load just images ,i am loading a custom row with imageviews,textviews,buttons etc.. – Sagar G. Apr 19 '13 at 12:09
0

For eg:- if i load first 5 items in a list then on scrolling down load next five items

You are responsible for populating the adapter with enough items to warrant scrolling, and 5 is unlikely to be enough.

You are responsible for determining how many additional items to load when you are told to go load additional data.

and as it is a endlessadapter this procedure must repeat endlessly

Or until you tell the EndlessAdapter that there is no more data, such as by returning false from cacheInBackground(). The idea here is that when you do the Web service call to get the next set of data, you should also determine whether you have reached the end of the available data.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • i call web service for data only once and receive the complete list in a seperate async task and pass this list to endless adapter,then it loads the complete list at one go, so i want it to load it in a batch of 5 items .how can i tell adapter to load only 5 items per batch. – Sagar G. Apr 19 '13 at 12:10
  • 1
    @SagarG.: "then it loads the complete list at one go, so i want it to load it in a batch of 5 items" -- that makes no sense to me whatsoever. If you have the data, please show it to the user. "how can i tell adapter to load only 5 items per batch" -- write a `cacheInBackground()` method that adds five at a time until you run out of data. – CommonsWare Apr 19 '13 at 12:19
  • ok thanks,i will see what i can do in cacheInBackground() method also do i need to change appendCachedData() method. – Sagar G. Apr 19 '13 at 12:29