【问题标题】:Empty CursorLoader after returning from child activity从子活动返回后清空 CursorLoader
【发布时间】:2015-04-17 23:00:10
【问题描述】:

我的活动有两个加载器:第一个获取一些元数据,另一个在查询中使用这些数据来获取 ListView 的实际数据。因此,当我开始此活动时,一切正常。然后我启动子活动来编辑/创建列表元素,当我按下后退按钮时,我再次看到第一个活动,但这次 CursorLoader(第一个)返回一个空光标(不是 null,只是 getCount() = 0)。这只发生在按下后退按钮时。如果我完成子活动,一切都很好,就像我第一次开始第一个活动一样。

这是我的活动:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_routine_journal);

        listView = (ListView) findViewById(R.id.routine_journal_listView);

        Intent i = getIntent();
        routineId = i.getLongExtra("routineId", -1);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        if (id == R.id.action_add_routine_item) {
            Intent i = new Intent(this, RoutineItemEditActivity.class);
            i.putExtra("routineId", routineId);
            i.putParcelableArrayListExtra("fields", fields);
            startActivity(i);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {

        CursorLoader cursorLoader = null;

        if (id == FIELDS) {
            cursorLoader = new CursorLoader(this,
                    TrackerContentProvider.CONTENT_FIELD_URI,
                    FieldTable.getColumns(),
                    FieldTable.COLUMN_ROUTINE + " = ?",
                    new String[]{routineId + ""},
                    null);

        } else if (id == JOURNAL) {
            String query = getQuery();
            Log.d("VK", "query=" + query);
            cursorLoader = new CursorLoader(this,
                    TrackerContentProvider.CONTENT_ROUTINE_ITEM_URI,
                    null,
                    null,
                    null,
                    query);
        }

        return cursorLoader;
    }

    @Override
    protected void onResume() {
        super.onResume();

        getLoaderManager().restartLoader(FIELDS, null, this);
    }

    @Override
    public void onLoadFinished(Loader loader, Cursor data) {

        int loaderId = loader.getId();

        if (loaderId == FIELDS) {
            storeFields(data);
        } else if (loaderId == JOURNAL) {
            cursorAdapter.swapCursor(data);
        }
    }

    @Override
    public void onLoaderReset(Loader loader) {
        cursorAdapter.swapCursor(null);
    }

    private void storeFields(Cursor c) {

        if (c == null) return;

        if (c.moveToFirst()) {
            FieldItem fieldItem;
            do {
                long id = c.getLong(c.getColumnIndex(FieldTable.COLUMN_ID));
                String name = c.getString(c.getColumnIndex(FieldTable.COLUMN_NAME));
                String type = c.getString(c.getColumnIndex(FieldTable.COLUMN_TYPE));
                boolean active = c.getInt(c.getColumnIndex(FieldTable.COLUMN_ACTIVE)) != 0;
                long rid = c.getLong(c.getColumnIndex(FieldTable.COLUMN_ROUTINE));

                fieldItem = new FieldItem(id, name, type, active, rid);
                fields.add(fieldItem);

            } while (c.moveToNext());
        }

        cursorAdapter = new RoutineCursorAdapter(getBaseContext(), null, 0);
        cursorAdapter.setFields(fields);
        listView.setAdapter(cursorAdapter);

        getLoaderManager().initLoader(JOURNAL, null, this);
    }

更新:由于我注意到在子活动中使用 finish() 时一切正常,因此我尝试在子活动中覆盖默认操作栏的后退按钮行为:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == android.R.id.home) {
        finish();
        return true;
    }
}

这可以解决问题,但似乎不是正确的方法。有什么想法吗?

顺便说一句:后退按钮是指 Activity 操作栏中的按钮(不是系统导航栏中的后退按钮)。

【问题讨论】:

    标签: android android-cursorloader


    【解决方案1】:

    改变

    getLoaderManager().initLoader(JOURNAL, null, this);
    

    getLoaderManager().restartLoader(JOURNAL, null, this);
    

    因为loader已经创建所以没有再创建,必须重启。

    【讨论】:

    • 正如我所说,它有两个加载器,在这里你指的是第二个,如果第一个返回数据,它工作正常。这里的问题是第一个不返回数据...
    • 另外,如果第一个重新加载,您对第二个有问题,请将.forceload(); 添加到第一个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多