我正在尝试使用 Content Provider 让我的 ListView 刷新。我已经创建了我的 Provider 并尝试将它链接到我的 SqlLite DB。一旦我完成了我的提供者,我将实施我的光标加载器,但首先我需要我的提供者的帮助。我还没有为新手用户找到有关如何创建内容提供者的详细信息。任何援助将不胜感激。我的数据库和提供者发布在下面。我的主要问题是尝试创建一个链接到我的数据库的 URI。
D B:
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;
}
}