Java tutorial
package com.jsw.MngProductDatabase.Presenter; /* * Copyright (c) 2017 Jos Luis del Pino Gallardo. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * jose.gallardo994@gmail.com */ import android.app.Activity; import android.app.LoaderManager; import android.content.Context; import android.content.Loader; import android.database.Cursor; import android.os.Bundle; import android.support.v4.widget.CursorAdapter; import com.jsw.MngProductDatabase.Cursors.CategoryCursorLoader; import com.jsw.MngProductDatabase.Model.Category; import com.jsw.MngProductDatabase.database.DatabaseManager; import com.jsw.MngProductDatabase.interfaces.ICategoryPresenter; /** * Created by usuario on 26/01/17. */ public class CategoryPresenter implements ICategoryPresenter, LoaderManager.LoaderCallbacks<Cursor> { private final static int CATEGORY = 1; private ICategoryPresenter.View view; private Context context; public CategoryPresenter(ICategoryPresenter.View view) { this.view = view; this.context = view.getContext(); } @Override public void getAllCategories(CursorAdapter adapter) { //Cursor cursor = DatabaseManager.getInstance().getAllCategories(); //adapter.swapCursor(cursor); ((Activity) context).getLoaderManager().initLoader(CATEGORY, null, this); //Inicializamos el cargador dentro del presentador. } public void addCategory(Category category) { DatabaseManager.getInstance().addCategory(category); this.restartLoader(); } private void restartLoader() { ((Activity) context).getLoaderManager().restartLoader(CATEGORY, null, this); } /** * Instantiate and return a new Loader for the given ID. * * @param id The ID whose loader is to be created. * @param args Any arguments supplied by the caller. * @return Return a new Loader instance that is ready to start loading. */ @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { CategoryCursorLoader ccl; switch (id) { case CATEGORY: ccl = new CategoryCursorLoader(context); break; default: ccl = null; } return ccl; } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { view.setCursorCategory(data); } /** * Called when a previously created loader is being reset, and thus * making its data unavailable. The application should at this point * remove any references it has to the Loader's data. * * @param loader The Loader that is being reset. */ @Override public void onLoaderReset(Loader<Cursor> loader) { view.setCursorCategory(null); } }