Wednesday, 29 December 2010

Multiple SQL Tables Java Android?

How do you create multiple SQLite database tables in Java for an Android application?

I was having a really frustrating time of things when I was attempting to create multiple SQL tables for a single database for my Android App. It was a complete nightmare!

The Eclipse console kept on telling me that it could not find the table I had defined. I checked the code over and over again and all looked good so could not for the life of me figure out the problem!

Anyway it seems that the solution was to rename the database name in the code, before recompiling and starting again. Each time you make a change to your SQL database, you need to rename the database so a new version of it is created with the new settings. You do this by manually changing its name in the code, or by changing its version number.

Anyway, here is an example of how to create 2 SQL tables for a single SQL database in Java:

Define two SQL tables in the usual way:


public static final String DATABASE_CREATE= "create table " + DATABASE_TABLE + " (_id integer primary key autoincrement, col1 text not null);";

public static final String DATABASE_CREATE2= "create table " + DATABASE_TABLE_2+ " (_id integer primary key autoincrement, col1 text not null, col2 text not null, col3 text not null);";



Then you create each table seperately within the onCreate method of your SQL database adapter class:


public void onCreate(SQLiteDatabase db)
{
db.execSQL(DATABASE_CREATE);
db.execSQL(DATABASE_CREATE2);
}


The error some people make is trying to do everything in one big string. Just to exactly the same as you did with your first table for other tables you add. Change the database name (or version) recompile, and all should be working nicely. Hopefully!

Sunday, 26 December 2010

Java - How to Convert String to Date and Date to String for Android App

To convert a date in string format to a date in java date format the java SimpleDateFormat method is used. You define your own custom date structure using the java simple date format.

In my code below I use the format “EEE MMM dd HH:mm:ss”. This means the date will, for example, look something like this: “Mon Jan 04 07:40:22 2011”.

The string you have needs to exactly match this format. For a list of the different ways of setting a simple date string see http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html

What I was trying to do with my android app was to convert the published date string that the Google Search API gave me (within a JSON object) as a query result. The initial date string that Google gave me was not the way I wanted to structure my date so I used the java substring method to manually alter the string content to the required format before I performed the date conversion below:

SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy");

Date mydate = sdf.parse(datestring);


To do the opposite, to convert the date back to a string, you can use the following code:

SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy");

String datestring = sdf.format(date);


Thanks for reading, please check back at the Android Adam blog for more Java and Android solutions.

Wednesday, 22 December 2010

Android Emulator Internet Not Working

One of the first Android problems I encountered when developing my Android App was that I was unable to get the Android AVD emulator to connect to the Internet. It was causing an Android Twitter4J exception. (Twitter4J is the library I was using that needed access to the internet).

In order to allow your Android Application (whether being run on an Android Phone or the Android AVD Emulator) to access the Internet you need to add the following android.permission.INTERNET line to the Android Manifest File:


   </application>

<uses-permission name="android.permission.INTERNET"></uses-permission>

</manifest>

Welcome to the Android Adam Android Development Blog

Welcome to my Android blog! I will use this blog to post quick and easy solutions to specifc Android and Java problems that I come across as I develop my own Android Applications.

 My name is Adam and I have recently started teaching myself the programming required to develop applications for the Google Android phone. I am throwing myself right in at the deep end and am learning on the fly as I develop my first Android App.

I come from an engineering background and have a degree in Electronic Engineering and a Masters in Communication Networks and Software. I have experience with C++ and Java programming, but I must confess that until now I always did my best to avoid programming! I always used to get a bit lost after the Hello World examples! But now I am starting to really get into it and find my engineering background does speed the progress along nicely.

I am not an expert by any means but I find that I spend many hours trawling the Internet for example code, tutorials, and solutions to many a nasty error message that appears on my screen!

I will use this blog to post solutions that I have found to work to solve the various problems I have come across (and will come across in the future) in the hope that others will be able to gain use from them and speed up their own Internet search when looking for a quick example of how something is done.

I hope you find this blog useful, and overtime as the blog develops with new solutions to different problems, I hope that this will become a really useful resource and one of the first places worth checking for an answer to an Android problem.

All the best and please stop by again soon,
Android Adam