com.lkunic.lib.activityaddonlib.twopane.ItemListCursorAdapter.java Source code

Java tutorial

Introduction

Here is the source code for com.lkunic.lib.activityaddonlib.twopane.ItemListCursorAdapter.java

Source

/**********************************************************************
 * Copyright (c) 2015 Luka Kunic, "ItemListCursorAdapter.java"
 * 
 * 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.
 *
 * Author: lkunic
 * Last modified: 08/02/2015
 **********************************************************************/
package com.lkunic.lib.activityaddonlib.twopane;

import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Provides a way to populate a grid with data received from a data set.
 */
public abstract class ItemListCursorAdapter extends CursorAdapter {
    private LayoutInflater mLayoutInflater;

    public ItemListCursorAdapter(Context context, Cursor c) {
        super(context, c, 0);

        mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public void bindView(View convertView, Context context, Cursor cursor) {
        setupContent(convertView, cursor);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        // Inflate the view
        View view = mLayoutInflater.inflate(getListItemLayoutResourceId(), parent, false);

        // Setup the view holder object
        setupViewHolder(view);

        return view;
    }

    /**************************************************************************************************/
    /**** Abstract methods ****/
    /**************************************************************************************************/

    /**
     * Implementation of this method should return a valid resource id for a grid item layout.
     */
    protected abstract int getListItemLayoutResourceId();

    /**
     * Implement this to set up a view holder object used for storing references to views in the layout.
     * Create a static ViewHolder structure with references to all views that will be populated in the 
     * bindView() method. Store the object as a tag in the given parent view.
     * @param view Parent view of the grid item layout.
     */
    protected abstract void setupViewHolder(View view);

    /**
     * Implement this to set up content of the view. The view is already inflated and should contain a
     * ViewHolder object (if it has been added as a tag to the view in setupViewHolder() method).
     * @param view ConvertView that is used to display data for a single grid item.
     * @param cursor Cursor containing data for the grid item.
     */
    protected abstract void setupContent(View view, Cursor cursor);
}