导图社区 服务器数据分页信息知识点笔记
1.通常在Web页面,在显示数据量比较大时,无法在一个页面上显示所有的数据。 2.在某些特定场景下,并不需要返回所有满足条件的数据。 3.从数据的角度看,需要返回.
编辑于2022-11-07 18:03:23 广东服务器数据分页信息知识点笔记
思想
01数据分页策略
1.用多少查多少
2.全部查询出来,然后进行分页处理.
02需要的页数
刚好整除
记录总数/每页显示的记录数
不整除
记录总数/每页显示的记录数+1
03数据索引值
起始索引值
当前页-1)*每页显示的记录数
起点0
结束索引值
当前页*每页显示的记录数;
如果结束索引值大于记录总数则结束索引值应该为记录总数.
04访问当前Servlet的方式
http://10.2.155.23:8080/ServerPaginationDemo01/paginationServlet?pageNum=1
自定义分页信息实体类
初始化成员变量
每页显示数目
pageSize
记录总数
recordeCount
当前页
currentPage
总页数
pageCount
构造函数
PageInfo(int pageSize,int recordeCount,int currentPage)
获得成员变量
this.pageSize=pageSize;
this.recordCount=recordCount;
this.currentPage=currentPage;
getsetXX()
getPageSize()、recordeCount()、currentPage()、pageCount()
修改部分方法
getCurrentPage
小于1
int activityPage = currentPage<0?1:currentPage
超过总页数
activityPage = activityPage>getPageCount()?getPageCount:activityCount
getPageCount()
获得pageCount=recordCount/pageSize;
判断余数为0
int mod=recordCount%pageSize;
if(mod!=0){
pageCount++;
若为0也设置为1
return recordCount==0?1:pageCount;
索引值
getStartIndex()
return (getCurrentPage()-1)*this.pageSize
getEndIndex()
return Math.min(this.recordCount, currentPage*this.pageSize)
速记:
树下4个成员,构造鸭子、getset八戒耳朵,4,修改船,5、索引
服务器端分页
自定义数据源类
public List<String> getDataSource(){
List<String> list=new ArrayList<String>();
for(int i=1;i<=100;i++){
list.add("第"+i+"名");
return list;
定义 数据格式
定义数据格式
response.setContentType("text/html;charset=utf-8");
定义输出流对象
PrintWriter out = response.getWriter()
获得数据源
new实例化
PageDataSource pageDataSource=new PageDataSource();
调用该自定义对象的方法获得数据
List<String> list=pageDataSource.getDataSource();
获得当前页面
初始化为1
int currentPage=1;
接收response相应页面数
String pageNum=request.getParameter("pageNum");
页面细节处理
不为空
if(pageNum!=null){
currentPage=Integer.parseInt(pageNum);
不小于0
if(currentPage<=0){
currentPage=1;
定义当前页面记录数及总数
int pageSize=30
int recordCount=list.size();
实例化信息实体
PageInfo pageInfo=new PageInfo(pageSize,recordCount,currentPage);
int startIndex=pageInfo.getStartIndex();
int endIndex=pageInfo.getEndIndex();
分页后显示的数据
subList获得显示的部分内容
List<String> subList=list.subList(startIndex, endIndex);
自定义集合map封装数据
Map<String,Object> map=new HashMap<String,Object>();
map.put("ranking",subList);
fastJson解析数据
String json=JSON.toJSONString(map);
out.println(json);
客户端(本地数据)
提供本地数据
List<String> getData()
List<String> list=new ArrayList<String>();
for(int i=(pageNum-1)*30+1;i<=pageNum*30;i++){
list.add("第"+i+"名");
return list;
声明对象
ListView控件
分页显示数据
ProgressDialog
MyAdapter
自定义适配器对象
pageNum
当前页号
boolean isBottom
用于判断滚动到ListView的最后条目
initView()初始化控件
listView_data
this.findViewById(R.id.listView_data)
this.progressDialog
new ProgressDialog(this)
setTitle("分页加载");
setMessage("正在加载数据,请稍等......");
自定义MyAdapter()
this.listView_data.setAdapter(this.myAdapter);
class MyAdapter extends BaseAdapter
声明显示总数据的集合
private List<String> data;
this.data=new ArrayList<String>();
自定义addData(List<String> result)方法
data.addAll(result);
添加分页数据
getCount()
return data.size();
getItem(int position)
return data.get(position)
getIetmId()
View getView(int position, View convertView, ViewGroup parent)
TextView textView
if(convertView==null){
textView=new TextView(getApplicationContext());
else{
textView=(TextView) convertView;
定义条目内容
String item=data.get(position);
textView.setText(item);
return textView
new MyAsyncTask().execute();
自定义异步任务类
class MyAsyncTask extends AsyncTask<Void, Void, List<String>>{
onPreExecute()
progressDialog.show();
List<String> doInBackground(Void... params)
List<String> list=getData();
获得数据
onPostExecute(List<String> result)
UI添加数据
myAdapter.addData(result);
动态刷新
myAdapter.notifyDataSetChanged();
pageNum++;
progressDialog.dismiss();
给ListView注册滚动监听
this.listView_data.setOnScrollListener(new OnScrollListener() {
onScrollStateChanged
到底部更新
if(isBottom && scrollState==OnScrollListener.SCROLL_STATE_IDLE && pageNum<5){
new MyAsyncTask().execute();
已加载完成
else if(pageNum>4 && isBottom)
Toast.makeText(getApplicationContext(), "没有数据了!",Toast.LENGTH_LONG).show();
滚动状态发生变化
空闲状态
OnScrollListener.SCROLL_STATE_IDLE=0
滚动触摸
SCROLL_STATE_TOUCH_SCROLL=1
惯性滑动
CROLL_STATE_FLING=2
onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount)
第一个可见条目的索引值
visibleItemCount
当前屏幕中可见条目的个数
totalItemCount
适配器中数据源的数据条目总数
客户端访问服务器分页
自定义全局常量类
class GlobalConst
public static final String SERVER_FOR_JSON
"http://10.2.155.20:8080/ServerPaginationDemo01/paginationServlet?pageNum="
自定义Http使用工具类HttpUtils
HttpClient httpClient=new DefaultHttpClient();
自定义Json解析数据工具类
List<String> getListByParseJson(String json)
json对象
JSONObject jsonObject=new JSONObject(json);
通过json对象获得json数组
JSONArray jsonArray=jsonObject.getJSONArray("ranking");
遍历数组
int len=jsonArray.length();
for(int i=0;i<len;i++){
String item=jsonArray.getString(i);
list.add(item);
启动异步类
new MyAsyncTask().execute(GlobalConst.SERVER_FOR_JSON+pageNum);
MyAsyncTask extends AsyncTask<String, Void, List<String>>
doInBackground
获得path
String path=params[0];
通过网络path获得json数据
String json=HttpUtils.getServerContent(path);
通过json获得解析后的list数据
JsonUtils.getListByParseJson(json);