安卓分頁顯示數據(上一頁和下一頁)


先說一下分頁,大部分都是滾動加載,而有上一頁下一頁效果的,網上很多都是同一個例子,就是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>  
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM