I am making an animeList-app, like the website myAnimeList.
My problem is that the data that I show in the app from my db is "random". Some data doesn't show up or isn't right and if I click on an item (like the anime "One Piece") Toast.makeText(myAnime_completed.get(position).getName()); will give me a completely random item from the db.
I have a databaseHelper where I load data.
//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);");
}
After this I try to seperate the data in different catagories: watching, completed, on hold, dropped and plan to watch.
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;
}
Then in the activity I get the data from the 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());
}
});
To be honest, I'm a student and I realise that this could be very easy to solve, but for someone with not much expierience this is a braintwister.
I really hope someone can help me!
Aucun commentaire:
Enregistrer un commentaire