我正在创建一个animeList-app,就像网站myAnimeList。
我的问题是我在我的数据库中显示的数据是“随机的”。有些数据没有显示或不正确,如果我点击一个项目(如动漫“海贼王”)Toast.makeText(myAnime_completed.get(position).getName());将从db中给我一个完全随机的项目。
我有一个databaseHelper,我在其中加载数据。
//CREATE TABLE MYANIME
String CREATE_TABLE_MYANIME = "CREATE TABLE myAnime (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT," +
"name TEXT," +
"description TEXT," +
"episodes INTEGER," +
"genre TEXT," +
"rating INTEGER," +
"episodesWatched INTEGER," +
"stateId INTEGER)";
db.execSQL(CREATE_TABLE_MYANIME);
private void insertMyAnime(SQLiteDatabase db) {
db.execSQL("INSERT INTO myAnime (name, description, episodes, genre, rating, episodesWatched, stateId) VALUES ('One Piece', 'GUUD', 700, 'brut', 0, 50, 2);");
db.execSQL("INSERT INTO myAnime (name, description, episodes, genre, rating, episodesWatched, stateId) VALUES ('Death Note', 'GUUD TUU', 35, 'thriller', 0, 50, 1);");
db.execSQL("INSERT INTO myAnime (name, description, episodes, genre, rating, episodesWatched, stateId) VALUES ('Fairy Tail', 'GUUD TUU', 35, 'thriller', 0, 50, 2);");
db.execSQL("INSERT INTO myAnime (name, description, episodes, genre, rating, episodesWatched, stateId) VALUES ('Naruto', 'GUUD TUU', 35, 'thriller', 0, 50, 1);");
db.execSQL("INSERT INTO myAnime (name, description, episodes, genre, rating, episodesWatched, stateId) VALUES ('Bleach', 'GUUD TUU', 35, 'thriller', 0, 50, 3);");
db.execSQL("INSERT INTO myAnime (name, description, episodes, genre, rating, episodesWatched, stateId) VALUES ('No Game No Life', 'GUUD TUU', 35, 'thriller', 0, 50, 3);");
db.execSQL("INSERT INTO myAnime (name, description, episodes, genre, rating, episodesWatched, stateId) VALUES ('Kill la Kill', 'GUUD TUU', 35, 'thriller', 0, 50, 3);");
}
在此之后,我尝试将数据分成不同的类别:观看,完成,暂停,放弃并计划观看。
public MyAnime getMyAnimeByID(int animeId) {
SQLiteDatabase db = this.getReadableDatabase();
MyAnime myAnime = new MyAnime();
Cursor cursor = db.query("myAnime", new String[] { "id", "name",
"description", "episodes", "genre", "rating",
"episodesWatched", "stateId" }, "id = ?",
new String[] { String.valueOf(animeId) }, null, null, null);
if (cursor.moveToFirst()) {
myAnime = new MyAnime(cursor.getInt(0), cursor.getString(1),
cursor.getString(2), cursor.getInt(3), cursor.getString(4),
cursor.getInt(5), cursor.getInt(6), cursor.getInt(7));
}
cursor.close();
db.close();
return myAnime;
}
public List<MyAnime> getMyAnimes_watching() {
List<MyAnime> myAnimeList = new ArrayList<MyAnime>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query("myAnime",
new String[] { "id" }, "stateId = ?",
new String[] { String.valueOf(1)}, null, null, null);
if (cursor.moveToFirst()) {
do {
myAnimeList.add(getMyAnimeByID(cursor.getInt(0)));
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return myAnimeList;
}
public List<MyAnime> getMyAnimes_completed() {
List<MyAnime> myAnimeList = new ArrayList<MyAnime>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query("myAnime", new String[] { "id" },
"stateId = ?", new String[] { String.valueOf(2)}, null, null,
null);
if (cursor.moveToFirst()) {
do {
myAnimeList.add(getMyAnimeByID(cursor.getInt(0)));
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return myAnimeList;
}
然后在活动中我从db获取数据:
//FILL LISTVIEW COMPLETED
final List<MyAnime> myAnime_completed = db.getMyAnimes_completed();
ArrayAdapter<MyAnime> adapter_completed = new ArrayAdapter<MyAnime>(this, android.R.layout.simple_list_item_1, myAnime_completed);
ListView lvCompleted = (ListView) findViewById(R.id.myAnime_completed);
lvCompleted.setAdapter(adapter_completed);
lvCompleted.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parentView,
View childView, int position, long id) {
show(myAnime_completed.get(position).getName());
//goToNextActivity(myAnime_completed.get(position).getId());
}
});
说实话,我是一名学生,我意识到这可能很容易解决,但对于那些没有多少经验的人来说,这是一个愚蠢的人。
我真的希望有人可以帮助我!
答案 0 :(得分:0)
我假设函数show(...)显示吐司。根据给出的信息,看起来代码是正确的,如果单击按钮OnePiece,它看起来应该返回OnePiece。但是,我需要更多信息,我无法发表评论。我会在getMyAnimeByID上设置一个断点,然后逐步执行代码以确保它返回正确的行。
我想确保列表只显示stateId = 2的值。看起来你也不需要对它进行排序,以回应下面的答案。
这是一个偏离主题的建议:
if (cursor.moveToFirst()) {
do {
myAnimeList.add(getMyAnimeByID(cursor.getInt(0)));
} while (cursor.moveToNext());
}
可以缩减为
while (cursor.moveToNext()) {
myAnimeList.add(...);
}
光标从索引-1开始。
答案 1 :(得分:-1)
创建游标时,您应该在查询中传递“Order By”参数。使用db.query方法,将第二个到最后一个null参数更改为您希望数据排序的列。该方法的签名是:
Cursor query(boolean distinct, String table, String[] columns,
String selection, String[] selectionArgs, String groupBy,
String having, String orderBy, String limit)
因此,您可以传入字符串值“stateId ASC”。