io.pivotal.arca.fragments.ArcaItemSupportFragment.java Source code

Java tutorial

Introduction

Here is the source code for io.pivotal.arca.fragments.ArcaItemSupportFragment.java

Source

/* 
 * Copyright (C) 2014 Pivotal Software, Inc. 
 * 
 * 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.
 */
package io.pivotal.arca.fragments;

import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.CursorAdapter;
import android.view.View;

import io.pivotal.arca.dispatcher.QueryResult;

public abstract class ArcaItemSupportFragment extends ArcaQuerySupportFragment {

    public abstract CursorAdapter onCreateAdapter(final View view, final Bundle savedInstanceState);

    private CursorAdapter mAdapter;

    @Override
    public void onViewCreated(final View view, final Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        setupAdapterView(view, savedInstanceState);
    }

    private void setupAdapterView(final View view, final Bundle savedInstanceState) {
        mAdapter = onCreateAdapter(view, savedInstanceState);
    }

    public CursorAdapter getCursorAdapter() {
        return mAdapter;
    }

    @Override
    public final void onRequestComplete(final QueryResult result) {
        if (result.hasError()) {
            onContentError(result.getError());
        } else {
            getCursorAdapter().swapCursor(result.getResult());
            bindViewAtPosition(0);
            onContentChanged(result);
        }
    }

    public void bindViewAtPosition(final int position) {
        final CursorAdapter adapter = getCursorAdapter();
        final Cursor cursor = adapter.getCursor();
        if (cursor != null && cursor.moveToPosition(position)) {
            adapter.bindView(getView(), getActivity(), cursor);
        }
    }

    @Override
    public final void onRequestReset() {
        getCursorAdapter().swapCursor(null);
        onContentReset();
    }

    protected void onContentChanged(final QueryResult result) {
    }

    protected void onContentError(final io.pivotal.arca.dispatcher.Error error) {
    }

    protected void onContentReset() {
    }
}