Android浏览器— —书签\历史

本博客数据库设计参考于:http://blog.csdn.net/ddddpow/article/details/10317091

书签功能
首先,我们先定义好书签接口

public interface BookmarkController {

    //添加书签
    public boolean addBookmark(SQLiteDatabase db, String title, String url);

    //删除书签
    public boolean deleteBookmark(SQLiteDatabase db, String id);

    //修改书签
    public boolean modifyBookmark(SQLiteDatabase db, String title, String url, String id);

    //查询所有书签
    public Cursor getAllBookmark(SQLiteDatabase db);

    //判断是否书签是否车重复,根据URL
    public boolean checkBookmark(SQLiteDatabase db, String url);

    //开始事务
    void transactionAround(boolean readOnly, Callback callback);
}
public interface Callback {
        public void doSomething(SQLiteDatabase sqLiteDatabase);
}

接下来我们开始实现接口,即调用接口,会调用下面对应的方法

public class BookmarkModel extends SQLiteOpenHelper implements BookmarkController{

    private BookmarkController  deHelper;

    private static final String TAG = "BookmarkModel";
    public static final String CREATE_BOOKMARK = "create table bookmark("
            + "id integer primary key autoincrement,"
            + "title text,"
            + "url text)";

    public BookmarkModel(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //创建数据库时,创建表
        db.execSQL(CREATE_BOOKMARK);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {

    }

    @Override
    public boolean addBookmark(SQLiteDatabase db, String title, String url) {

        ContentValues bookmark = new ContentValues();
        bookmark.put("title", title);
        bookmark.put("url", url);
        long result = db.insert("bookmark", null, bookmark);
        if(result!=-1){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean deleteBookmark(SQLiteDatabase db, String id) {
        int result = db.delete("bookmark", "id = ?", new String[]{ id });
        if(result!=0){
            return true;
        }else{
            return false;
        }

    }

    @Override
    public boolean modifyBookmark(SQLiteDatabase db, String title, String url, String id) {
        ContentValues bookmark = new ContentValues();
        bookmark.put("title", title);
        bookmark.put("url", url);
        int number = db.update("bookmark", bookmark, "id = ?", new String[]{id});
        if(number!=0){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public Cursor getAllBookmark(SQLiteDatabase db) {
        String[] returnColmuns = new String[]{
                "id as _id",
                "title",
                "url"
        };

        Cursor bookmark = db.query("bookmark", returnColmuns, null, null, null, null, "id");
        while(bookmark.moveToNext()){
            String id = String.valueOf(bookmark.getInt(bookmark.getColumnIndex("_id")));
            String title = bookmark.getString(bookmark.getColumnIndex("title"));
            String url = bookmark.getString(bookmark.getColumnIndex("url"));
            Log.v(TAG, "id:"+id+",title:"+title+",url:"+url);
        }
        return bookmark;
    }

    @Override
    public boolean checkBookmark(SQLiteDatabase db, String url) {
        Cursor result = db.query("bookmark",null, "url=?", new String[]{url},null, null,null);
        if(result.getCount()>0){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public void transactionAround(boolean readOnly, Callback callback) {
        SQLiteDatabase db = null;
        db = getWritableDatabase();
        db.beginTransaction();
        callback.doSomething(db);
        db.setTransactionSuccessful();
        db.endTransaction();

    }
}
这里写代码片

你可能感兴趣的:(界面设计)