Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright (C) 2014 University of Washington
 *
 * 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.
 */

import android.annotation.SuppressLint;
import android.database.Cursor;

public class Main {
    /**
     * Return the data stored in the cursor at the given index and given position
     * (ie the given row which the cursor is currently on) as null OR a String.
     * <p>
     * NB: Currently only checks for Strings, long, int, and double.
     *
     * @param c
     * @param i
     * @return
     */
    @SuppressLint("NewApi")
    public static String getIndexAsString(Cursor c, int i) {
        // If you add additional return types here be sure to modify the javadoc.
        if (i == -1)
            return null;
        if (c.isNull(i)) {
            return null;
        }
        switch (c.getType(i)) {
        case Cursor.FIELD_TYPE_STRING:
            return c.getString(i);
        case Cursor.FIELD_TYPE_FLOAT: {
            // the static version of this seems to have problems...
            Double d = c.getDouble(i);
            String v = d.toString();
            return v;
        }
        case Cursor.FIELD_TYPE_INTEGER: {
            // the static version of this seems to have problems...
            Long l = c.getLong(i);
            String v = l.toString();
            return v;
        }
        case Cursor.FIELD_TYPE_NULL:
            return c.getString(i);
        default:
        case Cursor.FIELD_TYPE_BLOB:
            throw new IllegalStateException("Unexpected data type in SQLite table");
        }
    }
}