コンテンツ プロバイダーを使用して ListView を更新しようとしています。プロバイダーを作成し、それを SqlLite DB にリンクしようとしました。プロバイダーが完成したら、カーソルローダーを実装しますが、最初にプロバイダーのサポートが必要です。コンテンツ プロバイダーの作成方法に関する初心者ユーザー向けの詳細な情報は見つかりませんでした。任意の支援をいただければ幸いです。私のDBとプロバイダーは以下に掲載されています。私の主な問題は、DB にリンクする URI を作成しようとすることです。
DB:
public class dataStore extends SQLiteOpenHelper {
//Table attributes
public static final String DATABASE_NAME = "SiteLogindb";
public static final int DATABASE_VERSION = 2;
public static final String TABLE_NAME_INFOTABLE = "infoTable";
// Data attributes
public static final String COLUMN_NAME_SITE = "sName";
public static final String COLUMN_NAME_ADDRESS = "wUrl";
public static final String COLUMN_NAME_USERNAME = "uName";
public static final String COLUMN_NAME_PASSWORD = "pWord";
public static final String COLUMN_NAME_NOTES = "lNotes";
public dataStore(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String sqlDataStore = "create table if not exists " +
TABLE_NAME_INFOTABLE + " ("+ BaseColumns._ID + " integer primary key autoincrement,"
+ COLUMN_NAME_SITE + " text not null,"
+ COLUMN_NAME_ADDRESS + " text not null,"
+ COLUMN_NAME_USERNAME + " text not null,"
+ COLUMN_NAME_PASSWORD + " text not null,"
+ COLUMN_NAME_NOTES + " text not null);";
db.execSQL(sqlDataStore);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
if(oldVersion == 1 && newVersion == 2){
//Upgrade the database
}
}
}
コンテンツ プロバイダ:
public class ListProvider extends ContentProvider {
private dataStore lDB;
private static final String AUTHORITY = "com.loginplus.home.ListProvider";
public static final int TUTORIALS = 1;
public static final int TUTORIAL_ID = 2;
private static final String TUTORIALS_BASE_PATH = "tutorials";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
+ "/" + TUTORIALS_BASE_PATH);
public static final String CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE
+ "/mt-tutorial";
public static final String CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE
+ "/mt-tutorial";
@Override
public boolean onCreate() {
lDB = new dataStore(getContext());
return true;
}
private static final UriMatcher sURIMatcher = new UriMatcher(
UriMatcher.NO_MATCH);
static {
sURIMatcher.addURI(AUTHORITY, TUTORIALS_BASE_PATH, TUTORIALS);
sURIMatcher.addURI(AUTHORITY, TUTORIALS_BASE_PATH + "/#", TUTORIAL_ID);
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
queryBuilder.setTables(dataStore.TABLE_NAME_INFOTABLE);
int uriType = sURIMatcher.match(uri);
switch (uriType) {
case TUTORIAL_ID:
queryBuilder.appendWhere(dataStore.DATABASE_VERSION + "="
+ uri.getLastPathSegment());
break;
case TUTORIALS:
// no filter
break;
default:
throw new IllegalArgumentException("Unknown URI");
}
Cursor cursor = queryBuilder.query(lDB.getReadableDatabase(),
projection, selection, selectionArgs, null, null, sortOrder);
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
}
//Deleting DB entries
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int uriType = sURIMatcher.match(uri);
SQLiteDatabase sqlDB = lDB.getWritableDatabase();
int rowsAffected = 0;
switch (uriType) {
case TUTORIALS:
rowsAffected = sqlDB.delete(dataStore.TABLE_NAME_INFOTABLE,
selection, selectionArgs);
break;
case TUTORIAL_ID:
String id = uri.getLastPathSegment();
if (TextUtils.isEmpty(selection)) {
rowsAffected = sqlDB.delete(dataStore.TABLE_NAME_INFOTABLE,
BaseColums.ID + "=" + id, null);
} else {
rowsAffected = sqlDB.delete(dataStore.TABLE_NAME_INFOTABLE,
selection + " and " + BaseColums.ID + "=" + id,
selectionArgs);
}
break;
default:
throw new IllegalArgumentException("Unknown or Invalid URI " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return rowsAffected;
}
@Override
public String getType(Uri uri) {
// TODO Auto-generated method stub
return null;
}
//Inserting DB entries
@Override
public Uri insert(Uri uri, ContentValues values) {
// TODO Auto-generated method stub
return null;
}
//updating DB entries
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
int uriType = sURIMatcher.match(uri);
SQLiteDatabase sqlDB = lDB.getWritableDatabase();
int rowsAffected = 0;
switch (uriType) {
case TUTORIALS:
rowsAffected = sqlDB.update(dataStore.TABLE_NAME_INFOTABLE,
null, selection, selectionArgs);
break;
case TUTORIAL_ID:
String id = uri.getLastPathSegment();
if (TextUtils.isEmpty(selection)) {
rowsAffected = sqlDB.update(dataStore.TABLE_NAME_INFOTABLE,
null, BaseColums.ID + "=" + id, null);
} else {
rowsAffected = sqlDB.update(dataStore.TABLE_NAME_INFOTABLE,
null, selection + " and " + BaseColums.ID + "=" + id,
selectionArgs);
}
break;
default:
throw new IllegalArgumentException("Unknown or Invalid URI " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return rowsAffected;
}
}