0

I am having listview items more than 500 products. I need to retrieve limit of data 50 . Whenever I scroll,list has to be loaded to next 50 items.

Your answer is more appreciated. Thanks in advance.

1

1 Answer 1

5

Try this code :-

public class EndlessListView extends ListActivity implements AbsListView.OnScrollListener {

EndlessAdapter adapter = new EndlessAdapter();

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setListAdapter(adapter);
    getListView().setOnScrollListener(this);
}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {

}

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

    boolean loadMore = firstVisibleItem + visibleItemCount >= totalItemCount;

    if (loadMore) {
        adapter.count += visibleItemCount; // or any other amount
        adapter.notifyDataSetChanged();
    }
}


class EndlessAdapter extends BaseAdapter {
    int count = 50; /* starting amount */

    public int getCount() {
        return count;
    }

    public Object getItem(int pos) {
        return pos;
    }

    public long getItemId(int pos) {
        return pos;
    }

    public View getView(int pos, View v, ViewGroup p) {
        TextView view = new TextView(EndlessListView.this);
        view.setText(String.format("entry %d", pos));
        return view;
    }
}
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.