コンテンツ プロバイダーに値を入れようとしていますが、アプリは常に動作を停止し、trows を返します:
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
public class ParcoursProvider extends ContentProvider
{
public static final String PROVIDER_NAME =
"com.amf.provider.Parcours";
public static final Uri CONTENT_URI =
Uri.parse("content://"+ PROVIDER_NAME + "/parcours");
public static final String _ID = "_id";
public static final String NORMALE = "normale";
private static final UriMatcher uriMatcher;
static{
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(PROVIDER_NAME, "normale", 1);
}
//---for database use---
private SQLiteDatabase parcoursDB;
private static final String DATABASE_NAME = "Parcours";
private static final String DATABASE_TABLE = "normale";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE =
"create table " + DATABASE_TABLE +
" (_id integer primary key autoincrement, "
+ "normale REAL not null);";
/* Classe helper pour la BD provisoire utilisees
* pour le stockage des livres
*/
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion) {
Log.w("Content provider database",
"maj de la BD de la version " +
oldVersion + " à " + newVersion +
", ce qui détruira les anciennes données.");
db.execSQL("DROP TABLE IF EXISTS titles");
onCreate(db);
}
}
@Override
public String getType(Uri uri) {
switch (uriMatcher.match(uri)){
//---get all books---
case 1:
return "normale "; // vnd : vendor
//---get a particular book---
case 2:
return "vnd.android.cursor.item/vnd.amf.parcours ";
default:
throw new IllegalArgumentException("URI invalide: " + uri);
}
}
@Override
public Uri insert(Uri uri, ContentValues values) {
//---add a new book---
long rowID = parcoursDB.insert(DATABASE_TABLE, "", values);
//---if added successfully---
if (rowID>0)
{
Uri _uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
getContext().getContentResolver().notifyChange(_uri, null);
return _uri;
}
throw new SQLException("Adding failed: " + uri);
}
@Override
public boolean onCreate() {
Context context = getContext();
DatabaseHelper dbHelper = new DatabaseHelper(context);
parcoursDB = dbHelper.getWritableDatabase();
return (parcoursDB == null)? false:true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder sqlBuilder = new SQLiteQueryBuilder();
sqlBuilder.setTables(DATABASE_TABLE);
if (uriMatcher.match(uri) == 2)
//---if getting a particular book---
sqlBuilder.appendWhere( // ajouter une clause where à la requête
_ID + " = " + uri.getPathSegments().get(1));
if (sortOrder==null || sortOrder=="")
sortOrder = NORMALE;
Cursor c = sqlBuilder.query(
parcoursDB,
projection,
selection,
selectionArgs,
null,
null,
sortOrder);
//---register to watch a content URI for changes---
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
int count = 0;
switch (uriMatcher.match(uri)){
case 1:
count = parcoursDB.update(DATABASE_TABLE, values,selection, selectionArgs);
break;
case 2:
count = parcoursDB.update(DATABASE_TABLE, values,_ID + " = " + uri.getPathSegments().get(1) + (!TextUtils.isEmpty(selection)), selectionArgs);
break;
default: throw new IllegalArgumentException("Unknown URI " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
@Override
public int delete(Uri arg0, String arg1, String[] arg2) {
// TODO Auto-generated method stub
return 0;
}
}
そして、追加する EditText:
ContentValues values = new ContentValues();
values.put("normale", ((EditText) findViewById(R.id.txtTitle)).getText().toString());
Uri uri = getContentResolver().insert(Uri.parse("content://com.amf.provider.Parcours/normale"), values);
いつものように、それは非常に単純なものでなければなりません。どんな助けでも大歓迎です よろしくお願いします!:)