回答
你也可以试试这个, MainActivity.Java
public class MainActivity extends Activity {
private ListView listview;
private TextView title;
private ArrayList<String> data;
ArrayAdapter<String> sd;
public int TOTAL_LIST_ITEMS = 1030;
public int NUM_ITEMS_PAGE = 100;
private int noOfBtns;
private Button[] btns;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView)findViewById(R.id.list);
title = (TextView)findViewById(R.id.title);
Btnfooter();
data = new ArrayList<String>();
/**
* The ArrayList data contains all the list items
*/
for(int i=0;i<TOTAL_LIST_ITEMS;i++)
{
data.add("This is Item "+(i+1));
}
loadList(0);
CheckBtnBackGroud(0);
}
private void Btnfooter()
{
int val = TOTAL_LIST_ITEMS%NUM_ITEMS_PAGE;
val = val==0?0:1;
noOfBtns=TOTAL_LIST_ITEMS/NUM_ITEMS_PAGE+val;
LinearLayout ll = (LinearLayout)findViewById(R.id.btnLay);
btns =new Button[noOfBtns];
for(int i=0;i<noOfBtns;i++)
{
btns[i] = new Button(this);
btns[i].setBackgroundColor(getResources().getColor(android.R.color.transparent));
btns[i].setText(""+(i+1));
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ll.addView(btns[i], lp);
final int j = i;
btns[j].setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
loadList(j);
CheckBtnBackGroud(j);
}
});
}
}
/**
* Method for Checking Button Backgrounds
*/
private void CheckBtnBackGroud(int index)
{
title.setText("Page "+(index+1)+" of "+noOfBtns);
for(int i=0;i<noOfBtns;i++)
{
if(i==index)
{
btns[index].setBackgroundDrawable(getResources().getDrawable(R.drawable.box_green));
btns[i].setTextColor(getResources().getColor(android.R.color.white));
}
else
{
btns[i].setBackgroundColor(getResources().getColor(android.R.color.transparent));
btns[i].setTextColor(getResources().getColor(android.R.color.black));
}
}
}
/**
* Method for loading data in listview
* @param number
*/
private void loadList(int number)
{
ArrayList<String> sort = new ArrayList<String>();
int start = number * NUM_ITEMS_PAGE;
for(int i=start;i<(start)+NUM_ITEMS_PAGE;i++)
{
if(i<data.size())
{
sort.add(data.get(i));
}
else
{
break;
}
}
sd = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
sort);
listview.setAdapter(sd);
}
}
XML布局为MainActivity.java
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.mycompany.databaseconnectoin.MainActivity">
<ListView
android:layout_width="match_parent"
android:id="@+id/listView"
android:layout_height="match_parent"
android:layout_below="@+id/horizontalScrollView">
</ListView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/title"
android:text="ddfdf"
/>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_below="@+id/title"
android:layout_height="wrap_content"
android:id="@+id/horizontalScrollView">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/btnLay"
android:orientation="horizontal" >
<Button
android:id="@+id/prev"
android:text="Prev"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/next"
android:text="Next"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
/>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
运行这段代码,你会得到你的结果。现在根据您的需求进行更改。 另见此仅适用于两个按钮,如 Next & Previous
我得到运行时错误 –
发布logcat检查.. –
我在这一行中得到错误LinearLayout ll =(LinearLayout)findViewById(R.id .btnLay);错误:btnLay无法解析或不是字段 –
You can create logic for that suppose you want to fetch 10 records from the database on single button click . please check below code
package com.example.editdemo;
import java.util.ArrayList;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.InputFilter;
import android.text.Spanned;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
private InputFilter filter;
Button prevButton, nextButton, bt1, bt2, bt3, bt4, bt5, bt6, bt7, bt8, bt9,
bt10;
int cureentclcik;
ArrayList<String> data = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prevButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
cureentclcik = cureentclcik - 1;
int lastvalue = cureentclcik * 10;
int statvalue = lastvalue - 10;
for (int i = statvalue; i < lastvalue; i++) {
// get value heree from your arraylist
String value = data.get(i);
}
}
});
nextButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
cureentclcik = cureentclcik + 1;
int lastvalue = cureentclcik * 10;
int statvalue = lastvalue - 10;
for (int i = statvalue; i < lastvalue; i++) {
// get value heree from your arraylist
String value = data.get(i);
}
}
});
bt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
cureentclcik = Integer.parseInt(bt1.getText().toString());
int lastvalue = cureentclcik * 10;
int statvalue = lastvalue - 10;
for (int i = statvalue; i < lastvalue; i++) {
// get value heree from your arraylist
String value = data.get(i);
}
}
});
bt2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
cureentclcik = Integer.parseInt(bt2.getText().toString());
int lastvalue = cureentclcik * 10;
int statvalue = lastvalue - 10;
for (int i = statvalue; i < lastvalue; i++) {
// get value heree from your arraylist
String value = data.get(i);
}
}
});
}
}
希望这会帮助你
:谢谢我让你知道一旦我完成任务.. –
好的确定好友 – sukhbir
我仍然怀疑你的代码是否超过10在这种情况下如何做到这一点......例如:如果我有100编号和每页我正在显示5条记录是否需要添加btn11,btn12 ..?因为我的数据库每月更新两次,它真的很难像这样添加...这是可能的默认添加数字按钮,所以当我点击下一步它会自动去btn11到btn20 ..? –
- 1. 如何在android中设置视图分页器的选项卡?
- 2. 如何在PHP中设置分页?
- 3. 设置分页
- 4. 如何在WordPress中的category.php页面设置分页。
- 5. 如何在div底部设置分页?
- 6. Android设置页面
- 7. 如何在android中设置页眉和页脚?
- 8. 如何在ExtJS 4分页中设置总页数?
- 9. 如何在asp.net中的DataList控件中设置分页?
- 10. 如何在Android的设置页脚的Android
- 11. 如何在android中设置DatePickerDialog的页眉布局
- 12. 如何设置Magento中特定类别的分页范围?
- 13. jquery分页设置每页的记录
- 14. 如何在CakePHP中设置控制器中的分页器页面
- 15. 如何设置的android
- 16. 如何设置的android
- 17. 如何设置的android
- 18. 如何设置api在android中登录30分钟后命中
- 19. 如何设置android中的图像
- 20. 如何设置android中tabview的颜色?
- 21. 设置分页打印机
- 22. RDL设置分页符
- 23. Kohana 3分页,如何设置最大页面链接限制?
- 24. 如何在Android中设置网页图片按钮资源?
- 25. 如何在自定义视图组中设置页边距android
- 26. Android:如何在导航抽屉中实现设置页面?
- 27. 如何设置时间在android中加载插页式广告?
- 28. 如何在android中设置页眉和页脚的线性布局
- 29. cakephp从列表中分页设置condistion
- 30. 如何在android中设置不同的区域设置?
此[教程](http://blogs.technicise.com/android-listview-pagination/)可以帮助您。 –
感谢回复,但它不工作......我是初学者,所以有没有其他简单的方法......?和是否有可能在XML文件本身分页...? –