0
A
回答
1
你可以找到官方的文檔here也可以實現它,我有
DBFlow版本使用3
//我已經編輯下面
爲內容提供商提供的部分更簡單的方法我的回答&方式將此添加到內部應用程序
<provider
android:authorities="com.hashx19.pristinekashmir.mycontentprovider"
android:exported="false"
android:name=".MyContentProvider"/>
創建名爲MyContentPro的java文件vider &複製下面的代碼 &根據您的項目替換AUTHORITY,ENDPOINT,AppDatabase(您的數據庫名稱),TableClassName。
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.CursorIndexOutOfBoundsException;
import android.net.Uri;
import com.hashx19.pristinekashmir.MySQLiteHelper;
import com.raizlabs.android.dbflow.annotation.ConflictAction;
import com.raizlabs.android.dbflow.config.FlowManager;
import com.raizlabs.android.dbflow.structure.ModelAdapter;
import java.util.Arrays;
import java.util.HashSet;
/**
* Created by Filu on 8/25/2016.
*/
public class MyContentProvider extends ContentProvider {
public static final String AUTHORITY = "com.hashx19.pristinekashmir.mycontentprovider";
private static final String ENDPOOINT = "feeds";
// @ContentUri(path = ENDPOOINT, type = ContentUri.ContentType.VND_MULTIPLE + ENDPOOINT)
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
+ "/" + ENDPOOINT);
private static final int feeds_CONTENT_URI = 0;
private static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
static {
MATCHER.addURI(AUTHORITY, ENDPOOINT, feeds_CONTENT_URI);
}
;
@Override
public final String getType(Uri uri) {
String type = null;
switch(MATCHER.match(uri)) {
case feeds_CONTENT_URI: {
type = "vnd.android.cursor.dir/" +ENDPOINT;
break;
}
default: {
throw new IllegalArgumentException("Unknown URI" + uri);
}
}
return type;
}
@Override
public boolean onCreate() {
return false;
}
@Override
public final Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
android.database.Cursor cursor = null;
switch(MATCHER.match(uri)) {
case feeds_CONTENT_URI: {
cursor = FlowManager.getDatabase("AppDatabase").getWritableDatabase().query("TableClassName", projection, selection, selectionArgs, null, null, sortOrder);
break;
}
}
if (cursor != null) {
cursor.setNotificationUri(getContext().getContentResolver(), uri);
}
return cursor;
}
@Override
public final Uri insert(Uri uri, ContentValues values) {
switch(MATCHER.match(uri)) {
case feeds_CONTENT_URI: {
ModelAdapter adapter = FlowManager.getModelAdapter(FlowManager.getTableClassForName("AppDatabase", "TableClassName"));
final long id = FlowManager.getDatabase("AppDatabase").getWritableDatabase().insertWithOnConflict("TableClassName", null, values, ConflictAction.getSQLiteDatabaseAlgorithmInt(adapter.getInsertOnConflictAction()));
getContext().getContentResolver().notifyChange(uri, null);
return ContentUris.withAppendedId(uri, id);
}
default: {
throw new IllegalStateException("Unknown Uri" + uri);
}
}
}
@Override
public final int delete(Uri uri, String selection, String[] selectionArgs) {
switch(MATCHER.match(uri)) {
case feeds_CONTENT_URI: {
long count = FlowManager.getDatabase("AppDatabase").getWritableDatabase().delete("TableClassName", selection, selectionArgs);
if (count > 0) {
getContext().getContentResolver().notifyChange(uri, null);
}
return (int) count;
}
default: {
throw new IllegalArgumentException("Unknown URI" + uri);
}
}
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
switch(MATCHER.match(uri)) {
case feeds_CONTENT_URI: {
ModelAdapter adapter = FlowManager.getModelAdapter(FlowManager.getTableClassForName("AppDatabase", "TableClassName"));
long count = FlowManager.getDatabase("AppDatabase").getWritableDatabase().updateWithOnConflict("TableClassName", values, selection, selectionArgs, ConflictAction.getSQLiteDatabaseAlgorithmInt(adapter.getUpdateOnConflictAction()));
if (count > 0) {
getContext().getContentResolver().notifyChange(uri, null);
}
return (int) count;
}
default: {
throw new IllegalStateException("Unknown Uri" + uri);
}
}
}
}
那麼當重載裝載機方法做這樣的事情
getLoaderManager().initLoader(1, null, this);
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String selection, sortOrder;
String[] selectionArgs, projection;
selection = ...;
selectionArgs = ...;
sortOrder = ...;
projection= new String[]{"id","date", "link","title","content","excerpt","author",};
CursorLoader cursorLoader = new CursorLoader(getContext(),MyContentProvider.CONTENT_URI, projection,null,null,null);
return cursorLoader;
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
TableClass post = new TableClass();
while (!cursor.isAfterLast()) {
try{
post.setId(cursor.getInt(cursor.getColumnIndex("id")));
}catch (NullPointerException e){
e.printStackTrace();
}catch (CursorIndexOutOfBoundsException c){
c.printStackTrace();
}
}
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
}
editted
想通了實現內容提供商更簡單的方法。
將此添加到您的清單/或修改這種方式,如果你已經添加提供商代碼。
修改AppDatabase類爲
@ContentProvider(authority = AppDatabase.AUTHORITY, database = AppDatabase.class, baseContentUri = AppDatabase.BASE_CONTENT_URI) @Database(name = AppDatabase.NAME, version = AppDatabase.VERSION) public class AppDatabase { public static final String NAME = "AppDatabase"; // we will add the .db extension public static final int VERSION = 2; public static final String AUTHORITY = "com.hashx19.pristinekashmir.dbflowcontentprovider"; public static final String BASE_CONTENT_URI = "content://"; }
修改你想要的供應商使用,因爲
@TableEndpoint(name = PostData.ENDPOINT, contentProvider = AppDatabase.class) @Table(database = AppDatabase.class ,allFields = true ,name = PostData.ENDPOINT) public class PostData extends BaseModel { public static final String ENDPOINT = "PostData"; @ContentUri(path = ENDPOINT, type = ContentUri.ContentType.VND_MULTIPLE + ENDPOINT) public static final Uri CONTENT_URI = Uri.parse(AppDatabase.BASE_CONTENT_URI + AppDatabase.AUTHORITY + "/" + ENDPOINT); @PrimaryKey public int id; public String image; }
對於使用內容提供商,如光標裝載機使用表名的每個表.CONTENT_URI在此情況下爲
CursorLoader cursorLoader = new CursorLoader(getContext(),PostData.CONTENT_URI,projection,null,null,null);
相關問題
- 1. Android LoaderManager和CursorLoader混淆
- 2. 的Android CursorLoader和LoaderManager錯誤
- 3. CursorAdapter和CursorLoader的問題android
- 4. Android CursorLoader vs AsyncQueryHandler?
- 5. AsyncTask and CursorLoader [Android]
- 6. Android CursorLoader Listview
- 7. DBFlow和Butterknife衝突
- 8. Android - DBflow遷移腳本
- 9. Android Combine ImageDownload with CursorLoader
- 10. 與DbFlow
- 11. FragmentActivity,CursorLoader和getActivity undefined
- 12. 的Android SimpleCursorAdapter與LoaderManager/CursorLoader
- 13. IntentService和CursorLoader Sychronisation問題
- 14. 在DBFlow中升級數據庫Android
- 15. 的Android DBFlow並不保存對象
- 16. 如何在Android中更新dbFlow?
- 17. DBFlow Android選擇最常見的值
- 18. DBFlow「計數爲」
- 19. DBFlow選擇列
- 20. GROUP BY with CursorLoader
- 21. 使用CursorLoader和SimpleCursorAdapter的AutoCompleteTextView
- 22. 分頁列表和CursorLoader
- 23. DBFlow - 無法刪除數據
- 24. Android CursorLoader對ContentProvider通知沒有響應
- 25. 需要性能比較AsyncQueryHandler vs CursorLoader Android
- 26. 如何測試的Android CursorLoader與robolectric
- 27. 使用GROUP BY與CursorLoader - Android電子
- 28. 的Android使用cursorloader代替managedQuery
- 29. Android CursorLoader不工作在片段
- 30. 如何在Android ListView的OnClickItemListener中使用LoaderManager和CursorLoader
使用由android提供的loader管理器,易於使用和維護,http://developer.android.com/guide/components/loaders.html –