Subversion Repositories ES

Compare Revisions

Last modification

Ignore whitespace Rev 10 → Rev 11

/trunk/src/de/pointedears/converter/db/CurrenciesDatabase.java
New file
0,0 → 1,90
/**
*
*/
package de.pointedears.converter.db;
 
import java.util.HashMap;
import java.util.Map.Entry;
 
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import de.pointedears.converter.app.CurrenciesActivity;
 
/**
* @author pelinux
*
*/
public class CurrenciesDatabase extends SQLiteOpenHelper
{
private static final String DATABASE_NAME = "currency.db"; //$NON-NLS-1$
private static final int DATABASE_VERSION = 1;
 
public static final String TABLE = "currency"; //$NON-NLS-1$
public static final String COLUMN_CURRENCY1 = "currency1"; //$NON-NLS-1$
public static final String COLUMN_CURRENCY2 = "currency2"; //$NON-NLS-1$
public static final String COLUMN_FACTOR = "factor"; //$NON-NLS-1$
 
private final CurrenciesActivity context;
 
/**
* @param context
* The Activity in which this wrapper is used
*/
public CurrenciesDatabase(CurrenciesActivity context)
{
super(context, CurrenciesDatabase.DATABASE_NAME, null,
CurrenciesDatabase.DATABASE_VERSION);
this.context = context;
}
 
/*
* (non-Javadoc)
*
* @see
* android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite
* .SQLiteDatabase)
*/
@SuppressWarnings("nls")
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL("CREATE TABLE IF NOT EXISTS " + CurrenciesDatabase.TABLE
+ "(" + CurrenciesDatabase.COLUMN_CURRENCY1 + " TEXT, "
+ CurrenciesDatabase.COLUMN_CURRENCY2 + " TEXT, "
+ CurrenciesDatabase.COLUMN_FACTOR
+ " NUMERIC)");
 
HashMap<String, HashMap<String, Double>> currencyConversions =
this.context.getCurrencyConversions();
for (String key : currencyConversions.keySet())
{
for (Entry<String, Double> factorEntry : currencyConversions.get(key)
.entrySet())
{
ContentValues values = new ContentValues();
values.put(CurrenciesDatabase.COLUMN_CURRENCY1, key);
values.put(CurrenciesDatabase.COLUMN_CURRENCY2, factorEntry.getKey());
values.put(CurrenciesDatabase.COLUMN_FACTOR, factorEntry.getValue());
db.insert(CurrenciesDatabase.TABLE, CurrenciesDatabase.COLUMN_FACTOR,
values);
}
}
}
 
/*
* (non-Javadoc)
*
* @see
* android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite
* .SQLiteDatabase, int, int)
*/
@SuppressWarnings("nls")
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
/* NOTE: Should migrate database instead */
db.execSQL("DROP TABLE IF EXISTS " + CurrenciesDatabase.TABLE);
this.onCreate(db);
}
}
Property changes:
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Index: trunk/src/de/pointedears/converter/app/CurrenciesActivity.java
===================================================================
--- trunk/src/de/pointedears/converter/app/CurrenciesActivity.java (revision 10)
+++ trunk/src/de/pointedears/converter/app/CurrenciesActivity.java (revision 11)
@@ -3,6 +3,9 @@
import java.util.HashMap;
import android.app.Activity;
+import android.database.Cursor;
+import android.database.sqlite.SQLiteDatabase;
+import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.text.Editable;
import android.view.KeyEvent;
@@ -16,6 +19,7 @@
import android.widget.EditText;
import android.widget.Spinner;
import de.pointedears.converter.R;
+import de.pointedears.converter.db.CurrenciesDatabase;
/**
* Activity that implements currency conversion
@@ -31,7 +35,7 @@
private static final String VALUE_EUR = "EUR"; //$NON-NLS-1$
private static final String VALUE_USD = "USD"; //$NON-NLS-1$
- private static HashMap<String, HashMap<String, Double>> currencyConversions;
+ protected static HashMap<String, HashMap<String, Double>> currencyConversions;
/* Unit spinners (dropdowns) */
private Spinner spinnerUnit1;
@@ -45,7 +49,7 @@
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_currencies);
- /* TODO: Set up currency database */
+ /* Set up currency database */
CurrenciesActivity.currencyConversions =
new HashMap<String, HashMap<String, Double>>();
@@ -57,7 +61,7 @@
conversionFactors = new HashMap<String, Double>();
conversionFactors.put(CurrenciesActivity.VALUE_CHF, 1.30235077);
- conversionFactors.put(CurrenciesActivity.VALUE_CHF, 1.3468);
+ conversionFactors.put(CurrenciesActivity.VALUE_USD, 1.3468);
CurrenciesActivity.currencyConversions.put(CurrenciesActivity.VALUE_EUR,
conversionFactors);
@@ -67,6 +71,64 @@
CurrenciesActivity.currencyConversions.put(CurrenciesActivity.VALUE_USD,
conversionFactors);
+ /* Create database with values above if it does not exist */
+ CurrenciesDatabase db = new CurrenciesDatabase(this);
+ try
+ {
+ SQLiteDatabase dbConn = db.getReadableDatabase();
+
+ @SuppressWarnings("nls")
+ Cursor myCursor =
+ dbConn.query(true, CurrenciesDatabase.TABLE, null, null, null, null,
+ null, CurrenciesDatabase.COLUMN_CURRENCY1 + ","
+ + CurrenciesDatabase.COLUMN_CURRENCY2, null);
+
+ @SuppressWarnings({ "unused", "nls" })
+ String queryResult = "";
+ if (myCursor != null)
+ {
+ try
+ {
+ int currency1Id =
+ myCursor
+ .getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_CURRENCY1);
+ int currency2Id =
+ myCursor
+ .getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_CURRENCY2);
+ int factorId =
+ myCursor.getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_FACTOR);
+
+ if (myCursor.moveToFirst())
+ {
+ do
+ {
+ String currency1Str = myCursor.getString(currency1Id);
+ String currency2Str = myCursor.getString(currency2Id);
+ Double factor = myCursor.getDouble(factorId);
+
+ /* DEBUG */
+ queryResult +=
+ currency1Str + " --> " + currency2Str + ": " + factor + "\n";
+ }
+ while (myCursor.moveToNext());
+ }
+ }
+ catch (IllegalArgumentException e)
+ {
+ /* Could not retrieve column index */
+ e.printStackTrace();
+ }
+ }
+
+ /* TODO: Close only on exit */
+ dbConn.close();
+ }
+ catch (SQLiteException e1)
+ {
+ /* Could not open database */
+ e1.printStackTrace();
+ }
+
final EditText editValue1 =
(EditText) this.findViewById(R.id.currencies_edit_value1);
final EditText editValue2 =
@@ -229,4 +291,12 @@
/* update here */
return super.onOptionsItemSelected(item);
}
+
+ /**
+ * @return
+ */
+ public HashMap<String, HashMap<String, Double>> getCurrencyConversions()
+ {
+ return CurrenciesActivity.currencyConversions;
+ }
}
/trunk/bin/resources.ap_
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
/trunk/bin/classes.dex
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
/trunk/bin/Converter.apk
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream