在Android中的ListView分页

时间:2011-06-24 09:34:13

标签: android web-services listview

我必须在ListView上应用分页概念,我的列表视图包含从Web服务解析的数据。下面给出了我如何在列表视图中显示数据的代码,如下所示。

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();
}

1 个答案:

答案 0 :(得分:2)

您只需在创建的Listyou中添加页脚视图。然后,对于页脚视图(可能是按钮/图像/文本),为其设置ClickListener,并在Listener中将项添加到列表中,然后再次刷新活动。我正在添加一个可以帮助你的小教程。

我使用以下方法进行分页:

列表类:

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:(您可以在列表的页脚中添加您链接的内容。我使用了按钮,您可以使用文字或图片或任何您想要的内容)

<?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:(修改为你喜欢的列表行)

 <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>    

我跳这将肯定会帮助你。!

将此标记为true和UpVote;如果这对你有所帮助。

由于 沙..