Okay folks!! May be this question is asked a hell lot of times but still i am asking in new way I believe.
I am downloading data from a website using JSOUP parsing and stored it in a arraylist (also loaded in recyclerview). It works perfectly when I am online. For offline I have done this so far:
--> converted the arraylist into json using gson.
--> stored the converted in sqlite database (in the json format).
I have multiple columns in a table where multiple json string array is stored. Now I want to do following things:
--> Retrieve the contents from database (from multiple columns) and again convert it into arraylist so that I can load it into Recyclerview.
--> I want to load these contents in recyclerview on app start and on some other actions.
What could be the best way to do that, if any one can explain me. Thanks in advanced.
My codes so far:
NewsDatabaseAdapter.java
public class NewsDatabaseAdapter {
NewsDatabase dbhelper;
int pos;
public NewsDatabaseAdapter(Context context, int posi) {
dbhelper = new NewsDatabase(context);
this.pos = posi;
}
public long insertData(String titles, String dates) {
SQLiteDatabase db = dbhelper.getWritableDatabase();
ContentValues values = new ContentValues();
db.execSQL("DELETE FROM " + dbhelper.TABLE_NEWS);
db.execSQL("VACUUM");
if (pos == 1) {
values.put(dbhelper.TITLES, titles);
values.put(dbhelper.DATE, dates);
} else if (pos == 2) {
values.put(dbhelper.MYAGDI_TITLES, titles);
values.put(dbhelper.MYAGDI_DATE, dates);
}
long id = db.insert(dbhelper.TABLE_NEWS, null, values);
return id;
}
public String getData() {
SQLiteDatabase db = dbhelper.getWritableDatabase();
String[] column = new String[0];
if (pos == 1) {
column = new String[]{dbhelper.ID, dbhelper.TITLES, dbhelper.DATE};
} else if (pos == 2) {
column = new String[]{dbhelper.ID, dbhelper.MYAGDI_TITLES, dbhelper.MYAGDI_DATE};
}
Cursor cursor = db.query(dbhelper.TABLE_NEWS, column, null, null, null, null, null);
StringBuffer buffer = new StringBuffer();
while (cursor.moveToNext()) {
int cid = cursor.getInt(0);
String title = cursor.getString(1);
String date = cursor.getString(2);
buffer.append(cid + " " + title + " " + date + "\n");
}
return buffer.toString();
}
static class NewsDatabase extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 4;
private static final String DATABASE_NAME = "dbOffline";
private static final String TABLE_NEWS = "newsTable";
private static final String ID = "id";
private static final String TITLES = "newsTitles";
private static final String DATE = "newsDates";
private static final String MYAGDI_TITLES = "myagdiTitles";
private static final String MYAGDI_DATE = "myagdiDates";
public NewsDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE = "CREATE TABLE " + TABLE_NEWS + "("
+ ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ TITLES + " TEXT," + DATE + " TEXT,"
+ MYAGDI_TITLES + " TEXT," + MYAGDI_DATE + " TEXT);";
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NEWS);
onCreate(db);
}
public void addNews(String titles) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(TITLES, titles);
}
}
}
..........................To insert and extract contents from Database...............................
String titles = gson.toJson(mTitles);
String dates = gson.toJson(mDates);
dbHelper = new NewsDatabaseAdapter(mContext, posi);
long id = dbHelper.insertData(titles, dates);
if (id < 0) {
Log.d("DATABASE", "No Success");
} else {
Log.d("DATABASE", "Success");
}
String data = dbHelper.getData();
Log.d("DATAadsaf", data);
Aucun commentaire:
Enregistrer un commentaire