安卓:查询sqlite数据库中的数据,分页加载显示出来

结果:


主逻辑代码文件:

<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>


  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值