1

我有一个 listFragment ,当单击列表中的一个项目时,它会启动一个新活动。当按下返回按钮时,列表再次显示。我正在使用光标加载器,所以我想知道是否有一种简单的方法可以在按下后退按钮时刷新光标加载器。这是因为新活动改变了列表的内容。

编辑:

public class ListWordFragment extends SherlockListFragment implements LoaderManager.LoaderCallbacks<Cursor> {

    private static String TAG = ListWordFragment.class.getSimpleName();
    private CursorLoader cursorLoader;

    // Loader
    private static final int URL_LOADER = 0;
    private SimpleCursorAdapter adapter;
    public static LoaderManager mLoaderManager;

    // ActionBar
    private AutoCompleteTextView autoCompView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_list_words, null);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        ListView list = getListView();


        String[] from = new String[] { Word.NAME, Word.TYPE, Word.TRANSLATE };
        int[] to = new int[] { R.id.textView_word, R.id.textView_type,
                R.id.textView_translate };

        adapter = new ListWordAdapter(getSherlockActivity(),
                R.layout.row_list_words, null, from, to, 0);
        setListAdapter(adapter);

        mLoaderManager = getLoaderManager();
        mLoaderManager.initLoader(URL_LOADER, null, this);

    }



    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        fillRow(l, id, position, false);

    }

    private void fillRow(ListView l, long ide, int position, boolean firstCall) {

        Cursor cursor = getActivity().getContentResolver().query(
                Uri.withAppendedPath(WordListProvider.WORDS_CONTENT_URI,
                        String.valueOf(ide)), null, null, null, null);

        if(cursor.moveToFirst()){

        String id = cursor.getString(cursor.getColumnIndex(Word.ID));
        String name = cursor.getString(cursor.getColumnIndex(Word.NAME));
        String type = cursor.getString(cursor.getColumnIndex(Word.TYPE));
        String translate = cursor.getString(cursor
                .getColumnIndex(Word.TRANSLATE));
        String example = cursor.getString(cursor.getColumnIndex(Word.EXAMPLE));
        String note = cursor.getString(cursor.getColumnIndex(Word.NOTE));

        // Master/Detail
        if ((getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
                && (getResources().getString(R.string.selected_configuration)
                        .equals(Constants.CONFIGURATION_LARGE))) {

            WordDetailFragment frag = (WordDetailFragment) getFragmentManager()
                    .findFragmentById(R.id.word_detail_fragment);

            if (frag != null) {
                frag.setId(id);
                frag.setName(name);
                frag.setType(type);
                frag.setTranslate(translate);
                frag.setExample(example);
                frag.setNote(note);
            }

        } else if (firstCall) {
            // Do nothing

        } else {

            Intent i = new Intent(getActivity().getApplicationContext(),
                    WordDetailActivity.class);

            i.putExtra(Word.ID, id);
            i.putExtra(Word.NAME, name);
            i.putExtra(Word.TYPE, type);
            i.putExtra(Word.TRANSLATE, translate);
            i.putExtra(Word.EXAMPLE, example);
            i.putExtra(Word.NOTE, note);

            startActivity(i);
        }
        cursor.close();
        }

    }

    public void onWordSaved() {
        getLoaderManager().restartLoader(URL_LOADER, null, this);    
    }

    @Override
    public Loader<Cursor> onCreateLoader(int loaderID, Bundle bundle) {
        String[] projection = { Word.ID, Word.NAME, Word.TYPE, Word.TRANSLATE,
                Word.EXAMPLE, Word.NOTE };

        /* Takes action based on the ID of the Loader that's being created */

        switch (loaderID) {
        case URL_LOADER:

            // Returns a new CursorLoader
            cursorLoader = new CursorLoader(getSherlockActivity(), // Parent
                                                                    // activity
                    // context
                    WordListProvider.WORDS_CONTENT_URI, // Table to query
                    projection, // Projection to return
                    null, // No selection clause
                    null, // No selection arguments
                    null // Default sort order
            );
            return cursorLoader;
        default:
            // An invalid id was passed in
            return null;
        }
    }


    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        adapter.swapCursor(cursor);
    }


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


}
4

1 回答 1

1

尝试添加到您的 Fragment 类:

@Override
protected void onResume()
{
    super.onResume();
    getLoaderManager().restartLoader(URL_LOADER, null, this); 
}


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

清除您的效率问题:当您调用初始 init 时,它将启动,但相同 ID 上的 restartLoader 将自动取消先前的请求并启动新的请求。所以它不会在活动启动时完全执行两次。

Google Loader Dev Doco中的更多信息。

于 2013-10-31T22:30:01.307 回答