jeudi 25 juin 2015

Only first entry inserted to SQLite Database [duplicate]

This question is an exact duplicate of:

I want to store a String (markerID), an Integer (position in ListView) and another Integer (Like: 0 or 1) in an SQLDatabase Table. The first Entry is stored perefctly and on the next call I get the Debug Log "Rows" with the correct String and Integers. After that the problems occur. Even if I click on other List Items and get the particular Debug Logs, there is no new row inserted. Also, if I click on the first List Item again, the Row is not updated. This is how I call the cursor:

Context context = this;
   getALlEntrysListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            likeButton = (ImageView) view.findViewById(R.id.heartImage);

            DbHelper dbh = new DbHelper(context);
            Cursor cursor = dbh.getLike(dbh);
            cursor.moveToFirst();
            if (cursor.moveToFirst()) {
                do {
                    Log.d("Debug", "LongItemClick on " + position);
                    Log.d("Database", "Rows: " + cursor.getString(0) + cursor.getString(1) + cursor.getString(2));
                    if (cursor.getString(0).equals(markerID) && Integer.parseInt(cursor.getString(1)) == position && Integer.parseInt(cursor.getString(2)) == 1) {
                        dbh.updateLike(dbh, markerID, position, Integer.parseInt(cursor.getString(2)), 0);
                        Log.d("Debug", "Dislike");
                        cursor.close();
                        return true;
                    }
                    else if (cursor.getString(0).equals(markerID) && Integer.parseInt(cursor.getString(1)) == position && Integer.parseInt(cursor.getString(2)) == 0) {
                        Log.d("Debug", "Change Dislike to Like");
                        dbh.updateLike(dbh, markerID, position, Integer.parseInt(cursor.getString(2)), 1);
                        cursor.close();
                        return true;
                    }
                    else {
                        Log.d("Debug", "New Like");
                        dbh.addLike(dbh, markerID, position, 1);
                        cursor.close();
                        return true;
                    }
                } while (cursor.moveToNext());
            } else {
                Log.d("Debug", "First Entry");
                dbh.addLike(dbh, markerID, position, 1);
                cursor.close();
                return true;
            }

        }
    });

These are the methods addLike, updateLike and getLike in the class DbHelper:

public void addLike (DbHelper dbh, String markerID, Integer position, Integer like) {
    dbase = dbh.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put(LIKES_MARKERID, markerID);
    cv.put(LIKES_POSITION, position);
    cv.put(LIKES_LIKE, like);
    dbase.insert(TABLE_LIKES, null, cv);
}

public void updateLike (DbHelper dbh, String markerID, Integer position, Integer like, Integer newLike) {
    dbase = dbh.getWritableDatabase();
    String selection = LIKES_POSITION+ " LIKE ? AND "+ LIKES_MARKERID + " LIKE ? ";
    String args[] = {like.toString(), markerID};
    ContentValues values = new ContentValues();
    values.put (LIKES_LIKE, newLike);
    dbase.update(TABLE_LIKES, values, selection, args);
}

public Cursor getLike(DbHelper dbh) {
    dbase = dbh.getReadableDatabase();
    String columns[] = {LIKES_MARKERID, LIKES_POSITION, LIKES_LIKE};
    Cursor cursor = dbase.query(TABLE_LIKES, columns, null, null, null, null, null);
    return cursor;
}

Creation of the table:

String likes = "CREATE TABLE IF NOT EXISTS " + TABLE_LIKES + " ( "
            + LIKES_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + LIKES_MARKERID + " TEXT, "
            + LIKES_POSITION + " INTEGER, "
            + LIKES_LIKE + " INTEGER) ";
    db.execSQL(likes);

Aucun commentaire:

Enregistrer un commentaire