从asynctask更新CursorAdapter

时间:2014-03-25 18:13:36

标签: android android-asynctask android-cursoradapter

我有一个问题,通知CursorAdapter数据已被更改。

    public class MyListFragment extends ListFragment implements LoaderCallbacks<Cursor> {

    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
            return new MyListCursorLoader(getActivity());
        }


        public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
            MyCursorAdapter adapter = new MyCursorAdapter(getActivity(),(MyCursor)cursor);
            setListAdapter(adapter);
        }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getLoaderManager().initLoader(0, null, this);
    }

    private static class MyCursorAdapter extends CursorAdapter {

    ... some code ...

    }

    private static class MyListCursorLoader extends SQLiteCursorLoader {

    ... code ...

    }

使用AsyncTask进行SQLite数据库更新(具有公共接口,ListFragment使用侦听器实现),我尝试使用onPostExecute刷新ListAdapter,但我真的不知道如何。

AsyncTask代码

public class MyDownloader extends AsyncTask<Void, Void, Void> {

    public interface OnMyDownloaded {
        void onMyDownloaded();
    }


    public JSONDownloader(Context context, OnMyDownloaded listener) {

            ...code...

    }

    protected Void doInBackground(Void... params) {
            ...code...
    }


    protected void onPostExecute(Void result) {
        listener.onMyDownloaded();
    }

请帮助我理解我应该在MyListFragment内部类

中做什么
private class OnEventDownloaded implements OnJSONDownloaded {
    ?????
}

1 个答案:

答案 0 :(得分:0)

你可以致电getLoaderManager().restartLoader(int id, Bundle args, LoaderCallbacks<D> callback); 它将调用onCreateLoader,因此将重新创建加载器。如果我理解正确的话。