1. 程式人生 > >Android之ListView分頁獲取網路資料(伺服器端)(一)

Android之ListView分頁獲取網路資料(伺服器端)(一)

資料庫分頁:

mysql:select pname from product limit 0,2;第一個引數是指要開始的地方,第二個引數是指每頁顯示多少條資料;注意:第一頁用0表示。

oracle:rownumber

SqlServer:top

一、伺服器端

①新建包com.paging.action中建立CityAction.java類

package com.paging.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONSerializer;

/**
 * Servlet implementation class CityAction
 */
@WebServlet("/CityAction")
public class CityAction extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public CityAction() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter writer=response.getWriter();
		//String type=request.getParameter("type");
			List<String> list=CityDataSource.getCitysList();//生成json物件,json資料一定要帶<K,V>
			String pageNo=request.getParameter("pageNo");//伺服器端接收客戶端的一個頁碼
			int currentPage=1;//當前頁,當前頁是第一頁,開啟手機進來的時候是第一頁
			
			if(pageNo!=null){
				currentPage=Integer.parseInt(pageNo);//如果接收到一個值(這個值是傳遞過來的),則轉化成一個整型資料,原因是這個頁碼是要變化的,而且還要累加
			}
			
			DividePage pUtil=new DividePage(25,list.size(), currentPage);//
			
			int start=pUtil.getFromIndex();//從哪開始
			int end=pUtil.getToIndex();//到哪結束
			
			List<String> subList=list.subList(start, end);//在總集合中擷取集合,模擬分頁
			
			Map<String, List<String>>map=new HashMap<String, List<String>>();
			map.put("citys", subList);
			String jsonString=JSONSerializer.toJSON(map).toString();
			writer.println(jsonString);//注意生成json字串時,這個地方一定要是獨立乾淨的
		
		writer.flush();
		writer.close();
	}

}
②建立資料來源CityDataSource.java
package com.paging.action;

import java.util.ArrayList;
import java.util.List;

public class CityDataSource {

	public CityDataSource() {
		// TODO Auto-generated constructor stub
	}
	
	/**
	 * 提供資料來源
	 * @return
	 */
	public static List<String> getCitysList(){
		List<String> list=new ArrayList<String>();
		for(int i=0;i<300;i++){
			list.add("上海"+i);
		}//為了增強滑動效果,用for增加資料集合
		return list;
	}
}

③需要把資料拆分,第一頁顯示幾條,第二頁顯示幾條,所以,頁碼是變化的,變化由客戶端提交變化(手機進來是第一頁,再滑動則是第二頁,再滑第三頁,頁碼是由客戶端提交上來的),建立分頁工具類DividePage.java
package com.paging.action;

import java.util.*;

public class DividePage {

	private int pageSize;// 每頁顯示的條數
	private int recordCount; // 記錄的總條數
	private int currentPage;// 當前頁
	private int pageCount;// 總頁數

	public DividePage(int pageSize, int recordCount, int currentPage) {
		this.pageSize = pageSize;
		this.recordCount = recordCount;
		this.setCurrentPage(currentPage);

	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getRecordCount() {
		return recordCount;
	}

	public void setRecordCount(int recordCount) {
		this.recordCount = recordCount;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	/**
	 * 設定定位在當前頁
	 * 
	 * @param currentPage
	 */
	public void setCurrentPage(int currentPage) {
		int activePage = currentPage <= 0 ? 1 : currentPage;
		activePage = activePage > getPageCount() ? getPageCount()
				: activePage;
		this.currentPage = activePage;
	}

	/**
	 * 獲得總頁數
	 * 
	 * @return
	 */
	public int getPageCount() {
		pageCount = recordCount / pageSize;
		int mod = recordCount % pageSize;
		if (mod != 0) {
			pageCount++;
		}
		return recordCount == 0 ? 1 : pageCount;
	}

	public int getFromIndex() {
		return (currentPage - 1) * pageSize;
	}

	public int getToIndex() {

		return Math.min(recordCount, currentPage * pageSize);
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}
}

在位址列輸入http://192.168.1.102:8080/Web_ListView_Paging/CityAction?pageNo=1顯示如下


在位址列輸入http://192.168.1.102:8080/Web_ListView_Paging/CityAction?pageNo=2顯示如下


在位址列輸入http://192.168.1.102:8080/Web_ListView_Paging/CityAction?pageNo=3顯示如下