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

阅读更多
个人分类: @android开发经验
上一篇android开发之上传图片和调用相机以及相册
下一篇Ubuntu SVN命令大全
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭