先說一下分頁,大部分都是滾動加載,而有上一頁下一頁效果的,網上很多都是同一個例子,就是data是一個String型的數組,在其最重要的getView()方法中,寫得很讓人看不懂,自己又參考了其它的例子,終於明白了,於是就有了以下的代碼:
DsznzActivity代碼:
- public class DsznzActivity extends Activity {
- private ArrayList<HashMap<String, String>> listItem;
- private ListView list_ylfn;
- Button btnPre, btnNext;
- View.OnClickListener clickListener;
- // 用於顯示每列5個Item項。
- int VIEW_COUNT = 10;
- // 用於顯示頁號的索引
- int index = 0;
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.list_ylfn);
- list_ylfn = (ListView) findViewById(R.id.listYlfn);
- btnPre = (Button) findViewById(R.id.btnPre);
- btnNext = (Button) findViewById(R.id.btnNext);
- listItem = new ArrayList<HashMap<String, String>>();
- HttpClient client = new DefaultHttpClient();
- HttpEntity entity = null;
- try {
- String uri = GetConnParams.getConnUri()
- + "/phone_listYlfn?zgy.zgynum=" + zgynumLoged;
- //此處是從服務端獲取數據,有些代碼就省略了
- HttpPost request = new HttpPost(uri);
- HttpResponse response;
- response = client.execute(request);
- if (response.getStatusLine().getStatusCode() == 200) {
- entity = response.getEntity();
- }
- String json = EntityUtils.toString(entity, "UTF-8").trim();
- JSONArray array = new JSONArray(URLDecoder.decode(json, "utf-8"));
- for (int i = 0; i < array.length(); i++) {
- HashMap<String, String> map = new HashMap<String, String>();
- map.put("ylfn_did", array.getJSONObject(i).getString("did"));
- map.put("ylfn_name", array.getJSONObject(i).getString("name"));
- map.put("gmsfz", array.getJSONObject(i).getString("gmsfz"));
- listItem.add(map);
- tmpListItem.add(map);
- }
- // // 生成適配器的Item和動態數組對應的元素
- // SimpleAdapter listItemAdapter = new SimpleAdapter(this, listItem,// 數據源
- // R.layout.ylfn,// ListItem的XML實現
- // // 動態數組與ImageItem對應的子項
- // new String[] { "ylfn_did", "ylfn_name", "gmsfz" },
- // // ImageItem的XML文件里面的一個ImageView,兩個TextView ID
- // new int[] { R.id.ylfn_did, R.id.ylfn_name, R.id.gmsfz });
- //
- myAdapter=new MyAdapter(this);
- list_ylfn.setAdapter(myAdapter);
- clickListener = new Button.OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- switch (v.getId()) {
- case R.id.btnPre:
- preView();
- break;
- case R.id.btnNext:
- nextView();
- break;
- }
- }
- };
- // 添加2個Button的監聽事件。
- btnPre.setOnClickListener(clickListener);
- btnNext.setOnClickListener(clickListener);
- // 檢查2個Button是否是可用的
- checkButton();
- } catch (ClientProtocolException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (JSONException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally{
- try {
- if(entity!=null)
- entity.consumeContent();
- client.getConnectionManager().shutdown();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- // 點擊左邊的Button,表示向前翻頁,索引值要減1.
- public void preView() {
- index--;
- // 刷新ListView里面的數值。
- myAdapter.notifyDataSetChanged();
- // 檢查Button是否可用。
- checkButton();
- }
- // 點擊右邊的Button,表示向后翻頁,索引值要加1.
- public void nextView() {
- index++;
- // 刷新ListView里面的數值。
- myAdapter.notifyDataSetChanged();
- // 檢查Button是否可用。
- checkButton();
- }
- public void checkButton() {
- // 索引值小於等於0,表示不能向前翻頁了,以經到了第一頁了。
- // 將向前翻頁的按鈕設為不可用。
- if (index <= 0) {
- btnPre.setEnabled(false);
- }else{
- btnPre.setEnabled(true);
- }
- // 值的長度減去前幾頁的長度,剩下的就是這一頁的長度,如果這一頁的長度比View_Count小,表示這是最后的一頁了,后面在沒有了。
- // 將向后翻頁的按鈕設為不可用。
- if (listItem.size() - index * VIEW_COUNT <= VIEW_COUNT) {
- btnNext.setEnabled(false);
- }
- // 否則將2個按鈕都設為可用的。
- else {
- btnNext.setEnabled(true);
- }
- }
- // ListView的Adapter,這個是關鍵的導致可以分頁的根本原因。
- public class MyAdapter extends BaseAdapter {
- Activity activity;
- public MyAdapter(Activity a) {
- activity = a;
- }
- // 設置每一頁的長度,默認的是View_Count的值。
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- // return data.length;
- // ori表示到目前為止的前幾頁的總共的個數。
- int ori = VIEW_COUNT * index;
- // 值的總個數-前幾頁的個數就是這一頁要顯示的個數,如果比默認的值小,說明這是最后一頁,只需顯示這么多就可以了
- if (listItem.size() - ori < VIEW_COUNT) {
- return listItem.size() - ori;
- }
- // 如果比默認的值還要大,說明一頁顯示不完,還要用換一頁顯示,這一頁用默認的值顯示滿就可以了。
- else {
- return VIEW_COUNT;
- }
- }
- @Override
- public Object getItem(int position) {
- // TODO Auto-generated method stub
- return position;
- }
- @Override
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return position;
- }
- //重點是getView方法
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- // TODO Auto-generated method stub
- // return addTestView(position);
- convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.ylfn,null);
- TextView ylfn_did_view = (TextView)convertView.findViewById(R.id.ylfn_did);
- TextView ylfn_name_view = (TextView)convertView.findViewById(R.id.ylfn_name);
- TextView ylfn_gmsfz_view = (TextView)convertView.findViewById(R.id.gmsfz);
- ylfn_did_view.setText(listItem.get(position + index * VIEW_COUNT).get("ylfn_did"));
- ylfn_name_view.setText(listItem.get(position + index * VIEW_COUNT).get("ylfn_name"));
- ylfn_gmsfz_view.setText(listItem.get(position + index * VIEW_COUNT).get("gmsfz"));
- return convertView;
- }
- }
- }
list_ylfn.xml代碼:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:background="@drawable/beijing">
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="36dp"
- android:gravity="center"
- android:orientation="horizontal"
- android:layout_marginTop="44dip">
- <TextView
- android:layout_width="40dp"
- android:layout_height="36dp"
- android:gravity="center"
- android:text="編號"
- android:textSize="12sp" />
- <TextView
- android:layout_width="160dp"
- android:layout_height="36dp"
- android:gravity="center"
- android:text="姓名"
- android:textSize="12sp" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="36dp"
- android:gravity="center"
- android:text="身份證號"
- android:textSize="12sp" />
- </LinearLayout>
- <ListView android:id="@+id/listYlfn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="88dp"
- android:layout_marginBottom="32dip"
- android:textFilterEnabled="true">
- </ListView>
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="32dip"
- android:layout_alignParentBottom="true"
- android:layout_alignParentLeft="true"
- android:gravity="center"
- android:orientation="horizontal" >
- <Button
- android:id="@+id/btnPre"
- android:layout_width="80dip"
- android:layout_height="32dip"
- android:text="上一頁"
- android:textSize="12sp" />
- <Button
- android:id="@+id/btnNext"
- android:layout_width="80dip"
- android:layout_height="32dip"
- android:layout_marginLeft="20dip"
- android:text="下一頁"
- android:textSize="12sp" />
- </LinearLayout>
- </RelativeLayout>
ylfn.xml代碼:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center">
- <TextView android:text="編號"
- android:layout_width="40dp"
- android:layout_height="36dp"
- android:id="@+id/ylfn_did"
- android:gravity="center"
- android:textSize="12sp"
- android:textColor="#000000"/>
- <TextView android:text="姓名"
- android:layout_width="80dp"
- android:layout_height="36dp"
- android:id="@+id/ylfn_name"
- android:gravity="center"
- android:textSize="12sp"
- android:textColor="#000000"/>
- <TextView android:text="身份證號"
- android:layout_width="wrap_content"
- android:layout_height="36dp"
- android:id="@+id/gmsfz"
- android:gravity="center"
- android:textSize="12sp"
- android:textColor="#000000"/>
- </LinearLayout>