当前位置:Gxlcms > 数据库问题 > sqlite实现新闻收藏和取消收藏

sqlite实现新闻收藏和取消收藏

时间:2021-07-01 10:21:17 帮助过:2人阅读

1 private static final String DBNAME="news.db";
 2 private static final int VERSION=3;  //设置版本号
 3 private static final String TBL_DETAILNEWS="news";   //创建表名为news的表
 4     private static final String TBL_DETAILNEWS_COLUMN_TITLE="_title"; 
 5     private static final String TBL_DETAILNEWS_COLUMN_URL="_url";
 6     private static final String TBL_DETAILNEWS_COLUMN_DOCID="_docid";
 7     private static final String TBL_DETAILNEWS_COLUMN_STATE="_state";
 8 
 9     public NewsDBHelper(Context context){
10         super(context,DBNAME,null,VERSION);
11     }
12 
13     @Override
14     public void onCreate(SQLiteDatabase db) {
15         // TODO Auto-generated method stub
16         StringBuffer sb=new StringBuffer();
17         sb.append("create table if not exists ");
18         sb.append(TBL_DETAILNEWS+"(");
19         sb.append(TBL_DETAILNEWS_COLUMN_DOCID +" varchar(100) primary key ,");   //设置主键
20         sb.append(TBL_DETAILNEWS_COLUMN_TITLE+ " varchar(100) ,");
21         sb.append(TBL_DETAILNEWS_COLUMN_URL+" varchar(100) ,");
22         sb.append(TBL_DETAILNEWS_COLUMN_STATE+" integer ");
23         sb.append(")");
24         db.execSQL(sb.toString());
25 
26     }
27 @Override
28     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
29         String sql2="drop table if exists "+TBL_DETAILNEWS;
30         db.execSQL(sql2);    //创建
31         onCreate(db);
32     }

   Android提供了一个名为SQLiteDatabase的类,它封装了一些操作数据库的API。使用它能实现基本的CRUD操作,通过getWritableDatabase()和getReadableDatabase()可以获取数据库实例,便可以写一些dao层的方法来进行对表的操作:

public class DetailNewsDao {
    private NewsDBHelper helper;
    public DetailNewsDao(Context context){    
        helper=new NewsDBHelper(context);   //与数据库建立连接
    }
    //插入数据
    public void insertDetsilNews(News news){
        SQLiteDatabase db=helper.getWritableDatabase();  
        db.execSQL("insert into news(_title,_url,_docid)" +  //将要收藏新闻的标题title,标识docid,详细地址url传入数据库,便可以依此打开新闻详显
                "values(?,?,?)",new String[]{news.getTitle(),news.getDetailUrl(),news.getDocid()});
        db.close();
    }
    //删除数据
    public void del(String docid){   //根据传入参数docid删除数据
        SQLiteDatabase db=helper.getReadableDatabase();
        db.execSQL("delete from news where _docid = ?",new Object[]{docid});
        db.close();
    }

    //查询数据
    public List<News> findSelected(){
        SQLiteDatabase db=helper.getReadableDatabase();
        Cursor c=db.rawQuery("select * from news", null);    //只有对数据进行查询时,才用rawQuery(),增、删、改和建表,都用execSQl()
        List<News> list=new ArrayList<News>();
        while(c.moveToNext()){
            News news=new News();
            news.setTitle(c.getString(c.getColumnIndex("_title")));
            news.setDetailUrl(c.getString(c.getColumnIndex("_url")));
            news.setDocid(c.getString(c.getColumnIndex("_docid")));
            list.add(news);
        }
        c.close();
        db.close();
        return list;
    }
}

 技术分享

用actionbar做好菜单按钮,点击收藏,执行代码 :

if(item.getTitle().equals("收藏")){    
                Toast.makeText(this,"收藏成功", Toast.LENGTH_LONG).show();
                detailNewsDao.insertDetsilNews(news);
                item.setTitle("取消收藏");
            }else{
                detailNewsDao.del(news.getDocid());
                Toast.makeText(this,"取消收藏", Toast.LENGTH_LONG).show();
                item.setTitle("收藏");
            }

这样整个收藏,取消收藏就做好了。

 

sqlite实现新闻收藏和取消收藏

标签:

人气教程排行