将arraylist存储在数据库中并将其检索以用于离线目的以进行recyclelerview

时间:2015-07-29 02:42:08

标签: java android json sqlite arraylist

好的人!!可能是这个问题被问了很多次,但我仍然以新的方式问我相信。

我正在使用JSOUP解析从网站下载数据并将其存储在arraylist中(也在recyclelerview中加载)。当我上网时它完美无缺。 对于离线我到目前为止已经这样做了:
- >使用gson将arraylist转换为json。
- >存储转换后的sqlite数据库(采用json格式)。

我在表中有多个列,其中存储了多个json字符串数组。现在我想做以下事情:
- >从数据库中检索内容(从多个列中)并再次将其转换为arraylist,以便我可以将其加载到Recyclerview中。
- >我想在app start和其他一些操作中在recyclerview中加载这些内容。

如果有人可以解释我,最好的办法是什么? 谢谢你提前。

我的代码到目前为止:
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);

    }
}
}

..........................从数据库插入和提取内容.............. .................

            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);

0 个答案:

没有答案