Ich habe Schwierigkeiten, die Update-Methode in meinem Content-Provider funktioniert, das Update gibt 0 zurück und es gibt keine aktualisierten Informationen in der Tabelle. Die Tabelle wird an diesem Punkt ausgefüllt.Update-Funktion in Content-Provider nicht Datenbank aktualisieren
Hier ist die Update-Funktion:
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
SQLiteDatabase db = leagueDBHelper.getWritableDatabase();
int rowsUpdated = 0;
int uriType = uriMatcher.match(uri);
switch (uriType) {
case LEAGUES:
rowsUpdated = db.update(LeagueContract.LEAGUE_TABLE_NAME,
values, selection, selectionArgs);
break;
case LEAGUE_ID:
String newSelection = appendToSelection(uri, selection);
rowsUpdated = db.update(LeagueContract.LEAGUE_TABLE_NAME,
values, newSelection, selectionArgs);
break;
default:
throw new IllegalArgumentException("Unrecognised uri: " + uri);
}
getContext().getContentResolver().notifyChange(uri,null);
return rowsUpdated;
}
private String appendToSelection(Uri uri, String selection) {
String id = uri.getLastPathSegment();
StringBuilder newSelection = new StringBuilder(LeagueContract.COLUMN_KEY_ID + "=" + id);
if (selection != null && !selection.isEmpty()) {
newSelection.append(" AND " + selection);
}
return newSelection.toString();
}
Hier ist, wo ich die Aktualisierung der Content-Provider nennen:
message = "Player 1 wins";// we should get the name
ContentValues mUpdateValues = new ContentValues();
String[] projectionFields = new String[] {
LeagueContract.COLUMN_NAME_SCORE };
Uri uri = ContentUris.withAppendedId(LeagueContentProvider.LEAGUE_URI, player1Id);
ContentResolver content = getContentResolver();
Cursor cursor = content.query(uri, projectionFields, null, null, null);
cursor.moveToFirst();
int score = cursor.getInt(0);
score ++;
ContentValues values = new ContentValues();
values.put(LeagueContract.COLUMN_NAME_SCORE,score);
content.update(uri,values,null,null);
Alles sieht gut für mich aus. Hast du die Spieler-ID überprüft? –
Ja, es zieht die Punktzahl aus dem Database fein. Also player1Id ist korrekt. Ich dachte, alles sah gut aus, kann das Problem wirklich nicht sehen! –
Problem gelöst, COLUMN_KEY_ID war eine ganze Zahl, der Titel der Spalte wurde verwendet und dies funktioniert dann. –