结构
1:Android中数据库的创建
2:ListView的优化
3:数据库的分页查询以及ListView分页展示
1:Android中数据库的创建
1:写类继承SQLiteOpenHelper
public class BlackNamedb extends SQLiteOpenHelper{
public BlackNamedb(Context context) {
super(context, "blackname.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
//执行SQL语句
sqLiteDatabase.execSQL("create table blackname" +
"(_id integer primary key autoincrement,phone varchar(20),mode integer);");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i2) {
}
}
2:重新写类私有化构造方法
3:申明一个静态的当前类对象
3:提供一个静态方法如果当前类的对象为空则要创建一个新的对象
public class BlacknameJDBC {
private final BlackNamedb blackNamedb;
//1:私有化构造方法
private BlacknameJDBC(Context context) {
blackNamedb = new BlackNamedb(context);
}
//2:申明当前类的对象。必须是静态的
private static BlacknameJDBC blacknameJDBC = null;
//3:提供一个静态方法如果当前类的对象为空则要创建一个新的对象
public static BlacknameJDBC getInstance(Context context){
if (blacknameJDBC==null){
blacknameJDBC = new BlacknameJDBC(context);
return blacknameJDBC;
}else {
return blacknameJDBC;
}
}
4:提供增删改查等操作
/**
*
* @param phone 拦截的电话
* @param mode 拦截的类型 1是短信 2是电话 3是所有
*/
public void insert(String phone , String mode){
//开启数据库做写入操作
SQLiteDatabase writableDatabase = blackNamedb.getWritableDatabase();
//准备ContentValues 封装
ContentValues values = new ContentValues();
values.put("phone",phone);
values.put("mode",mode);
//进行写入操作 方法接收参数(表的名称,没有给值时,将null插入数据库,内容 ContentValues 用这个封装);
writableDatabase.insert("blackname",null,values);
blackNamedb.close();
}
/**
*
* @param phone 想要删除的电话号码
*/
public void delect(String phone){
SQLiteDatabase writableDatabase = blackNamedb.getWritableDatabase();
//进行删除操作(表的名称,要删除的字段名称,要删除的字段值,要使用String类型的数组)
writableDatabase.delete("blackname","phone=?",new String[]{phone});
writableDatabase.close();
}
/**
* 根据电话号码去更新拦截模式
* @param phone 要更新的电话
* @param mode 要更新为的模式(1:短信 2:电话 3:拦截所有(短信+电话)
*/
public void updata(String phone,String mode){
SQLiteDatabase writableDatabase = blackNamedb.getWritableDatabase();
//准备数据
ContentValues values = new ContentValues();
values.put("mode",mode);
// ava.lang.String table, android.content.ContentValues values, java.lang.String whereClause, java.lang.String[] whereArgs
//进行更新操作(表的名称,要更新的值,要更新的字段,依据什么字段更新)
writableDatabase.update("blackname",values,"mode=?",new String[]{phone});
writableDatabase.close();
}
/**
* 返回的就是数据库中所有的号码以及类型
*/
public List<Blacknamebin> FindAll(){
SQLiteDatabase writableDatabase = blackNamedb.getWritableDatabase();
//查询(表名,查询的字段,查询的条件,查询的条件,查询的条件,查询的条件, 查询出来排序的方法 默认为正序,desc为倒叙)
//拿到游标
Cursor query = writableDatabase.query("blackname", new String[]{"phone", "mode"}, null, null, null, null, "_id desc");
//创建一个集合
List<Blacknamebin> blackname = new ArrayList<Blacknamebin>();
while (query.moveToNext()){
//生成一个BIN类放常用字段
Blacknamebin blacknamebin = new Blacknamebin();
blacknamebin.phone = query.getString(0);
blacknamebin.mode = query.getString(1);
System.out.println("blacknamebin.phone"+query.getString(0)+"````````````blacknamebin.mode"+query.getString(1));
blackname.add(blacknamebin);
}
query.close();
writableDatabase.close();
return blackname;
}
public List<Blacknamebin> FindOther(int index){
SQLiteDatabase writableDatabase = blackNamedb.getWritableDatabase();
Cursor query = writableDatabase.rawQuery("select phone,mode from blackname order by _id desc limit ?,10;", new String[]{index+""});
List<Blacknamebin> blackname = new ArrayList<Blacknamebin>();
while (query.moveToNext()){
//生成一个BIN类放常用字段
Blacknamebin blacknamebin = new Blacknamebin();
blacknamebin.phone = query.getString(0);
blacknamebin.mode = query.getString(1);
System.out.println("blacknamebin.phone"+query.getString(0)+"````````````blacknamebin.mode"+query.getString(1));
blackname.add(blacknamebin);
}
query.close();
writableDatabase.close();
return blackname;
}
public int getCount(){
SQLiteDatabase writableDatabase = blackNamedb.getWritableDatabase();
Cursor cursor = writableDatabase.rawQuery("select count(*) from blackname;", null);
int count =0 ;
if (cursor.moveToNext()){
count = cursor.getInt(0);
}
cursor.close();
writableDatabase.close();
return count;
}
}
2:ListView的优化
listView优化
1:复用convertView
2:复用ViewHolder让其减少findViewById()次数
3:static ViewHolder
4:分页算法
模版代码
ViewHolder viewHolder = null;
if(convertView == null){
viewHolder = new ViewHolder();
convertView = View.inflate();
viewHolder.text1 = convertView.findViewById(R.id.text1);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder)convertView.getTag();
}
viewHolder.text1.setText("文本内容");
static class ViewHolder{
TextView text1;
}
3:数据库的分页查询以及ListView分页展示
1:数据库查询指定数据写法
public List<Blacknamebin> FindOther(int index){
SQLiteDatabase writableDatabase = blackNamedb.getWritableDatabase();
Cursor query = writableDatabase.rawQuery("select phone,mode from blackname order by _id desc limit ?,10;", new String[]{index+""});
List<Blacknamebin> blackname = new ArrayList<Blacknamebin>();
while (query.moveToNext()){
//生成一个BIN类放常用字段
Blacknamebin blacknamebin = new Blacknamebin();
blacknamebin.phone = query.getString(0);
blacknamebin.mode = query.getString(1);
blackname.add(blacknamebin);
}
query.close();
writableDatabase.close();
return blackname;
}
2:监听ListView的滚动事件重写里面的onScrollStateChanged方法
lv_black_name.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView absListView, int i) {
//滚动过程中状态发生改变
//SCROLL_STATE_IDLE 空闲状态
//SCROLL_STATE_TOUCH_SCROLL 拿手触摸着滚动滚动状态
// SCROLL_STATE_FLING 飞速滚动
条件:
//1:LV不能滚动
//2:最后一个条目可见(最后一个条目的索引值大于数据适配器中条目值-1)
//3:mIsload 这个是为了不重复加载
if (blacknamebins != null) {
if (i == AbsListView.OnScrollListener.SCROLL_STATE_IDLE &&
lv_black_name.getLastVisiblePosition() >= blacknamebins.size() - 1 &&
!mIsload)
{
//mCount返回的是数据库中总条目的个数
if (mCount>blacknamebins.size()){
//开启线程加载数据
new Thread(){
@Override
public void run() {
//获取数据库查询对象
blacknameSelect = BlacknameJDBC.getInstance(Blackname_Activity.this);
List<Blacknamebin> moreData = blacknameSelect.FindOther(blacknamebins.size());
blacknamebins.addAll(moreData);
mHandler.sendEmptyMessage(0);
super.run();
}
}.start();
}
}
}
}
@Override
public void onScroll(AbsListView absListView, int i, int i2, int i3) {
//滚动过程中调用的方法
}
});
3:通知数据适配器实时刷新调用API
if (mAdapter!=null){
mAdapter.notifyDataSetChanged();
}