Android:扩展的CursorAdapter问题

时间:2010-07-17 11:57:30

标签: android cursor adapter

我有一些代码在服务上查询rest api然后更新数据库,然后我有一个查看数据库的游标。我从谷歌iosched应​​用程序获得了一些底层框架。

mRunnersAdapter.notifyDataSetChanged()方法中对onReceiveResult的调用似乎没有做任何事情,只能通过在Runnable mRunnerHandler.startQuery中使用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;
            }
        }
    }
}

0 个答案:

没有答案