android中ListView的分页
我必须在ListView上应用分页概念。我的列表视图包含从web服务解析的数据。下面是我如何在列表视图中显示数据的代码,如下所示android中ListView的分页,android,web-services,listview,Android,Web Services,Listview,我必须在ListView上应用分页概念。我的列表视图包含从web服务解析的数据。下面是我如何在列表视图中显示数据的代码,如下所示 try { ArrayList<HashMap<String, String>> arl (ArrayList<HashMap<String, String>>) getIntent().getSerializableExtra("arraylist"); System.out.println("
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();
}
试试看{
ArrayList arl(ArrayList)getIntent().getSerializableExtra(“ArrayList”);
System.out.println(“…序列化数据..”+arl);
lv1=(ListView)findViewById(R.id.lstLodingResult);
适配器=新的SimpleAdapter(此、arl、R.layout.custom\u行\u视图,
新字符串[]{“Srno”,“name”,“URL”,“Address1”,“Address2”,“Telephone”,“Category”,“PetH”,
“PetInfo”},新的int[]{R.id.txtSrno,R.id.txtname,R.id.txtrl,R.id.txtdress1,R.id.txtdress2,R.id.txtphone,R.id.txtcategory,
R.id.txtpetpolicy,R.id.txtpetpolicy}
);
1级。设置CrollBarFadinEnabled(假);
lv1.refreshDrawableState();
lv1.设置适配器(适配器);
}捕获(例外e){
e、 printStackTrace();
}
您只需在创建的列表中添加页脚视图。然后为页脚视图(可能是按钮/图像/文本)设置一个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
公共类customListView扩展活动实现OnClickListener{
私有静态类EfficientAdapter扩展BaseAdapter{
私人停车场;
语境;
公共效率适配器(上下文){
this.context=上下文;
mInflater=LayoutInflater.from(上下文);
}
public int getCount(){
返回add_Names.size();
}
公共对象getItem(int位置){
返回位置;
}
公共长getItemId(int位置){
返回位置;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
视窗座;
if(convertView==null){
convertView=mInflater.inflate(R.layout.listcontent,null);
holder=新的ViewHolder();
holder.text=(TextView)convertView
.findviewbyd(R.id.txt1);
holder.text2=(TextView)convertView
.findviewbyd(R.id.txt2);
holder.text3=(TextView)convertView
.findviewbyd(R.id.txt3);
convertView.setTag(支架);
}否则{
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());
返回视图;
}
静态类视窗夹{
文本查看文本;
TextView text2;
TextView text3;
}
}//高效适配器类的结束
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
适配器=新的效率适配器(本);
l1=(ListView)findViewById(R.id.ListView01);
视图页脚视图=
((LayoutInflater)this.getSystemService(Context.LAYOUT\u INFLATER\u SERVICE)).inflate(R.LAYOUT.LAYOUT\u footer,null,false);
l1.添加页脚视图(页脚视图);
l1.设置适配器(适配器);
mLayout=(LinearLayout)footerView.findViewById(R.id.footer\u布局);
more=(按钮)footerView.findViewById(R.id.moreButton);
setOnClickListener(这个);
l1.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共链接(AdapterView arg0、视图arg1、内部arg2、,
长arg3){
Toast.makeText(getBaseContext(),“youclciked”+add_Names.get(arg2).toString(),Toast.LENGTH_LONG.show();
}
});
}
@凌驾
公共void onClick(视图v){
开关(v.getId())
{
案例R.id.moreButton:
//您的代码将向列表中添加更多数据,然后调用以下命令刷新您的LIT
adapter.notifyDataSetChanged();
打破
}//开关末端
}//onClick结束
}//自定义列表视图类的结束
布局\u footerview.xml:(您可以在列表的页脚中添加任何链接。我使用了按钮,您可以使用文本、图像或任何您想要的内容)
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>
列表内容.xml:(根据您的列表行进行修改)
我希望这对你绝对有帮助
将此标记为真实并向上投票;如果这对你有帮助的话
谢谢
sHaH..您能否在上面的代码中更清楚地说明您目前面临的问题是什么?您好,Nallath,分页是一种可以按页面显示结果的概念。以上代码正在运行。但它在开始时用所有值填充ListView,我希望ListView默认显示10条记录,当用户向下滚动到最后一条记录时,它将获取下一条10条记录。它应该以类似的方式工作。hello sHaH,当我试图运行此代码时,它只在ListView中显示GetMore按钮。这是我的荣幸。。。很高兴我能帮忙。:)
<?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>
<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>