Android学习之SQLite分页查询
MySQLiteDemo.java:
import android.app.Activity; import android.database.sqlite.SQLiteOpenHelper; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.LinearLayout; import android.widget.ListView; public class MySQLiteDemo extends Activity { private SQLiteOpenHelper helper = null ; // 数据库操作 private Button findBut = null ; // 定义按钮 private LinearLayout mylayout = null ; // 定义布局管理器 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 父类onCreate() super.setContentView(R.layout.main); // 默认布局管理器 this.findBut = (Button) super.findViewById(R.id.findBut) ; // 取得组件 System.out.println("**" + super.findViewById(R.id.mylayout).getClass()) ; this.mylayout = (LinearLayout) super.findViewById(R.id.mylayout) ; // 取得组件 this.helper = new MyDatabaseHelper(this) ; // 定义数据库辅助类 this.findBut.setOnClickListener(new OnClickListenerImpl()) ; // 设置监听 } private class OnClickListenerImpl implements OnClickListener { @Override public void onClick(View view) { MySQLiteDemo.this.helper = new MyDatabaseHelper(MySQLiteDemo.this) ; ListView listView = new ListView(MySQLiteDemo.this) ; // 定义ListView listView.setAdapter(new ArrayAdapter<String>(MySQLiteDemo.this, // 将数据包装 android.R.layout.simple_list_item_1, // 每行显示一条数据 new MytabCursor(MySQLiteDemo.this.helper .getReadableDatabase()).find())); // 设置显示数据 MySQLiteDemo.this.mylayout.addView(listView) ; // 追加组件 } } }
MytabCursor.java:
import java.util.ArrayList; import java.util.List; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; public class MytabCursor { private static final String TABLENAME = "mytab" ; // 数据表名称 private SQLiteDatabase db = null ; // SQLiteDatabase public MytabCursor(SQLiteDatabase db) { // 构造方法 this.db = db ; // 接收SQLiteDatabase } public List<String> find() { // 查询数据表 List<String> all = new ArrayList<String>() ; // 定义List集合 int currentPage = 1 ; // 当前页 int lineSize = 5 ; // 没页显示5条 String keyWord = "中国" ; // 查询关键字 String sql = "SELECT id,name,birthday FROM " + TABLENAME + " WHERE (name LIKE ? OR birthday LIKE ?)" + " LIMIT ?,? " ; // 查询SQL String selectionArgs[] = new String[] { "%" + keyWord + "%", "%" + keyWord + "%", String.valueOf((currentPage - 1) * lineSize), String.valueOf(lineSize) }; // 查询参数 Cursor result = db.rawQuery(sql, selectionArgs); // 查询 for (result.moveToFirst(); !result.isAfterLast(); result.moveToNext()) { all.add("【" + result.getInt(0) + "】" + " " + result.getString(1) + "," + result.getString(2)); // 设置集合数据 } this.db.close() ; // 关闭数据库连接 return all ; } }
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28