回答
你也可以試試這個, 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. 如何設置分頁計時器php
- 7. 如何在ExtJS 4分頁中設置總頁數?
- 8. Android設置頁面
- 9. 如何在android中設置頁眉和頁腳?
- 10. 如何在asp.net中的DataList控件中設置分頁?
- 11. Android中如何設置libsvm?
- 12. 在Cakephp中設置Ajax分頁未設置頁面
- 13. 如何在Android的設置頁腳的Android
- 14. cakephp分頁:如何設置不同的'頁面'參數
- 15. 如何在wordpress中設置兩個循環的常用分頁?
- 16. 如何設置Magento中特定類別的分頁範圍?
- 17. 如何在android中設置標籤主機的默認頁面?
- 18. 如何在android中設置DatePickerDialog的頁眉佈局
- 19. jquery分頁設置每頁的記錄
- 20. 如何在CakePHP中設置控制器中的分頁器頁面
- 21. Android,如何在relativelayout中設置帶有百分比的imageview
- 22. 如何設置的android
- 23. 如何設置Android的MockContentProvider
- 24. 如何設置的Android
- 25. 如何設置的android
- 26. 如何設置的android
- 27. 如何設置api在android中登錄30分鐘後命中
- 28. 如何在Android的AlertDialog中設置Edittext
- 29. 如何設置android中的圖像
- 30. 如何設置android中tabview的顏色?
此[教程](http://blogs.technicise.com/android-listview-pagination/)可以幫助您。 –
感謝回覆,但它不工作......我是初學者,所以有沒有其他簡單的方法......?和是否有可能在XML文件本身分頁...? –