-1

コンテンツプロバイダを使用して、データベースからデータを格納および取得しています。これは、ユーザーがアプリをインストールするときにアプリで起こっていることです。コンテンツプロバイダを使用してdbからデータをロード

  1. のAppは
  2. アプリケーションはいずれも存在しないだろう

問題が初めてであるDBからデータをロードするCursorLoaderを使用しているDBに それをサーバーからデータをダウンロードし、保存するサービスを開始しますサービスがデータをダウンロードして保存するまで、db内のデータ。その場合、CursorLoaderはdbからデータをロードしません。私は、アプリケーションを閉じて再オープンすると、dbからデータをロードします。

これは私がここでContentProvider

@Override 
public int bulkInsert(Uri uri, ContentValues[] values) { 
    final SQLiteDatabase db = feederDbHelper.getWritableDatabase(); 
    int match = sUriMatcher.match(uri); 
    switch (match) { 
     case CODE_INSERT_SOURCE: 
      //Return the number of rows inserted from our implementation of bulkInsert 
      return insertRecords(FeederContract.SourceEntry.TABLE_NAME, db, values, uri); 
     case CODE_INSERT_ARTICLE: 
      return insertRecords(FeederContract.ArticleEntry.TABLE_NAME, db, values, uri); 
     default: 
      return super.bulkInsert(uri, values); 
    } 
} 

を使用していますがここでinsertRecords方法

public int insertRecords(String tabbleName, SQLiteDatabase db, ContentValues[] values, Uri uri) { 
     db.beginTransaction(); 
     int rowsInserted = 0; 
     try { 
      for (ContentValues value : values) { 
       long _id = db.insertWithOnConflict(tabbleName, null, value, SQLiteDatabase.CONFLICT_REPLACE); 
       if (_id != -1) { 
        rowsInserted++; 
       } 
      } 
      db.setTransactionSuccessful(); 
     } finally { 
      db.endTransaction(); 
     } 

     if (rowsInserted > 0) { 
      getContext().getContentResolver().notifyChange(uri, null); 
     } 
     //Return the number of rows inserted from our implementation of bulkInsert 
     return rowsInserted; 
    } 

ある方法ですが、ユーザーがアプリを初めて起動したときに何が起こっているのかです。コメントも追加しました。私はここで何を

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.feeds_fragment, container, false); 
    ButterKnife.bind(this, view); 
    loadAd(); 
    initRc(); 
    // Starting service to download feeds 
    FeederSyncUtil.startSync(getActivity()); 
    // Starting loader to load feeds 
    getActivity().getSupportLoaderManager().initLoader(ID_FEEDS_LOADER, null, this); 
    return view; 
} 

これらはcallbacks

@Override 
public Loader<Cursor> onCreateLoader(int id, Bundle args) { 
    return new CursorLoader(getActivity(), 
      FeederContract.ArticleEntry.CONTENT_URI, 
      MAIN_FEED_PROJECTION, 
      null, 
      null, 
      null); 
} 

@Override 
public void onLoadFinished(Loader<Cursor> loader, Cursor data) { 
    Log.d(TAG, data.getCount() + ""); 
    mFeedsAdapter.swapCursor(data); 
} 

@Override 
public void onLoaderReset(Loader<Cursor> loader) { 
    mFeedsAdapter.swapCursor(null); 
} 

ローダーですが行方不明ですか?私はgetContext().getContentResolver().notifyChange(uri, null)を使用していますが、それでもまだリフレッシュしません。Cursorいくつかのガイダンスがあります。

答えて

1

Err!私は行方不明のものがあった。私はその後、私はドキュメントに行き、私は

/** 
* Register to watch a content URI for changes. This can be the URI of a specific data row (for 
* example, "content://my_provider_type/23"), or a a generic URI for a content type. 
* 
* @param cr The content resolver from the caller's context. The listener attached to 
* this resolver will be notified. 
* @param uri The content URI to watch. 
*/ 
void setNotificationUri(ContentResolver cr, Uri uri); 

だから、私は私のプロバイダのqueryメソッド内でこのメソッドの呼び出しをフックアップ追加この方法を見つけたどこにでも見て、この問題に時間を費やしています。 は、ここに私のプロバイダ

public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { 
     int match = sUriMatcher.match(uri); 
     Cursor cursor = null; 
     switch (match) { 
      case CODE_INSERT_SOURCE: 
       cursor = feederDbHelper.getReadableDatabase().query(FeederContract.SourceEntry.TABLE_NAME, 
         projection, 
         selection, 
         selectionArgs, 
         null, 
         null, 
         sortOrder); 
       break; 
      case CODE_VIEW_ARTICLE: 

       cursor = feederDbHelper.getReadableDatabase().query(FeederContract.ArticleEntry.TABLE_NAME, 
         projection, 
         selection, 
         selectionArgs, 
         null, 
         null, 
         sortOrder); 
       break; 

      default: 
       throw new UnsupportedOperationException("Unknown uri " + uri); 
     } 
     cursor.setNotificationUri(getContext().getContentResolver(),uri); 
     return cursor; 
    } 

だから、この行cursor.setNotificationUri(getContext().getContentResolver(),uri); は、すべての魔法をしてからいくつか抜粋です。 insert()delete()update()bulkInsert()を使用してURIの基礎となるデータに変更があった場合、その変更についてcontentresolverに通知します。 CursorLoaderはデータの変更を自動的に検出しないため、使用する必要がありますsetNotificationUri(ContentResolver cr, Uri uri);

私はこれが誰かを助けることを望みます。 :)

関連する問題