该应用程序运行良好,但是方法Cursor.moveToFirst()
开始返回false,并且该应用程序的列表已不再出现在Fragment中。
我认为这是在我将constat的名称从IMAGE_URI
更改为IMAGE_URL
,并将imageUri
更改为imageUrl
之后开始的。不确定。
我尝试调试和搜索类似的情况,但是原因很多。调试时,我无法进入onCreate
类的SQLiteHelper
来找出表填充期间发生的情况(似乎该表存在,因为该表的空db正在创建),所以我无法调试也无法搜索类似的问题,因为在Logcat
中找不到相关的输出。
GetDatabase
该类用于与SQLiteOpenHelper
类进行某些通信。
public class GetDatabase {
private static final int DB_VERSION = 1; //required for the constructor
public static final String dbName = "moviesList";
private SQLiteOpenHelper sqLiteOpenHelper;
private SQLiteDatabase db ;
public GetDatabase(Context context) {
Log.d("GetDatabase", "Cont.");
this.sqLiteOpenHelper = new SQLiteHelper(context, dbName, null, DB_VERSION);
}
public void open() {
db = sqLiteOpenHelper.getWritableDatabase();
}
public void close() {
if (sqLiteOpenHelper != null) {
sqLiteOpenHelper.close();
}
}
public ArrayList<Movie> getMovies() {
String[] columns = {
Movie.ID,
Movie.TITLE,
Movie.IMAGE_URL,
Movie.RATING,
Movie.RELEASE_YEAR,
Movie.GENRE
};
// sorting orders
String sortOrder =
Movie.RELEASE_YEAR + " ASC";
ArrayList<Movie> moviesList = new ArrayList<>();
Cursor cursor = db.query(TABLE_NAME, //Table to query
columns,
null,
null,
null,
null,
sortOrder);
if (cursor.moveToFirst()) {
do {
Movie movie = new Movie();
movie.setMovieId(Integer.parseInt(cursor.getString(cursor.getColumnIndex(Movie.ID))));
movie.setTitle(cursor.getString(cursor.getColumnIndex(Movie.TITLE)));
movie.setImageUrl(cursor.getString(cursor.getColumnIndex(Movie.IMAGE_URL)));
movie.setRating(cursor.getDouble(cursor.getColumnIndex(Movie.RATING)));
movie.setReleaseYear(cursor.getInt(cursor.getColumnIndex(Movie.RELEASE_YEAR)));
movie.setGenre(cursor.getString(cursor.getColumnIndex(Movie.GENRE)));
// Adding a movie to the list
moviesList.add(movie);
} while (cursor.moveToNext());
}
Log.d(TAG, "The movies list from sqlite: " + moviesList);
cursor.close();
db.close();
return moviesList;
}
private Cursor getData(int id) {
return db.rawQuery( "select * from " + TABLE_NAME + " where id="+id+"", null );
}
public Cursor getMovieDetails(int movieId) {// For viewing details of a specific item
Cursor cursor = getData(movieId);
cursor.moveToFirst();
return cursor;
}
}
SQliteHelper
SQLiteOpenHelper类
public class SQLiteHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "movies.db";
private static final int DB_VERSION = 1; //required for the constructor
private static final String TABLE_NAME = "movies";
private String fileName = "movies.json";
private String appName = "MoviesList";
private String path = getExternalStorageDirectory() + "/" + appName + "/" + fileName;
private String jsonString = null;
private SQLiteDatabase db;
// The cont. for saving to SQLite .Called from SplashActivity after downloading the JSON
public SQLiteHelper(Context context, String dbName, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.d(TAG, "At SQLiteHelper");
createSQLIteTable(db);
if(tableIsEmpty(db)) {
try {
parseJsonAndInsertToSQLIte(db);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
private void createSQLIteTable(SQLiteDatabase db) {
//creating a table for SQLite
String CREATE_SQL_TABLE_STRING = "CREATE TABLE IF NOT EXISTS "+TABLE_NAME
+ " ("
+ Movie.ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL ,"
+ Movie.TITLE + " TEXT,"
+ Movie.IMAGE_URL + " TEXT,"
+ Movie.RATING + " TEXT,"
+ Movie.RELEASE_YEAR + " TEXT,"
+ Movie.GENRE + " TEXT "
+ ")";
Log.i(TAG,"created sql table: "+CREATE_SQL_TABLE_STRING);
db.execSQL(CREATE_SQL_TABLE_STRING);
}
private void parseJsonAndInsertToSQLIte(SQLiteDatabase db) throws JSONException {
// parsing the json
String jsonString = getJsonFileData();
JSONArray moviesArray = new JSONArray(jsonString);
ContentValues insertValues;
for (int i = 0; i < moviesArray.length(); i++) {
JSONObject jsonObject = moviesArray.getJSONObject(i);
String title = jsonObject.getString("title");
String imageUrl = jsonObject.getString("imageUrl");
String rating = jsonObject.getString("rating");
String releaseYear = jsonObject.getString("releaseYear");
String genre = jsonObject.getString("genre");
insertValues = new ContentValues();
insertValues.put(Movie.TITLE, title);
insertValues.put(Movie.IMAGE_URL, imageUrl);
insertValues.put(Movie.RATING, rating);
insertValues.put(Movie.RELEASE_YEAR, releaseYear);
insertValues.put(Movie.GENRE, genre);
long res = db.insert(TABLE_NAME, null, insertValues);
Log.i(TAG, "parsed and inserted to sql - row: " + res);
}
}
private String getJsonFileData() {
//loading the jsonString
try {
InputStream in = new FileInputStream(new File(path));
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder output = new StringBuilder();
while ((jsonString = reader.readLine()) != null) {
output.append(jsonString);
}
System.out.println(output.toString()); //Prints the string content read from input stream
jsonString = output.toString();
Log.d(TAG, "the jsonString was loaded");
} catch (IOException ex) {
ex.printStackTrace();
}
return jsonString;
}
}
如果我不能解决这个问题,我认为整个应用程序一文不值,因为我不知道需要纠正什么错误。
谢谢!