按下后退按钮时,光标加载器应重新启动

时间:2013-10-31 21:50:19

标签: android android-cursorloader

我有一个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);
    }


}

1 个答案:

答案 0 :(得分: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中的更多信息。