CNN Android应用程序上的save stories是如何工作的?

CNN Android应用程序上的save stories是如何工作的?,android,saving-data,Android,Saving Data,在CNN Android应用程序上,有一项功能可以将新闻内容/故事保存到手机上,以便用户在未连接到互联网时(即,在脱机模式下)可以访问这些保存的故事。。我也尝试在我的android新闻应用程序上做类似的事情。。。请告诉我该怎么做(好的,我们就这么做吧) 1。您应该创建MySQLiteLoader.class,该类将被扩展SQLiteOpenHelper以保存和加载新闻项 2.SQLite数据库包含以下列:标题、副标题、描述、图像(BLOB)和其他 3.添加saveSingleNew()方法Fee

在CNN Android应用程序上,有一项功能可以将新闻内容/故事保存到手机上,以便用户在未连接到互联网时(即,在脱机模式下)可以访问这些保存的故事。。我也尝试在我的android新闻应用程序上做类似的事情。。。请告诉我该怎么做(好的,我们就这么做吧)

1。您应该创建
MySQLiteLoader.class
,该类将被扩展
SQLiteOpenHelper
以保存和加载
新闻项

2.SQLite数据库包含以下列:标题、副标题、描述、图像(BLOB)和其他

3.添加
saveSingleNew()
方法
FeedItem.class
-包含新闻的示例类

例如:

public void addSite(FeedItem site){

    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_TITLE, site.getTitle()); // title
    values.put(KEY_SUB_TITLE, site.getSubTitle()); // subtitle
    values.put(KEY_DATE, site.getDate()); // date
    values.put(KEY_IMG, site.getImg()); // img to byte[]
    values.put(KEY_DESCRIPTION, site.getSubTitle()); //  description

    db.close();
}
public List<FeedItem> getAllNews() {

    List<FeedItem> items = new ArrayList<>();


    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_NEWS, null, null, null, null, null, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            FeedItem item = new FeedItem();
            item.setItemId(Integer.parseInt(cursor.getString(0))); //Item id.
            item.setTitle(cursor.getString(1));
            item.setSubTitle(cursor.getString(2));
            item.setDate( cursor.getString(3));
            byte [] b = cursor.getBlob(1);
            Drawable image = new BitmapDrawable(BitmapFactory.decodeByteArray(b, 0, b.length));
            item.setDrawableId(image);
            item.setImageBlob(cursor.getString(4));
            item.setDescription(cursor.getString(5)); // description.

            items.add(item);
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();

    // return news list
    return items;

}
4.创建
getAllNews()
方法

例如:

public void addSite(FeedItem site){

    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_TITLE, site.getTitle()); // title
    values.put(KEY_SUB_TITLE, site.getSubTitle()); // subtitle
    values.put(KEY_DATE, site.getDate()); // date
    values.put(KEY_IMG, site.getImg()); // img to byte[]
    values.put(KEY_DESCRIPTION, site.getSubTitle()); //  description

    db.close();
}
public List<FeedItem> getAllNews() {

    List<FeedItem> items = new ArrayList<>();


    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_NEWS, null, null, null, null, null, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            FeedItem item = new FeedItem();
            item.setItemId(Integer.parseInt(cursor.getString(0))); //Item id.
            item.setTitle(cursor.getString(1));
            item.setSubTitle(cursor.getString(2));
            item.setDate( cursor.getString(3));
            byte [] b = cursor.getBlob(1);
            Drawable image = new BitmapDrawable(BitmapFactory.decodeByteArray(b, 0, b.length));
            item.setDrawableId(image);
            item.setImageBlob(cursor.getString(4));
            item.setDescription(cursor.getString(5)); // description.

            items.add(item);
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();

    // return news list
    return items;

}
public List getAllNews(){
列表项=新建ArrayList();
SQLiteDatabase db=this.getReadableDatabase();
Cursor Cursor=db.query(表_NEWS,null,null,null,null,null);
//循环遍历所有行并添加到列表
if(cursor.moveToFirst()){
做{
FeedItem=新的FeedItem();
item.setItemId(Integer.parseInt(cursor.getString(0));//项id。
item.setTitle(cursor.getString(1));
item.setSubTitle(cursor.getString(2));
item.setDate(cursor.getString(3));
字节[]b=游标.getBlob(1);
可绘制图像=新的BitmapDrawable(BitmapFactory.decodeByteArray(b,0,b.length));
item.setDrawableId(图像);
item.setImageBlob(cursor.getString(4));
item.setDescription(cursor.getString(5));//说明。
项目。添加(项目);
}while(cursor.moveToNext());
}
cursor.close();
db.close();
//返回新闻列表
退货项目;
}
5.制作一个
MyCustomAdapter(ArrayList)在列表中显示我们的新闻

例如:

使用
getAllNews()
方法将保存的新闻数组列表放入自定义适配器

添加
保存
按钮

6.享受你的应用程序)