package com.applikey.babybook.loaders;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.AsyncTaskLoader;
import android.util.Log;
import com.applikey.babybook.database.DataHelper;
import com.applikey.babybook.activity.MainActivity;
import com.applikey.babybook.utils.Lo;
/**
* Created by alex on 1/20/14.
*/
public abstract class BaseLoader extends AsyncTaskLoader<Object>{
private final Lo lo = new Lo(BaseLoader.this);
public static final String ARG_ID = "id";
public static final String ARG_ARGUMENT = "argument";
public static final String ARG_TASK_NAME = "taskName";
protected DataHelper dataHelper = new DataHelper();
public BaseLoader(Context context) {
super(context);
}
@Override
protected void onStartLoading() {
super.onStartLoading();
lo.g("onStartLoading");
forceLoad();
}
public static void restartTaskLoader (FragmentActivity activity,LoaderManager.LoaderCallbacks callback, String taskClassName, int...args) {
Lo.gt("BaseLoader", "restartTaskLoader, taskClassName: " + taskClassName);
Bundle bundle = new Bundle();
bundle.putString(ARG_TASK_NAME, taskClassName);
for (int i =1; i<=args.length; i++) {
bundle.putInt(ARG_ARGUMENT+i, args[i-1]);
}
int task_id = MainActivity.getLoaderIdMap().getID(callback.getClass().getName(), taskClassName);
activity.getSupportLoaderManager().restartLoader(task_id, bundle, callback);
}
public static void printCursor(Cursor cursor) {
String l = "Cursor";
String divider = "************************************************************";
String[] columns = cursor.getColumnNames();
if (cursor.moveToFirst()){
do {
for (String s : columns) {
Log.d(l, s + ": " + cursor.getString(cursor.getColumnIndex(s)));
}
} while (cursor.moveToNext());
}
Log.d(l, divider);
}
}