/*
* This file is part of Shadowlands RoadTrip - A vehicle logbook for Android.
*
* Copyright (C) 2010 Jeremy D Monin <jdmonin@nand.net>
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.shadowlands.roadtrip.android;
import org.shadowlands.roadtrip.db.Person;
import org.shadowlands.roadtrip.db.RDBAdapter;
import org.shadowlands.roadtrip.db.Vehicle;
import android.content.Context;
import android.database.sqlite.SQLiteException;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
/**
* Fill the data in spinners of data-backed object types,
* such as {@link Vehicle} or {@link Person} (driver).
*
* @author jdmonin
*/
public class SpinnerDataFactory
{
public static Person NEW_DRIVER; // TODO keep?
public static Person NEW_VEHICLE; // TODO keep?
/**
* Populate a spinner from the drivers (Persons) in the database.
* @param db connection to use
* @param ctx the calling Activity or Context
* @param sp spinner to fill with the drivers
* @param currentID If not -1, the driver _id to select in the Spinner.
* @return true on success, false if could not populate from database
*/
public static boolean setupDriversSpinner(RDBAdapter db, Context ctx, Spinner sp, final int currentID)
{
Person[] drivers = populateDriversList(db);
if (drivers == null)
return false;
ArrayAdapter<Person> daa = new ArrayAdapter<Person>(ctx, android.R.layout.simple_spinner_item, drivers);
daa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(daa);
if (currentID != -1)
{
for (int i = drivers.length - 1; i >= 0; --i)
if (currentID == drivers[i].getID())
{
sp.setSelection(i);
break;
}
}
return true;
}
/**
* Populate a spinner from the Vehicles in the database.
* @param db connection to use
* @param ctx the calling Activity or Context
* @param sp spinner to fill with the vehicles
* @param currentID If not -1, the driver _id to select in the Spinner.
* @return true on success, false if could not populate from database
*/
public static boolean setupVehiclesSpinner(RDBAdapter db, Context ctx, Spinner sp, final int currentID)
{
Vehicle[] veh = populateVehiclesList(db);
if (veh == null)
return false;
ArrayAdapter<Vehicle> daa = new ArrayAdapter<Vehicle>(ctx, android.R.layout.simple_spinner_item, veh);
daa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(daa);
if (currentID != -1)
{
for (int i = veh.length - 1; i >= 0; --i)
if (currentID == veh[i].getID())
{
sp.setSelection(i);
break;
}
}
return true;
}
/**
* For {@link #setupDriversSpinner(RDBAdapter, Context, Spinner)},
* gather the list of drivers (Persons) from the database.
*
* @param db connection to use
* @return array of drivers (Persons), or null
*/
private static Person[] populateDriversList(RDBAdapter db)
{
Person[] drivers = null;
try
{
drivers = Person.getAll(db, true);
}
catch (SQLiteException e)
{}
return drivers;
}
/**
* For {@link #setupVehiclesSpinner(RDBAdapter, Context, Spinner)},
* gather the list of vehicles from the database.
*
* @param db connection to use
* @return array of vehicles, or null
*/
private static Vehicle[] populateVehiclesList(RDBAdapter db)
{
Vehicle[] veh = null;
try
{
veh = Vehicle.getAll(db);
}
catch (SQLiteException e)
{}
return veh;
}
}
|