我正在尝试使用内容提供程序刷新我的ListView。我创建了我的Provider并尝试将它连接到我的SqlLite DB。一旦我完成了我的提供程序,我将实现我的cursorloader但是我需要帮助我的提供者。我没有找到关于如何创建内容提供商的新手用户的详细信息。任何援助将不胜感激。我的数据库和提供商发布在下面。我的主要问题是尝试创建一个链接到我的数据库的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;
}
}
答案 0 :(得分:0)
我的主要问题是尝试创建一个链接到我的数据库的URI。
现在您的ListProvider.CONTENT_URI
将使用ListProvider
在UriMatcher
中匹配。您的ListProvider
正在使用您的dataStore.TABLE_NAME_INFOTABLE
,这是您数据库中唯一的表格。
因此,您可以使用ContentProvider
中的ContentResolver
直接使用Activity
,代码如下:
getContentResolver().query(ListProvider.CONTENT_URI, projection, selection, selectionArgs, sortOrder);
getContentResolver().delete(ListProvider.CONTENT_URI, where, selectionArgs);
//etc...
一旦我完成了我的提供程序,我将实现我的cursorloader但是我需要帮助我的提供者。
您的ContentProvider
似乎已完成。但是,您必须记住使用相同的AUTHORITY
将其添加到您的清单中。您可以重命名权限,但它必须是唯一的,并且在您的`ContentProvider和清单中匹配。
<provider
android:name="com.loginplus.home.ListProvider"
android:authorities="com.loginplus.home.ListProvider" >
</provider>
然后,您就可以在CursorLoader
回调中创建public Loader<Cursor> onCreateLoader(int id, Bundle args)
。
CursorLoader cursorLoader = new CursorLoader(this, ListProvider.CONTENT_URI, projection, selection, selectionArgs, orderBy);
我没有找到有关如何创建内容提供商的新手用户的详细信息。任何帮助将不胜感激。
SQLite database
,ContentProvider
和CursorLoader
。这帮助我开始并给了我很好的基础。