1

I have to apply pagination concept on ListView my list view contains data parsed from web service. below is code given that how I have displayed data in list view as below.

try {
    ArrayList<HashMap<String, String>> arl (ArrayList<HashMap<String, String>>)  getIntent().getSerializableExtra("arraylist");

      System.out.println("...serialized data.."+arl);
lv1 = (ListView) findViewById(R.id.lstlodgingresult);
    adapter = new SimpleAdapter(this, arl, R.layout.custom_row_view,
                    new String[] { "Srno", "Names", "URL", "Address1",  "Address2", "Telephone", "Category", "PetH",
"PetInfo" }, new int[] { R.id.txtSrno,R.id.txtname, R.id.txturl, R.id.txtaddress1,  R.id.txtaddress2, R.id.txtphone, R.id.txtcategory,
R.id.txtpetpolicyH, R.id.txtpetpolicyC }
);
lv1.setScrollbarFadingEnabled(false);
lv1.refreshDrawableState();
lv1.setAdapter(adapter);
} catch (Exception e) {
e.printStackTrace();
}
Hussain
  • 5,552
  • 4
  • 40
  • 50
sandy
  • 3,311
  • 4
  • 36
  • 47
  • can you give more clear idea what is problem you are facing right now in above code? – Android Jun 24 '11 at 09:42
  • Hello Nallath, Pagination is the concept where you can show your result by page wise. Above code is working. But it fills the ListView with all the values at starting what I want is ListView should show 10 records by default and when user scrolls down up to last record then it will fetch next 10 records. It should work in the similar manner. – sandy Jun 27 '11 at 10:21

1 Answers1

2

you just need to add a Footer View in the Listyou created. Then for the footer view (might be button/image/text) set a ClickListener for that and in Listener add the items into your list and again refresh the activity. I am adding a little tutorial that will help you in this.

I used the following Method for Pagination:

The List Class:

public class customListView extends Activity implements OnClickListener{

  private static class EfficientAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    Context context;
    public EfficientAdapter(Context context) {
        this.context = context;
        mInflater = LayoutInflater.from(context);

    }

    public int getCount() {
        return add_Names.size();
    }

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
    convertView = mInflater.inflate(R.layout.listcontent, null);
    holder = new ViewHolder();
    holder.text = (TextView) convertView
    .findViewById(R.id.txt1);
    holder.text2 = (TextView) convertView
    .findViewById(R.id.txt2);
    holder.text3 = (TextView) convertView
    .findViewById(R.id.txt3);



    convertView.setTag(holder);
    } else {
    holder = (ViewHolder) convertView.getTag();
    }

    holder.text.setText(add_Names.get(position).toString());
    holder.text2.setText(location.get(position).toString());
    holder.text3.setText(details.get(position).toString());

    return convertView;
    }

    static class ViewHolder {
    TextView text;
    TextView text2;
    TextView text3;
    }
    }//end of efficient Adapter Class

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview);
      adapter = new EfficientAdapter(this);

    l1 = (ListView) findViewById(R.id.ListView01);
    View footerView = 
        ((LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.layout_footer, null, false);


    l1.addFooterView(footerView);



    l1.setAdapter(adapter);
    mLayout = (LinearLayout) footerView.findViewById(R.id.footer_layout);
    more = (Button) footerView.findViewById(R.id.moreButton);
    more.setOnClickListener(this);

    l1.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
        Toast.makeText(getBaseContext(), "You clciked "+add_Names.get(arg2).toString(), Toast.LENGTH_LONG).show();

        }
    });            
    }

 @Override
    public void onClick(View v) {
        switch(v.getId())
        {
        case R.id.moreButton:
            //Your code to add some more data into list and then call the following to refresh your lits
       adapter.notifyDataSetChanged();
            break;
      }//end of switch
 }//end of onClick


}//end of Custom List view class

layout_footerview.xml:(you can add whatever you link in the footer for the list. I used button you can use Text or image or whatever you want)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
 android:paddingTop="7dip"
 android:paddingBottom="7dip"
 android:orientation="horizontal"
 android:gravity="center">
<LinearLayout 
android:id="@+id/footer_layout" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_gravity="center">

<Button
    android:text="Get more.." 
    android:id="@+id/moreButton" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textSize="14dip" 
    android:textStyle="bold">
</Button> 

 </LinearLayout>
 </LinearLayout> 

listview.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="fill_parent"
  android:layout_height="fill_parent">

 <ListView android:id="@+id/ListView01" android:layout_height="wrap_content" 
      android:layout_width="fill_parent">
 </ListView>
</RelativeLayout>

list-content.xml:(modify as u like to be your list row)

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent" android:layout_height="wrap_content"
  android:orientation="horizontal">

  <ImageView android:id="@+id/image1" android:layout_width="wrap_content"         android:layout_height="wrap_content"
android:src="@drawable/icon"></ImageView>

  <TextView android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:id="@+id/txt1"     android:layout_toRightOf="@+id/image1"
    android:text="Test Description" android:textSize="15dip"  android:textStyle="bold">     
 </TextView>

<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/txt2"  android:layout_below="@+id/txt1" android:layout_toRightOf="@+id/image1"
android:text="Address" android:textSize="10dip"></TextView>

<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/txt3" android:layout_below="@+id/txt2" android:layout_toRightOf="@+id/image1"
android:text="Details" android:textSize="10dip" ></TextView>

   </RelativeLayout>    

I Hop this will definetly help you.!

Mark this as true and UpVote; if this helps you.

Thanks sHaH..

Shah
  • 4,990
  • 10
  • 48
  • 70