先进行简单的数据操作.然后在数据库上面做分页查询的操作,也就是listview分页.看代码
MysqLiteOpenHelper类,继承sqliteOpenHelper类,是android中操作数据库的辅助类
package zuoyesqllite.hall;
import android.content.Context;
import android.database.sqlite.sqliteDatabase;
import android.database.sqlite.sqliteDatabase.CursorFactory;
import android.database.sqlite.sqliteOpenHelper;
public class MysqLiteOpenHelper extends sqliteOpenHelper {
private static final int dataversion=1;
private String TableName="";
/**
* context当前的上下问,Dataname数据库名称
*factory游标工厂,version数据库版本,TableName 要创建的表名
*/
public MysqLiteOpenHelper(Context context,String Dataname,
CursorFactory factory,int version,String TableName)
{
super(context,Dataname,factory,version);
this.TableName=TableName;
}
public MysqLiteOpenHelper(Context context,String TableName)
{
this(context,null,version,TableName);
// TODO Auto-generated constructor stub
}
public MysqLiteOpenHelper(Context context,dataversion,TableName);
// TODO Auto-generated constructor stub
}
//onCreate方法只有在数据库被创建的时候触发
@Override
public void onCreate(sqliteDatabase db) {
// TODO Auto-generated method stub
String sql="create table "+TableName+"(_id integer primary key,id text,name text,age text);";
db.execsql(sql);
System.out.println("数据库被创建");
}
//数据库被打开的时候触发
@Override
public void onOpen(sqliteDatabase db) {
// TODO Auto-generated method stub
super.onOpen(db);
System.out.println("数据库被打开");
}
//数据库本版更新的时候触发
@Override
public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) {
// TODO Auto-generated method stub
System.out.println("数据库被更新");
}
zuoyesqllite 类,是一个activity类
//注意.当助手类被new出来的时候,并不会触发其oncreat或者onopen方法,
//只有当取得数据库对象的时候才会触发oncreat或者onopen方法
//如msqliteDatabase=mMysqLiteOpenHelper.getWritableDatabase();
//如果数据库存在,则打开数据库触发onopen(),如果数据库不存在,则创建数据库,触发onCreate()
//数据库操作中几个参数的意思:distinct="如果多个相同值,只去一个",table="表命",columns="要查询的列名",
selection="查询条件",
// selectionArgs="条件中用了占位符的参数",groupBy="数据分组",having="分组后的条件",
具体请看代码:
package zuoyesqllite.hall;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.sqliteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
public class zuoyesqllite extends Activity {
private TextView text_pageNo;//显示页码的textview
private Button button_next,button_last;
private ListView listview = null;
private ListAdapter adapter;
private MysqLiteOpenHelper mMysqLiteOpenHelper=null;//助手类
private sqliteDatabase msqliteDatabase=null;//数据库对象
private EditText edit_id,edit_name,edit_age;
private Button button_add,button_update,button_delete,button_query;
private String text_id="",text_name="",text_age="";
private int limit=5;//显示5行数据
private int pageNo=1;//页码
private int pageCount=0;//页面总数
private boolean isfirst=false;
public static final String TableName="userinfo1";
public static final String Dataname="myData";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mMysqLiteOpenHelper =new MysqLiteOpenHelper(this,TableName);
findView();// 获得控件对象
isfirst=true;
}
//获取每个控件
private void findView(){
edit_id=(EditText)findViewById(R.id.edit_id);
edit_age=(EditText)findViewById(R.id.edit_age);
edit_name=(EditText)findViewById(R.id.edit_name);
button_add=(Button)findViewById(R.id.button_add);
button_add.setOnClickListener(new MyButtonListener());
button_delete=(Button)findViewById(R.id.button_delete);
button_delete.setOnClickListener(new MyButtonListener());
button_update=(Button)findViewById(R.id.button_update);
button_update.setOnClickListener(new MyButtonListener());
button_query=(Button)findViewById(R.id.button_query);
button_query.setOnClickListener(new MyButtonListener());
button_last = (Button) findViewById(R.id.button_last);
button_last.setOnClickListener(new MyButtonListener());
button_next = (Button) findViewById(R.id.button_next);
button_next.setOnClickListener(new MyButtonListener());
listview = (ListView) findViewById(android.R.id.list);
text_pageNo=(TextView)findViewById(R.id.text_page);
}
//button的监听类
class MyButtonListener implements android.view.View.OnClickListener{
@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch(v.getId()){case R.id.button_add:insert();break;case R.id.button_delete:delete();break;case R.id.button_update:update();break;case R.id.button_query:query(pageNo);break; case R.id.button_next: pageNo++; if(pageNo>pageCount){ pageNo=1; } query(pageNo); break; case R.id.button_last: pageNo--; if(pageNo<1){ pageNo=pageCount; } query(pageNo); break;}} } //设置显示的页码 private void setpageNo(int pageNo){ String text=null; text=pageNo+"/"+pageCount; text_pageNo.setText(text); } //删除数据 private void delete(){ text_id=edit_id.getText().toString().trim(); String sql="id="+text_id; try{ msqliteDatabase=mMysqLiteOpenHelper.getWritableDatabase(); msqliteDatabase.delete(TableName,sql,null); }catch(Exception e){e.getMessage();System.out.println("修改出错");} } //修改数据 private void update(){ text_id=edit_id.getText().toString().trim(); text_age=edit_age.getText().toString().trim(); text_name=edit_name.getText().toString().trim(); String where="id="+text_id; ContentValues values=new ContentValues(); values.put("id",text_id); values.put("age",text_age); values.put("name",text_name); try{ msqliteDatabase=mMysqLiteOpenHelper.getWritableDatabase(); msqliteDatabase.update(TableName,values,where,null); }catch(Exception e){e.getMessage();System.out.println("修改出错");} } //添加数据 private void insert(){ text_id=edit_id.getText().toString().trim(); text_age=edit_age.getText().toString().trim(); text_name=edit_name.getText().toString().trim(); msqliteDatabase=mMysqLiteOpenHelper.getWritableDatabase(); ContentValues values=new ContentValues(); values.put("id",text_name); try{ msqliteDatabase.insert(TableName,values); }catch(Exception e){e.getMessage();System.out.println("插入出错");} } private void setAdapter(Cursor cur){ /*Cursor cur=null; cur=getDate();*/ if(cur!=null){ adapter=new SimpleCursorAdapter(zuoyesqllite.this,R.layout.user,cur,new String[]{"_id","id","name","age"},new int[]{R.id._id,R.id.userid,R.id.username,R.id.userage}); listview.setAdapter(adapter); }} //查询数据 private void query(int pageNo){ msqliteDatabase=mMysqLiteOpenHelper.getReadableDatabase(); Cursor cursor=null; if(isfirst){ cursor= msqliteDatabase.query(TableName,null); pageCount=cursor.getCount()/limit+1; isfirst=false; } cursor= msqliteDatabase.query(TableName,limit*(pageNo-1)+","+limit);//"5,9",标志从第下标为5的行开始,返回9行数据 System.out.println(limit*(pageNo-1)+"--=--"+limit*pageNo); s setpageNo(pageNo); setAdapter(cursor); }}
原文链接:https://www.f2er.com/sqlite/202630.html