android開發之收藏網頁功能

通過SQLiteOpenHelper結合SharedPreferences

  • “收藏”點擊事件
    首先,將id保存一份到SharedPreferences,便於每次進入當前頁面時,判斷是否已經保存;
    然後,創建FavoritesDao對象,將當前頁面的“_id”,“title”,“typeid”,“time”保存到數據庫當中,便於在收藏界面進行展示;
SharedPreferences sp = this.getSharedPreferences("favorites_show",Context.MODE_PRIVATE);
        editor = sp.edit();

@Override
public void onClick(View v) {
    switch(v.getId()) {
    case R.id.rl_sc: //我的收藏
        if(sp.getString(id,"0").equals(id)) {
            new FavoritesDao(this).delete(id);
            editor.putString(id,"0");
            editor.commit();
            iv_sc.setImageResource(R.drawable.sc_wx);
            tv_sc.setTextColor(getResources().getColor(R.color.color_999));
            Toast.makeText(this,"取消收藏",Toast.LENGTH_SHORT).show();
            return;
        }
        iv_sc.setImageResource(R.drawable.sc);
        tv_sc.setTextColor(getResources().getColor(R.color.color_orange));
        editor.putString(id,id);
        editor.commit();
        sd = sdf.format(new Date());
        new FavoritesDao(this).insert(id,detailEntity.title,title_tag,sd);
        Toast.makeText(this,"收藏成功",Toast.LENGTH_SHORT).show();
        break;
    }
}
  • Dao層
public class FavoritesDao {
    private static final String TABLE = "favoritestable";
    private FavoritesSQLiteOpenHelper openHelper;
    public FavoritesDao(Context context) {
        openHelper = new FavoritesSQLiteOpenHelper(context);
    }

    //增加
    public void insert(String _id,String title,int typeid,String time) {
        SQLiteDatabase db = openHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("_id",_id);
        values.put("title",title);
        values.put("typeid",typeid);
        values.put("time",time);
        db.insert(TABLE, null, values);
    }

    //刪除
    public void delete(String _id) {
        SQLiteDatabase db = openHelper.getWritableDatabase();
        db.delete(TABLE, "_id = ?", new String[]{_id});
    }

    //查詢所有
    public List<Favorites_Entity> queryAllFavorites() {
        SQLiteDatabase db = openHelper.getReadableDatabase();
        String[] columns = {"_id","title","typeid","time"};
        Cursor cursor = db.query(TABLE, columns, null, null, null, null, null);
        if(cursor != null && cursor.getCount() > 0) {
             List<Favorites_Entity> listFavorites = new ArrayList<Favorites_Entity>();
            while(cursor.moveToNext()) {
                String _id = cursor.getString(cursor.getColumnIndex("_id"));
                String title = cursor.getString(cursor.getColumnIndex("title"));
                int typeid = cursor.getInt(cursor.getColumnIndex("typeid"));
                String time = cursor.getString(cursor.getColumnIndex("time"));
                listFavorites.add(new Favorites_Entity(_id,title,typeid,time));
            }
            cursor.close();
            return listFavorites;
        }
        cursor.close();
        return null;
    }
}
  • SQLiteOpenHelper
public class FavoritesSQLiteOpenHelper extends SQLiteOpenHelper {

    public FavoritesSQLiteOpenHelper(Context context){
        this(context,"favorites.db",null,1);
    }

    private FavoritesSQLiteOpenHelper(Context context, String name,CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table favoritestable(_id varchar(20),title varchar(100),typeid integer,time varchar(100))");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}
  • 創建SQLite,一般在Splash界面或者Index界面進行操作
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.index_activity_layout);
    createDB("favorites.db");
}

//創建數據庫
private void createDB(String db_Name) {
    File file = getDatabasePath(db_Name);
    if(file.exists()&&file.length()>0){
    }else{
        FavoritesSQLiteOpenHelper openHelper = new FavoritesSQLiteOpenHelper(this);
        SQLiteDatabase db = openHelper.getReadableDatabase();
    }
}

通過SharedPreferences

發佈了93 篇原創文章 · 獲贊 8 · 訪問量 24萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章