gpt4 book ai didi

android - Android中ListView的分页

转载 作者:行者123 更新时间:2023-11-29 22:23:58 25 4
gpt4 key购买 nike

我必须在 ListView 上应用分页概念,我的 ListView 包含从 Web 服务解析的数据。下面是代码,说明我如何在 ListView 中显示数据,如下所示。

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

最佳答案

您只需要在您创建的列表中添加一个页脚 View 。然后为页脚 View (可能是按钮/图像/文本)设置一个 ClickListener 并在 Listener 中将项目添加到您的列表中并再次刷新 Activity 。我正在添加一个小教程来帮助您。

我使用了以下分页方法:

列表类:

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>

我希望这一定能帮到你!

将其标记为真并投赞成票;如果这对您有帮助。

谢谢沙哈..

关于android - Android中ListView的分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6465978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com