安卓:查询sqlite数据库中的数据,分页加载显示出来
2015-09-21 17:09
507 查看
结果:
主逻辑代码文件:
主布局文件:
适配器的样式布局文件:
主逻辑代码文件:
<span style="font-size:18px;">package com.example.day14_pageload; import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.annotation.SuppressLint; import android.app.Activity; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.widget.AbsListView; import android.widget.AbsListView.OnScrollListener; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.SimpleAdapter; public class MainActivity extends Activity { ListView lv; RelativeLayout rl; SQLiteDatabase db; SimpleAdapter adapter; List<Map<String, Object>> list ; int totalNum;//数据总条数 int pageSize=15;//每页显示的条数 int totalPage;//总的页数 int currentPage=1;//当前页码 boolean isbottom=false;//判断是否到当前页码的底部 String path = Environment.getExternalStorageDirectory(). getAbsolutePath()+File.separator+"Download"+File.separator+"test.db"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv=(ListView) findViewById(R.id.lv); rl=(RelativeLayout) findViewById(R.id.rl); db=SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READWRITE); String sql="select * from person"; Cursor c=db.rawQuery(sql, null); totalNum=c.getCount(); totalPage=(int)Math.ceil((totalNum/pageSize)); if(1==currentPage) { getData(currentPage); } adapter=new SimpleAdapter(getApplicationContext(),list, R.layout.style, new String[] { "name", "sex", "age", "love" }, new int[] { R.id.name, R.id.sex, R.id.age, R.id.love }); lv.setAdapter(adapter); lv.setOnScrollListener(new OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { if(isbottom&&(scrollState == OnScrollListener.SCROLL_STATE_IDLE)) { rl.setVisibility(View.VISIBLE); } else { rl.setVisibility(View.GONE); } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { isbottom=((firstVisibleItem+visibleItemCount)==totalItemCount); } }); } //点击加载更多的监听事件 public void click(View v) { currentPage++; list.addAll(getData(currentPage)); adapter.notifyDataSetChanged(); rl.setVisibility(View.GONE); } //获取当前页的数据 @SuppressLint("NewApi") public List<Map<String, Object>> getData(int currentPage){ int index = (currentPage-1)*pageSize;//0 0 1 20 2 40 String sql = "select * from person limit ?,?";//从哪一个位置 请求多少条 Cursor c = db.rawQuery(sql, new String[]{index+"",pageSize+""}); return getCursorList(c); } @SuppressLint("NewApi") public List<Map<String, Object>> getCursorList(Cursor c){ list = new ArrayList<Map<String,Object>>(); String[] coloms = c.getColumnNames();//[_id,name] 有多少字段项 while(c.moveToNext()){ Map<String, Object> map = new HashMap<String, Object>(); for(int i=0;i<coloms.length;i++){ String key = coloms[i]; Object values = null; int type = c.getType(i); switch (type) { case Cursor.FIELD_TYPE_BLOB: values = c.getBlob(i); break; case Cursor.FIELD_TYPE_FLOAT: values = c.getFloat(i); break; case Cursor.FIELD_TYPE_INTEGER: values = c.getInt(i); break; case Cursor.FIELD_TYPE_STRING: values = c.getString(i); break; default: break; } map.put(key, values); } list.add(map); } return list; } } </span>
主布局文件:
<span style="font-size:18px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <RelativeLayout android:layout_alignParentBottom="true" android:id="@+id/rl" android:layout_width="match_parent" android:layout_height="50dp" android:onClick="click" android:gravity="center" android:visibility="gone"> <ProgressBar android:id="@+id/pb" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="加载更多" android:gravity="center" android:layout_toRightOf="@id/pb"/> </RelativeLayout> <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@id/rl" > </ListView> </RelativeLayout> </span>
适配器的样式布局文件:
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#00ff00" android:textSize="28sp"/> <TextView android:id="@+id/sex" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#00ff00" android:textSize="28sp"/> <TextView android:id="@+id/age" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#00ff00" android:textSize="28sp"/> <TextView android:id="@+id/love" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#00ff00" android:textSize="28sp"/> </LinearLayout> </span>
相关文章推荐
- QUARTZ任务不写入数据库
- Oracle Jdbc 防sql注入
- 备忘:连接数据库的写法
- Mysql免安装版安装配置及常用操作
- SQL Server数据库优化的10多种方法
- memcache CAS实现原子操作
- 数据库SQL优化大总结之 百万级数据库优化方案
- Oracle存储过程的简单示例
- mysql常用引擎
- MongoDB 的安装和使用
- sql存储过程查询栏目下面的所有子栏目
- MYSQL用户和权限操作
- 数据库设计三大范式
- [MySQL分享]--interactive_timeout和wait_timeout小结(interactive_timeout会覆盖wait_timeout)
- MongoDB 基础操作
- mysql语句:批量更新多条记录的不同值
- PostgreSQL-角色、库、模式、表
- 不同场景下MySQL的迁移方案
- oracle中的null
- plsql的安装,与plsql链接服务器的操作步骤。