views:

173

answers:

0

I've got some code which queries a rest api on a service which then updates a database, I then have a cursor which looks at the database. I got some of the underlaying framework from the google iosched app.

Calls to mRunnersAdapter.notifyDataSetChanged() in the onReceiveResult method don't seem to do anything, it's only by manually initiating a query with mRunnerHandler.startQuery in the Runnable mRefreshRunnersRunnable does the data update. I think there's something wrong here, I'm sure I shouldn't need to restart the query again but I can't seem to get anything else to work.

Can anyone see where I'm going wrong?

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;
            }
        }
    }
}