データベースを更新するサービスのRESTAPIをクエリするコードがあり、データベースを調べるカーソルがあります。googleioschedアプリからいくつかの基礎となるフレームワークを入手しました。
mRunnersAdapter.notifyDataSetChanged()
メソッドでの呼び出しonReceiveResult
は何もしないようですmRunnerHandler.startQuery
。Runnableで手動でクエリを開始するだけでmRefreshRunnersRunnable
データが更新されます。ここに何か問題があると思います。クエリを再開する必要はないと確信していますが、他に何も機能しないようです。
誰かが私がどこで間違っているのか見ることができますか?
public class exampleActivity extends Activity implements DetachableResultReceiver.Receiver {
public void onCreate(Bundle savedInstanceState) {
mState = (AppState) activity.getApplication();
mState.mReceiver.setReceiver(this);
mRunnerHandler = new NotifyingAsyncQueryHandler(getContentResolver(), runnersListener);
mRunnersAdapter = new RunnerAdapter(this);
setListAdapter(mRunnersAdapter);
refreshRunnerPriceInfo();
}
public void resetTimer() {
nextRefreshTimePeriod = (SystemClock.uptimeMillis() / refreshPeriod + 1) * refreshPeriod;
}
public void refreshRunnerPriceInfo() {
resetTimer();
getRunnerPriceInfo();
}
private void getRunnerPriceInfo() {
Intent serviceIntent = new Intent(Intent.ACTION_SYNC, null, getBaseContext(), QueryService.class);
serviceIntent.putExtra(QueryService.EXTRA_STATUS_RECEIVER, mState.mReceiver);
serviceIntent.putExtra(QueryService.EXTRA_STATUS_URL_EXTENSION, Price.buildUrlExtension(marketId));
serviceIntent.putExtra(QueryService.EXTRA_STATUS_TYPE, Price.CONTENT_TYPE);
startService(serviceIntent);
}
public void onWindowFocusChanged(boolean hasFocus) {
if (!hasFocus) {
nextRefreshTimePeriod = -1;
} else {
refreshRunnerPriceInfo();
}
super.onWindowFocusChanged(hasFocus);
}
AsyncQueryListener runnersListener = new AsyncQueryListener() {
public void onQueryComplete(int token, Object cookie, Cursor cursor) {
startManagingCursor(cursor);
mRunnersAdapter.changeCursor(cursor);
}
};
private Runnable mRefreshRunnersRunnable = new Runnable() {
public void run() {
if (queriesStarted) {
getRunnerPriceInfo();
resetTimer();
mRunnerHandler.startQuery(Runner.buildUri(marketId), RunnerPriceQuery.PROJECTION, Price.DEFAULT_SORT);
mMessageHandler.postAtTime(mRefreshRunnersRunnable, nextRefreshTimePeriod);
}
}
};
private class RunnerAdapter extends CursorAdapter implements Filterable {
public RunnerAdapter(Context context) {
super(context, null);
}
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return getLayoutInflater().inflate(R.layout.market_view_list_item, parent, false);
}
public void bindView(View view, Context context, Cursor cursor) {
// populate view
}
}
protected void onResume() {
super.onResume();
mMessageHandler.post(mRefreshRunnersRunnable);
}
protected void onPause() {
mMessageHandler.removeCallbacks(mRefreshRunnersRunnable);
super.onPause();
}
interface RunnerPriceQuery {
String[] PROJECTION = { BaseColumns._ID, etc };
}
public void onReceiveResult(int resultCode, Bundle resultData) {
switch (resultCode) {
case QueryService.STATUS_RUNNING: {
break;
}
case QueryService.STATUS_FINISHED: {
String intentReturnType;
try {
intentReturnType = resultData.getString(QueryService.EXTRA_STATUS_TYPE);
} catch (NullPointerException e) {
BLog.e(getClass(), "No results found, probably network issues", e);
break;
}
if (Price.CONTENT_TYPE.equals(intentReturnType)) {
if (!queriesStarted) {
mMessageHandler.post(mRefreshRunnersRunnable);
mRunnerHandler.startQuery(Runner.buildUri(marketId), RunnerPriceQuery.PROJECTION, Price.DEFAULT_SORT);
queriesStarted = true;
}
if (mRunnersAdapter != null)
mRunnersAdapter.notifyDataSetChanged();
}
break;
}
case QueryService.STATUS_ERROR: {
final String errorText = getString(R.string.toast_sync_error, resultData.getString(Intent.EXTRA_TEXT));
Log.i(this.getClass(), "STATUS_ERROR\n" + errorText);
Toast.makeText(MarketActivity.this, errorText, Toast.LENGTH_LONG).show();
break;
}
}
}
}