Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;

public class Main {
    /** Name of the table */
    private static final String TABLE_NAME = "s";

    /**
     * Get specific row values from the database,
     * e.g. all years stored in the database or
     *      all months of a year stored in the database or
     *      all days in a month of a year stored in the database
     *
     * @param db
     *            pointer to database
     * @param requestField
     *            requested row from db
     *            "year" returns all year values found
     *            "month" returns all month values found in year <code>requestLimiterYear</code>
     *            "day" returns all day values found in month <code>requestLimiterMonth</code>
     *                                              and year <code>requestLimiterYear</code>
     * @param requestLimiterMonth
     *            limiter for request
     *            unused if requestField is "year"
     *            unused if requestField is "month"
     *            month if requestField is "day"
     * @param requestLimiterYear
     *            limiter for request
     *            unused if requestField is "year"
     *            year if requestField is "month"
     *            year if requestField is "day"
     *
     * @return <code>ArrayList<Integer></code>
     *            array list with all entries found
     */
    public static ArrayList<Integer> getEntries(SQLiteDatabase db, String requestField, int requestLimiterMonth,
            int requestLimiterYear) {

        /** Array list holding the found values */
        ArrayList<Integer> returnArray = new ArrayList<>();

        /** Limiter for row search */
        String queryRequest = "select distinct " + requestField + " from " + TABLE_NAME;
        if (requestField.equalsIgnoreCase("day")) {
            queryRequest += " where month = " + String.valueOf(requestLimiterMonth) + " and year = "
                    + String.valueOf(requestLimiterYear);
        } else if (requestField.equalsIgnoreCase("month")) {
            queryRequest += " where year = " + String.valueOf(requestLimiterYear);
        }

        /** Cursor holding the records of a day */
        Cursor allRows = db.rawQuery(queryRequest, null);

        allRows.moveToFirst();
        for (int i = 0; i < allRows.getCount(); i++) {
            returnArray.add(allRows.getInt(0));
            allRows.moveToNext();
        }
        allRows.close();
        return returnArray;
    }
}