org.aquabase.DetailsFragment.java Source code

Java tutorial

Introduction

Here is the source code for org.aquabase.DetailsFragment.java

Source

/*
 *  This file is part of AquaBase.
 *
 *  AquaBase is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  AquaBase is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with AquaBase.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  Copyright (c) 2014 Cdric Bosdonnat <cedric@bosdonnat.fr>
 */
package org.aquabase;

import java.util.Vector;

import android.database.Cursor;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Pair;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;

public class DetailsFragment extends Fragment {

    private Vector<Integer> mLabelIds = new Vector<Integer>();
    private Vector<String> mValues = new Vector<String>();

    public static DetailsFragment createInstance(Cursor cursor, Vector<Pair<String, Integer>> columns) {
        DetailsFragment fragment = new DetailsFragment();
        fragment.setData(cursor, columns);

        return fragment;
    }

    public void setData(Cursor cursor, Vector<Pair<String, Integer>> columns) {
        mLabelIds.clear();
        mValues.clear();

        for (Pair<String, Integer> pair : columns) {
            String columnName = pair.first;
            String value = new String();

            int columnIndex = cursor.getColumnIndexOrThrow(columnName);
            switch (cursor.getType(columnIndex)) {
            case Cursor.FIELD_TYPE_STRING:
                value = cursor.getString(columnIndex);
                break;
            case Cursor.FIELD_TYPE_FLOAT:
                value = Double.toString(cursor.getDouble(columnIndex));
                break;
            case Cursor.FIELD_TYPE_INTEGER:
                value = Integer.toString(cursor.getInt(columnIndex));
                break;
            default:
            }

            mLabelIds.add(pair.second);
            mValues.add(value);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
        View rootView = inflater.inflate(R.layout.fragment_details, container, false);

        // Fill the table layout with rows for the fields depending on the item
        TableLayout table = (TableLayout) rootView.findViewById(R.id.TableLayout);

        for (int i = 0; i < mLabelIds.size(); i++) {
            // Don't switch to an inflated XML file for each row: way too slow
            TableRow row = new TableRow(getActivity());
            row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            row.setPadding(0, 3, 0, 3);

            TextView titleView = new TextView(getActivity());
            titleView.setText(mLabelIds.get(i));
            titleView.setPadding(0, 0, 12, 0);
            titleView.setGravity(Gravity.TOP);
            titleView.setTypeface(Typeface.DEFAULT_BOLD);
            row.addView(titleView);

            TextView valueView = new TextView(getActivity());
            valueView.setText(mValues.get(i));
            titleView.setGravity(Gravity.TOP);
            row.addView(valueView);

            table.addView(row, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        }

        return rootView;
    }
}