安卓分页显示数据(上一页和下一页)

先说一下分页,大部分都是滚动加载,而有上一页下一页效果的,网上很多都是同一个例子,就是data是一个String型的数组,在其最重要的getView()方法中,写得很让人看不懂,自己又参考了其它的例子,终于明白了,于是就有了以下的代码:

DsznzActivity代码:

[java] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. public class DsznzActivity extends Activity {  
  2.       
  3.     private ArrayList<HashMap<String, String>> listItem;  
  4.     private ListView list_ylfn;  
  5.       
  6.         Button btnPre, btnNext;  
  7.     View.OnClickListener clickListener;  
  8.       
  9.     // 用于显示每列5个Item项。  
  10.     int VIEW_COUNT = 10;  
  11.   
  12.     // 用于显示页号的索引  
  13.     int index = 0;  
  14.   
  15.      protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.list_ylfn);  
  18.   
  19.         list_ylfn = (ListView) findViewById(R.id.listYlfn);  
  20.         btnPre = (Button) findViewById(R.id.btnPre);  
  21.         btnNext = (Button) findViewById(R.id.btnNext);  
  22.   
  23.         listItem = new ArrayList<HashMap<String, String>>();  
  24.   
  25.         HttpClient client = new DefaultHttpClient();  
  26.         HttpEntity entity = null;  
  27.         try {  
  28.             String uri = GetConnParams.getConnUri()  
  29.                     + "/phone_listYlfn?zgy.zgynum=" + zgynumLoged;  
  30.                 //此处是从服务端获取数据,有些代码就省略了  
  31.             HttpPost request = new HttpPost(uri);  
  32.             HttpResponse response;  
  33.             response = client.execute(request);  
  34.             if (response.getStatusLine().getStatusCode() == 200) {  
  35.                 entity = response.getEntity();  
  36.             }  
  37.             String json = EntityUtils.toString(entity, "UTF-8").trim();  
  38.   
  39.             JSONArray array = new JSONArray(URLDecoder.decode(json, "utf-8"));  
  40.             for (int i = 0; i < array.length(); i++) {  
  41.                 HashMap<String, String> map = new HashMap<String, String>();  
  42.                 map.put("ylfn_did", array.getJSONObject(i).getString("did"));  
  43.                 map.put("ylfn_name", array.getJSONObject(i).getString("name"));  
  44.                 map.put("gmsfz", array.getJSONObject(i).getString("gmsfz"));  
  45.                 listItem.add(map);  
  46.                 tmpListItem.add(map);  
  47.             }  
  48. //          // 生成适配器的Item和动态数组对应的元素  
  49. //          SimpleAdapter listItemAdapter = new SimpleAdapter(this, listItem,// 数据源  
  50. //                  R.layout.ylfn,// ListItem的XML实现  
  51. //                  // 动态数组与ImageItem对应的子项  
  52. //                  new String[] { "ylfn_did", "ylfn_name", "gmsfz" },  
  53. //                  // ImageItem的XML文件里面的一个ImageView,两个TextView ID  
  54. //                  new int[] { R.id.ylfn_did, R.id.ylfn_name, R.id.gmsfz });  
  55. //  
  56.             myAdapter=new MyAdapter(this);  
  57.             list_ylfn.setAdapter(myAdapter);  
  58.               
  59.             clickListener = new Button.OnClickListener() {  
  60.                 @Override  
  61.                 public void onClick(View v) {  
  62.                     // TODO Auto-generated method stub  
  63.                     switch (v.getId()) {  
  64.                     case R.id.btnPre:  
  65.                         preView();  
  66.                         break;  
  67.                     case R.id.btnNext:  
  68.                         nextView();  
  69.                         break;  
  70.                     }  
  71.                 }  
  72.   
  73.             };  
  74.               
  75.             // 添加2个Button的监听事件。  
  76.             btnPre.setOnClickListener(clickListener);  
  77.             btnNext.setOnClickListener(clickListener);  
  78.             // 检查2个Button是否是可用的  
  79.             checkButton();  
  80.   
  81.         } catch (ClientProtocolException e) {  
  82.             // TODO Auto-generated catch block  
  83.             e.printStackTrace();  
  84.         } catch (IOException e) {  
  85.             // TODO Auto-generated catch block  
  86.             e.printStackTrace();  
  87.         } catch (JSONException e) {  
  88.             // TODO Auto-generated catch block  
  89.             e.printStackTrace();  
  90.         }finally{             
  91.             try {  
  92.                 if(entity!=null)  
  93.                     entity.consumeContent();  
  94.                 client.getConnectionManager().shutdown();  
  95.             } catch (IOException e) {  
  96.                 // TODO Auto-generated catch block  
  97.                 e.printStackTrace();  
  98.             }  
  99.         }         
  100.     }  
  101.   
  102.       
  103.     // 点击左边的Button,表示向前翻页,索引值要减1.  
  104.     public void preView() {  
  105.         index--;  
  106.   
  107.         // 刷新ListView里面的数值。  
  108.         myAdapter.notifyDataSetChanged();  
  109.   
  110.         // 检查Button是否可用。  
  111.         checkButton();  
  112.     }  
  113.   
  114.     // 点击右边的Button,表示向后翻页,索引值要加1.  
  115.     public void nextView() {  
  116.         index++;  
  117.   
  118.         // 刷新ListView里面的数值。  
  119.         myAdapter.notifyDataSetChanged();  
  120.   
  121.         // 检查Button是否可用。  
  122.         checkButton();  
  123.     }  
  124.   
  125.     public void checkButton() {  
  126.         // 索引值小于等于0,表示不能向前翻页了,以经到了第一页了。  
  127.         // 将向前翻页的按钮设为不可用。  
  128.         if (index <= 0) {  
  129.             btnPre.setEnabled(false);  
  130.         }else{  
  131.             btnPre.setEnabled(true);  
  132.         }  
  133.         // 值的长度减去前几页的长度,剩下的就是这一页的长度,如果这一页的长度比View_Count小,表示这是最后的一页了,后面在没有了。  
  134.         // 将向后翻页的按钮设为不可用。  
  135.         if (listItem.size() - index * VIEW_COUNT <= VIEW_COUNT) {  
  136.             btnNext.setEnabled(false);  
  137.         }  
  138.         // 否则将2个按钮都设为可用的。  
  139.         else {  
  140.             btnNext.setEnabled(true);  
  141.         }  
  142.   
  143.     }  
  144.       
  145.     // ListView的Adapter,这个是关键的导致可以分页的根本原因。  
  146.     public class MyAdapter extends BaseAdapter {  
  147.         Activity activity;  
  148.   
  149.         public MyAdapter(Activity a) {  
  150.             activity = a;  
  151.         }  
  152.   
  153.         // 设置每一页的长度,默认的是View_Count的值。  
  154.         @Override  
  155.         public int getCount() {  
  156.             // TODO Auto-generated method stub  
  157.             // return data.length;  
  158.   
  159.             // ori表示到目前为止的前几页的总共的个数。  
  160.             int ori = VIEW_COUNT * index;  
  161.   
  162.             // 值的总个数-前几页的个数就是这一页要显示的个数,如果比默认的值小,说明这是最后一页,只需显示这么多就可以了  
  163.             if (listItem.size() - ori < VIEW_COUNT) {  
  164.                 return listItem.size() - ori;  
  165.             }  
  166.             // 如果比默认的值还要大,说明一页显示不完,还要用换一页显示,这一页用默认的值显示满就可以了。  
  167.             else {  
  168.                 return VIEW_COUNT;  
  169.             }  
  170.   
  171.         }  
  172.   
  173.         @Override  
  174.         public Object getItem(int position) {  
  175.             // TODO Auto-generated method stub  
  176.             return position;  
  177.         }  
  178.   
  179.         @Override  
  180.         public long getItemId(int position) {  
  181.             // TODO Auto-generated method stub  
  182.             return position;  
  183.         }  
  184.   
  185.                 //重点是getView方法  
  186.         @Override  
  187.         public View getView(int position, View convertView, ViewGroup parent) {  
  188.             // TODO Auto-generated method stub  
  189.                         // return addTestView(position);  
  190.             convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.ylfn,null);  
  191.             TextView ylfn_did_view = (TextView)convertView.findViewById(R.id.ylfn_did);  
  192.             TextView ylfn_name_view = (TextView)convertView.findViewById(R.id.ylfn_name);  
  193.             TextView ylfn_gmsfz_view = (TextView)convertView.findViewById(R.id.gmsfz);  
  194.             ylfn_did_view.setText(listItem.get(position + index * VIEW_COUNT).get("ylfn_did"));  
  195.             ylfn_name_view.setText(listItem.get(position + index * VIEW_COUNT).get("ylfn_name"));  
  196.             ylfn_gmsfz_view.setText(listItem.get(position + index * VIEW_COUNT).get("gmsfz"));  
  197.             return convertView;  
  198.         }  
  199.     }  
  200. }  

 list_ylfn.xml代码:

[xml] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"   
  6.     android:background="@drawable/beijing">  
  7.   
  8.     <LinearLayout  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="36dp"  
  11.         android:gravity="center"  
  12.         android:orientation="horizontal"   
  13.         android:layout_marginTop="44dip">  
  14.   
  15.         <TextView  
  16.         android:layout_width="40dp"  
  17.         android:layout_height="36dp"  
  18.             android:gravity="center"  
  19.             android:text="编号"  
  20.             android:textSize="12sp" />  
  21.   
  22.         <TextView  
  23.         android:layout_width="160dp"  
  24.         android:layout_height="36dp"  
  25.             android:gravity="center"  
  26.             android:text="姓名"  
  27.             android:textSize="12sp" />  
  28.   
  29.         <TextView  
  30.             android:layout_width="wrap_content"  
  31.             android:layout_height="36dp"  
  32.             android:gravity="center"  
  33.             android:text="身份证号"  
  34.             android:textSize="12sp" />  
  35.     </LinearLayout>  
  36.       
  37.     <ListView android:id="@+id/listYlfn"  
  38.         android:layout_width="wrap_content"  
  39.         android:layout_height="wrap_content"  
  40.         android:layout_marginTop="88dp"   
  41.         android:layout_marginBottom="32dip"  
  42.         android:textFilterEnabled="true">      
  43.     </ListView>  
  44.   
  45.     <LinearLayout  
  46.         android:layout_width="fill_parent"  
  47.         android:layout_height="32dip"  
  48.         android:layout_alignParentBottom="true"  
  49.         android:layout_alignParentLeft="true"  
  50.         android:gravity="center"  
  51.         android:orientation="horizontal" >  
  52.   
  53.         <Button  
  54.             android:id="@+id/btnPre"  
  55.             android:layout_width="80dip"  
  56.             android:layout_height="32dip"  
  57.             android:text="上一页"  
  58.             android:textSize="12sp" />  
  59.   
  60.         <Button  
  61.             android:id="@+id/btnNext"  
  62.             android:layout_width="80dip"  
  63.             android:layout_height="32dip"  
  64.             android:layout_marginLeft="20dip"  
  65.             android:text="下一页"  
  66.             android:textSize="12sp" />  
  67.     </LinearLayout>  
  68.   
  69. </RelativeLayout>  

ylfn.xml代码:

[xml] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="horizontal"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="wrap_content"  
  6.     android:gravity="center">  
  7.     <TextView android:text="编号"  
  8.         android:layout_width="40dp"  
  9.         android:layout_height="36dp"  
  10.         android:id="@+id/ylfn_did"  
  11.         android:gravity="center"  
  12.         android:textSize="12sp"  
  13.         android:textColor="#000000"/>  
  14.     <TextView android:text="姓名"  
  15.         android:layout_width="80dp"  
  16.         android:layout_height="36dp"  
  17.         android:id="@+id/ylfn_name"  
  18.         android:gravity="center"  
  19.         android:textSize="12sp"  
  20.         android:textColor="#000000"/>  
  21.     <TextView android:text="身份证号"  
  22.         android:layout_width="wrap_content"  
  23.         android:layout_height="36dp"  
  24.         android:id="@+id/gmsfz"  
  25.         android:gravity="center"  
  26.         android:textSize="12sp"  
  27.         android:textColor="#000000"/>  
  28. </LinearLayout>  
 
 
原文地址:https://www.cnblogs.com/wangfeng520/p/6178455.html