2016-04-28 95 views
1

我从数据库带来的记录,我需要分页因为有更多的记录数..如何设置分页中的Android

enter image description here

等待Response.Thanks提前

+0

此[教程](http://blogs.technicise.com/android-listview-pagination/)可以帮助您。 –

+0

感谢回复,但它不工作......我是初学者,所以有没有其他简单的方法......?和是否有可能在XML文件本身分页...? –

回答

0

你也可以试试这个, 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

+0

我得到运行时错误 –

+0

发布logcat检查.. –

+0

我在这一行中得到错误LinearLayout ll =(LinearLayout)findViewById(R.id .btnLay);错误:btnLay无法解析或不是字段 –

0

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

} 

} 

希望这会帮助你

+0

:谢谢我让你知道一旦我完成任务.. –

+0

好的确定好友 – sukhbir

+0

我仍然怀疑你的代码是否超过10在这种情况下如何做到这一点......例如:如果我有100编号和每页我正在显示5条记录是否需要添加btn11,btn12 ..?因为我的数据库每月更新两次,它真的很难像这样添加...这是可能的默认添加数字按钮,所以当我点击下一步它会自动去btn11到btn20 ..? –