On the CNN Android App, there is a feature for saving a news content/story to the phone, so that the user can access these saved stories when not connected to the internet (ie, in an offline mode).. I am also trying to do something similar on my android news app... please how do I go about this
Asked
Active
Viewed 346 times
1 Answers
0
Ok, lets do that )
1. You should create MySQLiteLoader.class
that will be extend SQLiteOpenHelper
to save and load newsItems
.
2. SQLite database contains columns such : Title, subtitle, description, image (BLOB), other.
3. Add saveSingleNew()
method. FeedItem.class
- example class that will contains your news.
Example :
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();
}
4. Create getAllNews()
method.
Example :
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;
}
5. Make an MyCustomAdapter(ArrayList<FeedItems>);
to show our news in list.
Example: Custom list adapter
Use getAllNews()
method to get your saved news array list into custom adapter.
Add SAVE
button.
6. Enjoy your app )

Community
- 1
- 1

Max Vitruk
- 413
- 3
- 8