光标适配器在屏幕旋转时不从数据库刷新ListView

时间:2018-05-17 10:38:42

标签: android sqlite listview loader

我有以下片段:

public class Tab3Fragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {

    private static final int LOADER_ID = 42;
    private CursorAdapter _adapter;


    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
        _adapter.notifyDataSetChanged();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        _adapter.notifyDataSetChanged();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.tab3_frag, container, false);

        ListView lv = view.findViewById(R.id.student_view);
        _adapter = new SimpleCursorAdapter(getContext(),
                R.layout.container_list_item_view, null,
                new String[] { Contract.Student_Table.STUDENTS_COLUMN_NAME_NOID},
                new int[] { R.id.list_item });
        lv.setAdapter(_adapter);
        getActivity().getLoaderManager().restartLoader(LOADER_ID, null, this);

        return view;
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        if (id != LOADER_ID) {
            return null;
        }
        return new CursorLoader(getContext(),
                Contract.Student_Table.CONTENT_URI,
                new String[] { Contract.Student_Table.STUDENTS_COLUMN_ID, Contract.Student_Table.STUDENTS_COLUMN_NAME_NOID}, null, null,
                null);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        _adapter.changeCursor(data);
    }

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

但是,每当我旋转屏幕时,内容都会从显示屏上移除。我已经覆盖了必要的功能,我尝试将initLoader更改为restartLoader并将swapCursor更改为changeCursor,但我似乎无法找出问题所在。

任何人都可以确定问题并且可以指出它以便我可以解决它吗?

由于

0 个答案:

没有答案