我已经在网络上搜索了类似的问题,就像我想要的那样,但我找不到任何问题。 所以,我创建了一个应用程序,我从JSON获取新闻数据并将其显示在列表中,它一切都很好,但现在我想添加一些东西,当没有互联网连接时我想显示最新的列表已查看。为了做到这一点,我想到将JSON arraylist存储在数据库中,并在新闻发生变化时更新它,同时进行连接,这样当没有连接时,它会在数据库中显示数据。 问题是我在如何做到这一点上迷失了......
我创建了一个名为NewsDBHelper的java类,我创建了我的数据库和表。
public class NewsDBHelper extends SQLiteOpenHelper {
public static final String LOG_TAG = NewsDBHelper.class.getSimpleName();
private static final String DATABASE_NAME = "news.db";
private static final int DATABASE_VERSION = 1;
public NewsDBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String SQL_CREATE_PRODUCTS_TABLE = "CREATE TABLE " + Contract.NewsEntry.TABLE_NAME + " ("
+ Contract.NewsEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ Contract.NewsEntry.COLUMN_SECTION + " TEXT NOT NULL, "
+ Contract.NewsEntry.COLUMN_TITLE + " TEXT NOT NULL, "
+ Contract.NewsEntry.COLUMN_DATE + " BLOB, "
+ Contract.NewsEntry.COLUMN_AUTHOR + " TEXT DEFAULT NULL, "
+ Contract.NewsEntry.COLUMN_URL + " BLOB);";
Log.v(LOG_TAG,SQL_CREATE_PRODUCTS_TABLE);
db.execSQL(SQL_CREATE_PRODUCTS_TABLE);
}
然后在我看来,这个想法是当JSON数据存储在一个数组中时,我将获取数组值并将它们存储在表中。
private static List<News> extractFeatureFromJson(String newsJSON) {
if (TextUtils.isEmpty(newsJSON)) {
return null;
}
List<News> news = new ArrayList<>();
try {
JSONObject baseJsonResponse = new JSONObject(newsJSON);
String response = baseJsonResponse.getString("response");
JSONObject object = new JSONObject(response);
;
JSONArray newsArray = object.getJSONArray("results");
for (int i = 0; i < newsArray.length(); i++) {
JSONObject currentNews = newsArray.getJSONObject(i);
String title = currentNews.getString("webTitle");
String section = currentNews.getString("sectionName");
String date = currentNews.getString("webPublicationDate");
JSONArray tag = currentNews.getJSONArray("tags");
String author = null;
if (tag.length() != 0) {
author = tag.getJSONObject(0).getString("firstName");
}
String url = currentNews.getString("webUrl");
News nNews = new News(title, date, section, author, url);
news.add(nNews);
//add news array in the table
}
} catch (JSONException e) {
Log.e("QueryUtils", "Problem parsing the news JSON results", e);
}
return news;
}
我真的不知道如何做到这一点,或者它是否可能。如果它是我怎么做的?或者什么是实现我的想法的最佳方式?
答案 0 :(得分:0)
你完成了70%的工作。
您现在需要做的就是:
以下是采用上述步骤的示例代码。
//Create an instance of NewsDBHelper
NewsDBHelper newsDBHelper = new NewsDBHelper(context);
//get a writable database from newsDBHelper
SQLiteDatabase writableDatabase = newsDBHelper.getWritableDatabase();
//create content values of data to persist
ContentValues values = new ContentValues();
values.put("section","Sample section");
values.put("title","Sample title");
values.put("date","1/29/2018");
values.put("author","John Chan");
values.put("url","https://example.com");
//write the data to the database. Notice that the "news" is the table name
writableDatabase.insert("news",null,values);
要从数据库中获取数据,请从newsDBHelper获取可读数据库,如下所示:
SQLiteDatabase readableDatabase = newsDBHelper.getReadableDatabase();
//query or read data from the database by calling:
readableDatabase.query(String table, String[] columns, String selection,
String[] selectionArgs,String groupBy, String having,
String orderBy, String limit);
有关SQLiteOpenHelper,Readable和Writable Databases的更多信息,请查看官方文档SQLiteDatabase