com.armtimes.fragments.FragmentAdapter.java Source code

Java tutorial

Introduction

Here is the source code for com.armtimes.fragments.FragmentAdapter.java

Source

/*
 * Copyright (C) 2015 Victor Apoyan.
 *
 * 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 com.armtimes.fragments;

import android.content.Context;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.support.v4.widget.CursorAdapter;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TableRow;
import android.widget.TextView;

import com.armtimes.R;
import com.armtimes.database.NewsContract;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class FragmentAdapter extends CursorAdapter {

    private final static TableRow.LayoutParams params2f = new TableRow.LayoutParams(0,
            TableRow.LayoutParams.WRAP_CONTENT, 2f);
    private final static TableRow.LayoutParams params3f = new TableRow.LayoutParams(
            TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 0f);

    public FragmentAdapter(Context context, Cursor cursor) {
        super(context, cursor, 0);
    }

    // The newView method is used to inflate a new view and return it,
    // you don't bind any data to the view at this point.
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View view = LayoutInflater.from(context).inflate(R.layout.row_single_news_article, parent, false);
        ViewHolder viewHolder = new ViewHolder(view, cursor);
        view.setTag(viewHolder);
        return view;
    }

    // The bindView method is used to bind all data to a given view
    // such as setting the text on a TextView.
    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        ViewHolder viewHolder = (ViewHolder) view.getTag();
        final String title = cursor.getString(viewHolder.titleIndex);
        final String description = cursor.getString(viewHolder.shortDescriptionIndex);
        final int isRead = cursor.getInt(viewHolder.isRead);
        viewHolder.textViewInfo.setText(Html.fromHtml(String.format("<b>%s</b> %s", title, description)));

        final String imagePath = cursor.getString(viewHolder.imagePathIndex);
        if (imagePath != null && !imagePath.isEmpty()) {
            // Read image from storage.
            InputStream is = null;
            try {
                is = new FileInputStream(new File(imagePath));
                // Set Bitmap to Image View.
                viewHolder.imageViewThumbnail.setImageBitmap(BitmapFactory.decodeStream(is));
                // Set visibility of Image view to VISIBLE.
                viewHolder.imageViewThumbnail.setVisibility(View.VISIBLE);
                // In the case if Image was successfully set to the
                // image view change layout_weight parameter of image
                // view to (=2) and layout_width to (=0) in order to
                // show text and image view in a right way.
                viewHolder.textViewInfo.setLayoutParams(params2f);
            } catch (IOException ignore) {
                viewHolder.textViewInfo.setLayoutParams(params3f);
                // In the case if exception occurs and image can't be set.
                // Hide Thumbnail image.
                viewHolder.imageViewThumbnail.setVisibility(View.GONE);
            } finally {
                if (is != null) {
                    try {
                        is.close();
                    } catch (IOException ignore) {
                    }
                }
            }
        } else {
            viewHolder.imageViewThumbnail.setVisibility(View.GONE);
            viewHolder.textViewInfo.setLayoutParams(params3f);
        }

        view.setBackgroundColor(isRead != 1 ? Color.parseColor("#EEEEEE") : Color.WHITE);
    }

    private static class ViewHolder {

        public final TextView textViewInfo;
        public final ImageView imageViewThumbnail;
        public final int titleIndex;
        public final int shortDescriptionIndex;
        public final int imagePathIndex;
        public final int isRead;

        ViewHolder(final View view, final Cursor cursor) {
            textViewInfo = (TextView) view.findViewById(R.id.tvNewsInfo);
            imageViewThumbnail = (ImageView) view.findViewById(R.id.ivThumbnail);
            titleIndex = cursor.getColumnIndexOrThrow(NewsContract.COL_NAME_NEWS_TITLE);
            shortDescriptionIndex = cursor.getColumnIndexOrThrow(NewsContract.COL_NAME_NEWS_DESC_SHORT);
            imagePathIndex = cursor.getColumnIndexOrThrow(NewsContract.COL_NAME_NEWS_IMAGE_PATH);
            isRead = cursor.getColumnIndexOrThrow(NewsContract.COL_NAME_NEWS_IS_READ);
        }
    }
}