2016-03-21 77 views
0

任何人都知道如何使用cursorLoader和DBFlow?我看到這個issue,但沒有添加到DBFlow。Android DBFlow和CursorLoader

謝謝。

+0

使用由android提供的loader管理器,易於使用和維護,http://developer.android.com/guide/components/loaders.html –

回答

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

想通了實現內容提供商更簡單的方法。

  1. 將此添加到您的清單/或修改這種方式,如果你已經添加提供商代碼。

  2. 修改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://"; } 
    
  3. 修改你想要的供應商使用,因爲

    @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; 
    
    } 
    
  4. 對於使用內容提供商,如光標裝載機使用表名的每個表.CONTENT_URI在此情況下爲

    CursorLoader cursorLoader = new  CursorLoader(getContext(),PostData.CONTENT_URI,projection,null,null,null);