Example usage for android.support.v4.app LoaderManager initLoader

List of usage examples for android.support.v4.app LoaderManager initLoader

Introduction

In this page you can find the example usage for android.support.v4.app LoaderManager initLoader.

Prototype

public abstract <D> Loader<D> initLoader(int id, Bundle args, LoaderManager.LoaderCallbacks<D> callback);

Source Link

Document

Ensures a loader is initialized and active.

Usage

From source file:org.mozilla.search.autocomplete.SuggestionsFragment.java

public void loadSuggestions(String query) {
    final Bundle args = new Bundle();
    args.putString(KEY_SEARCH_TERM, query);
    final LoaderManager loaderManager = getLoaderManager();

    // Ensure that we don't try to restart a loader that doesn't exist. This becomes
    // an issue because SuggestionLoaderCallbacks.onCreateLoader can return null
    // as a loader if we don't have a suggestClient available yet.
    if (loaderManager.getLoader(LOADER_ID_SUGGESTION) == null) {
        loaderManager.initLoader(LOADER_ID_SUGGESTION, args, suggestionLoaderCallbacks);
    } else {/*from w w w  .java2 s .  c  om*/
        loaderManager.restartLoader(LOADER_ID_SUGGESTION, args, suggestionLoaderCallbacks);
    }
}

From source file:com.gh4a.activities.SearchActivity.java

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

    setContentView(R.layout.generic_list);

    ActionBar actionBar = getSupportActionBar();
    actionBar.setTitle(R.string.search);

    LayoutInflater inflater = LayoutInflater.from(UiUtils.makeHeaderThemedContext(this));
    LinearLayout searchLayout = (LinearLayout) inflater.inflate(R.layout.search_action_bar, null);
    actionBar.setCustomView(searchLayout);
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setDisplayHomeAsUpEnabled(true);

    mSearchType = (Spinner) searchLayout.findViewById(R.id.search_type);
    mSearchType.setAdapter(new SearchTypeAdapter(actionBar.getThemedContext(), this));
    mSearchType.setOnItemSelectedListener(this);

    mSearch = (SearchView) searchLayout.findViewById(R.id.search_view);
    mSearch.setIconifiedByDefault(true);
    mSearch.requestFocus();/* w  ww .  java 2s. c  o m*/
    mSearch.setIconified(false);
    mSearch.setOnQueryTextListener(this);
    mSearch.setOnCloseListener(this);
    mSearch.onActionViewExpanded();

    updateSelectedSearchType();

    mResultsView = (RecyclerView) findViewById(R.id.list);
    mResultsView.setLayoutManager(new LinearLayoutManager(this));
    mResultsView.addItemDecoration(new DividerItemDecoration(this));

    if (savedInstanceState != null) {
        mQuery = savedInstanceState.getString(STATE_KEY_QUERY);
        mSearch.setQuery(mQuery, false);

        LoaderManager lm = getSupportLoaderManager();
        int previousMode = savedInstanceState.getInt(STATE_KEY_SEARCH_MODE, SEARCH_MODE_NONE);
        switch (previousMode) {
        case SEARCH_MODE_REPO:
            lm.initLoader(0, null, mRepoCallback);
            break;
        case SEARCH_MODE_USER:
            lm.initLoader(0, null, mUserCallback);
            break;
        case SEARCH_MODE_CODE:
            lm.initLoader(0, null, mCodeCallback);
            break;
        }
    }
}

From source file:com.kaliturin.blacklist.fragments.JournalFragment.java

private void loadListViewItems(String itemsFilter, boolean deleteItems, int listPosition) {
    if (!isAdded()) {
        return;/*from   w w  w.  java 2 s . com*/
    }
    int loaderId = 0;
    JournalItemsLoaderCallbacks callbacks = new JournalItemsLoaderCallbacks(getContext(), cursorAdapter,
            itemsFilter, deleteItems, listView, listPosition);
    LoaderManager manager = getLoaderManager();
    if (manager.getLoader(loaderId) == null) {
        // init and run the items loader
        manager.initLoader(loaderId, null, callbacks);
    } else {
        // restart loader
        manager.restartLoader(loaderId, null, callbacks);
    }
}

From source file:com.kaliturin.blacklist.fragments.ContactsFragment.java

private void loadListViewItems(String itemsFilter, boolean deleteItems, int listPosition) {
    if (!isAdded()) {
        return;// w  w  w  .j  av  a 2s  .  co  m
    }
    int loaderId = 0;
    ContactsLoaderCallbacks callbacks = new ContactsLoaderCallbacks(getContext(), contactType, cursorAdapter,
            itemsFilter, deleteItems, listView, listPosition);
    LoaderManager manager = getLoaderManager();
    if (manager.getLoader(loaderId) == null) {
        // init and run the items loader
        manager.initLoader(loaderId, null, callbacks);
    } else {
        // restart loader
        manager.restartLoader(loaderId, null, callbacks);
    }
}

From source file:de.ub0r.android.callmeter.ui.LogsFragment.java

/**
 * Set Adapter./*from   ww w . j  a  va2  s .c  o  m*/
 *
 * @param forceUpdate force update
 */
public void setAdapter(final boolean forceUpdate) {
    LogAdapter adapter = (LogAdapter) getListAdapter();
    if (!forceUpdate && adapter != null && !adapter.isEmpty()) {
        return;
    }

    String where = DataProvider.Logs.TABLE + "." + DataProvider.Logs.TYPE + " in (-1";
    if (tbCall != null && tbCall.isChecked()) {
        where += "," + DataProvider.TYPE_CALL;
    }
    if (tbSMS != null && tbSMS.isChecked()) {
        where += "," + DataProvider.TYPE_SMS;
    }
    if (tbMMS != null && tbMMS.isChecked()) {
        where += "," + DataProvider.TYPE_MMS;
    }
    if (tbData != null && tbData.isChecked()) {
        where += "," + DataProvider.TYPE_DATA;
    }
    where += ") and " + DataProvider.Logs.TABLE + "." + DataProvider.Logs.DIRECTION + " in (-1";
    if (tbIn != null && tbIn.isChecked()) {
        where += "," + DataProvider.DIRECTION_IN;
    }
    if (tbOut != null && tbOut.isChecked()) {
        where += "," + DataProvider.DIRECTION_OUT;
    }
    where += ")";

    if (planId > 0L && tbPlan != null && tbPlan.isChecked()) {
        String plans = DataProvider.Plans.parseMergerWhere(getActivity().getContentResolver(), planId);
        where = DbUtils.sqlAnd(plans, where);
        Log.d(TAG, "where: ", where);
    }
    Bundle args = new Bundle(1);
    args.putString("where", where);

    LoaderManager lm = getLoaderManager();
    if (lm.getLoader(LOADER_UID) == null) {
        lm.initLoader(LOADER_UID, args, this);
    } else {
        lm.restartLoader(LOADER_UID, args, this);
    }
}

From source file:io.github.tjg1.nori.adapter.ServiceDropdownAdapter.java

/**
 * Create a new adapter that populates a {@link Spinner} with a service selection dropdown.
 *
 * @param context           Android context.
 * @param sharedPreferences Shared preferences (used to restore last selection of the spinner).
 * @param loaderManager     Android support library loader manager.
 * @param adapterView       View populated by this adapter (used to restore last selection of the spinner)
 * @param listener          Listener used to interact with the {@link android.app.Activity} using this
 *                          adapter./*from w w w  .ja v a 2  s  .  com*/
 */
public ServiceDropdownAdapter(@NonNull Context context, @NonNull SharedPreferences sharedPreferences,
        @NonNull LoaderManager loaderManager, @NonNull AdapterView<?> adapterView, @NonNull Listener listener) {
    this.context = context;
    this.sharedPreferences = sharedPreferences;
    this.adapterView = adapterView;
    this.listener = listener;

    // Restore last active item from SharedPreferences.
    lastSelectedItem = this.sharedPreferences.getLong(SHARED_PREFERENCE_LAST_SELECTED_INDEX, 1L);
    // Initialize the search client settings database loader.
    loaderManager.initLoader(LOADER_ID_API_SETTINGS, null, this);
}

From source file:org.totschnig.myexpenses.dialog.TransactionDetailFragment.java

public void fillData(Transaction o) {
    final FragmentActivity ctx = getActivity();
    mLayout.findViewById(R.id.progress).setVisibility(View.GONE);
    mTransaction = o;/* w ww  .j  a va  2 s  . c  o  m*/
    if (mTransaction == null) {
        TextView error = (TextView) mLayout.findViewById(R.id.error);
        error.setVisibility(View.VISIBLE);
        error.setText(R.string.transaction_deleted);
        return;
    }
    boolean doShowPicture = false;
    if (mTransaction.getPictureUri() != null) {
        doShowPicture = true;
        if (mTransaction.getPictureUri().getScheme().equals("file")) {
            if (!new File(mTransaction.getPictureUri().getPath()).exists()) {
                Toast.makeText(getActivity(), R.string.image_deleted, Toast.LENGTH_SHORT).show();
                doShowPicture = false;
            }
        }
    }
    AlertDialog dlg = (AlertDialog) getDialog();
    if (dlg != null) {
        Button btn = dlg.getButton(AlertDialog.BUTTON_POSITIVE);
        if (btn != null) {
            if (mTransaction.crStatus != Transaction.CrStatus.VOID) {
                btn.setEnabled(true);
            } else {
                btn.setVisibility(View.GONE);
            }
        }
        btn = dlg.getButton(AlertDialog.BUTTON_NEUTRAL);
        if (btn != null) {
            btn.setVisibility(doShowPicture ? View.VISIBLE : View.GONE);
        }
    }
    mLayout.findViewById(R.id.Table).setVisibility(View.VISIBLE);
    int title;
    boolean type = mTransaction.getAmount().getAmountMinor() > 0 ? ExpenseEdit.INCOME : ExpenseEdit.EXPENSE;

    if (mTransaction instanceof SplitTransaction) {
        mLayout.findViewById(R.id.SplitContainer).setVisibility(View.VISIBLE);
        //TODO: refactor duplicated code with SplitPartList
        title = R.string.split_transaction;
        View emptyView = mLayout.findViewById(R.id.empty);

        ListView lv = (ListView) mLayout.findViewById(R.id.list);
        // Create an array to specify the fields we want to display in the list
        String[] from = new String[] { KEY_LABEL_MAIN, KEY_AMOUNT };

        // and an array of the fields we want to bind those fields to 
        int[] to = new int[] { R.id.category, R.id.amount };

        // Now create a simple cursor adapter and set it to display
        mAdapter = new SplitPartAdapter(ctx, R.layout.split_part_row, null, from, to, 0,
                mTransaction.getAmount().getCurrency());
        lv.setAdapter(mAdapter);
        lv.setEmptyView(emptyView);

        LoaderManager manager = getLoaderManager();
        if (manager.getLoader(SPLIT_PART_CURSOR) != null && !manager.getLoader(SPLIT_PART_CURSOR).isReset()) {
            manager.restartLoader(SPLIT_PART_CURSOR, null, this);
        } else {
            manager.initLoader(SPLIT_PART_CURSOR, null, this);
        }

    } else {
        if (mTransaction instanceof Transfer) {
            title = R.string.transfer;
            ((TextView) mLayout.findViewById(R.id.AccountLabel)).setText(R.string.transfer_from_account);
            ((TextView) mLayout.findViewById(R.id.CategoryLabel)).setText(R.string.transfer_to_account);
        } else {
            title = type ? R.string.income : R.string.expense;
        }
    }

    String amountText;
    String accountLabel = Account.getInstanceFromDb(mTransaction.accountId).label;
    if (mTransaction instanceof Transfer) {
        ((TextView) mLayout.findViewById(R.id.Account)).setText(type ? mTransaction.label : accountLabel);
        ((TextView) mLayout.findViewById(R.id.Category)).setText(type ? accountLabel : mTransaction.label);
        if (((Transfer) mTransaction).isSameCurrency()) {
            amountText = formatCurrencyAbs(mTransaction.getAmount());
        } else {
            String self = formatCurrencyAbs(mTransaction.getAmount());
            String other = formatCurrencyAbs(mTransaction.getTransferAmount());
            amountText = type == ExpenseEdit.EXPENSE ? (self + " => " + other) : (other + " => " + self);
        }
    } else {
        ((TextView) mLayout.findViewById(R.id.Account)).setText(accountLabel);
        if ((mTransaction.getCatId() != null && mTransaction.getCatId() > 0)) {
            ((TextView) mLayout.findViewById(R.id.Category)).setText(mTransaction.label);
        } else {
            mLayout.findViewById(R.id.CategoryRow).setVisibility(View.GONE);
        }
        amountText = formatCurrencyAbs(mTransaction.getAmount());
    }

    //noinspection SetTextI18n
    ((TextView) mLayout.findViewById(R.id.Date))
            .setText(DateFormat.getDateInstance(DateFormat.FULL).format(mTransaction.getDate()) + " "
                    + DateFormat.getTimeInstance(DateFormat.SHORT).format(mTransaction.getDate()));

    ((TextView) mLayout.findViewById(R.id.Amount)).setText(amountText);

    if (!mTransaction.comment.equals("")) {
        ((TextView) mLayout.findViewById(R.id.Comment)).setText(mTransaction.comment);
    } else {
        mLayout.findViewById(R.id.CommentRow).setVisibility(View.GONE);
    }

    if (!mTransaction.referenceNumber.equals("")) {
        ((TextView) mLayout.findViewById(R.id.Number)).setText(mTransaction.referenceNumber);
    } else {
        mLayout.findViewById(R.id.NumberRow).setVisibility(View.GONE);
    }

    if (!mTransaction.payee.equals("")) {
        ((TextView) mLayout.findViewById(R.id.Payee)).setText(mTransaction.payee);
        ((TextView) mLayout.findViewById(R.id.PayeeLabel)).setText(type ? R.string.payer : R.string.payee);
    } else {
        mLayout.findViewById(R.id.PayeeRow).setVisibility(View.GONE);
    }

    if (mTransaction.methodId != null) {
        ((TextView) mLayout.findViewById(R.id.Method))
                .setText(PaymentMethod.getInstanceFromDb(mTransaction.methodId).getLabel());
    } else {
        mLayout.findViewById(R.id.MethodRow).setVisibility(View.GONE);
    }

    if (Account.getInstanceFromDb(mTransaction.accountId).type.equals(AccountType.CASH)) {
        mLayout.findViewById(R.id.StatusRow).setVisibility(View.GONE);
    } else {
        TextView tv = (TextView) mLayout.findViewById(R.id.Status);
        tv.setBackgroundColor(mTransaction.crStatus.color);
        tv.setText(mTransaction.crStatus.toString());
    }

    if (mTransaction.originTemplate == null) {
        mLayout.findViewById(R.id.PlannerRow).setVisibility(View.GONE);
    } else {
        ((TextView) mLayout.findViewById(R.id.Plan))
                .setText(mTransaction.originTemplate.getPlan() == null ? getString(R.string.plan_event_deleted)
                        : Plan.prettyTimeInfo(getActivity(), mTransaction.originTemplate.getPlan().rrule,
                                mTransaction.originTemplate.getPlan().dtstart));
    }

    dlg.setTitle(title);
    if (doShowPicture) {
        ImageView image = ((ImageView) dlg.getWindow().findViewById(android.R.id.icon));
        image.setVisibility(View.VISIBLE);
        image.setScaleType(ImageView.ScaleType.CENTER_CROP);
        Picasso.with(ctx).load(mTransaction.getPictureUri()).fit().into(image);
    }
}

From source file:com.kafilicious.popularmovies.ui.activity.MainActivity.java

public void loadMovies(String sort, int page) {
    setSubtitle(sort);/*  ww w  .  j ava 2s  .  c o m*/
    if (sort.equalsIgnoreCase(SORT_BY_FAVORITES))
        new FetchFavorites().execute();
    else {
        URL movieRequestUrl = NetworkUtils.buildUrl(sort, page);
        Log.i(TAG, "Formed URL: " + movieRequestUrl);
        Bundle queryBundle = new Bundle();
        queryBundle.putString(SEARCH_QUERY_URL_EXTRA, movieRequestUrl.toString());

        LoaderManager loaderManager = getSupportLoaderManager();

        Loader<MovieList> movieSearchLoader = loaderManager.getLoader(THEMOVIEDB_SEARCH_LOADER);

        if (movieSearchLoader == null) {
            loaderManager.initLoader(THEMOVIEDB_SEARCH_LOADER, queryBundle, this);
            Log.i(TAG, "Loader initialized");
        } else {
            loaderManager.restartLoader(THEMOVIEDB_SEARCH_LOADER, queryBundle, this);
            Log.i(TAG, "Loader restarted");
        }
    }
}

From source file:org.mariotaku.twidere.fragment.UserListFragment.java

public void getUserListInfo(final boolean omitIntentExtra) {
    final LoaderManager lm = getLoaderManager();
    lm.destroyLoader(0);/*from  w  w  w .ja v a 2s .co  m*/
    final Bundle args = new Bundle(getArguments());
    args.putBoolean(EXTRA_OMIT_INTENT_EXTRA, omitIntentExtra);
    if (!mUserListLoaderInitialized) {
        lm.initLoader(0, args, this);
        mUserListLoaderInitialized = true;
    } else {
        lm.restartLoader(0, args, this);
    }
}

From source file:org.totschnig.myexpenses.util.Utils.java

public static void requireLoader(LoaderManager manager, int loaderId, Bundle args,
        LoaderManager.LoaderCallbacks callback) {
    if (manager.getLoader(loaderId) != null && !manager.getLoader(loaderId).isReset()) {
        manager.restartLoader(loaderId, args, callback);
    } else {/*from   w w w  . j a va2 s  .c o  m*/
        manager.initLoader(loaderId, args, callback);
    }
}