Databases and Menus
Two for the price of one in this tutorial, actually. We're going to learn about SQLite databases and menus. I'm hoping you'll at least be slightly familiar with databases already, though SQLite is really quite simple. You can pick up the basics, of which we'll be using in this tutorial, at the following links:

It may look a little intimidating, but once you get to grips with it, it's actually quite close to English.

We won't be creating the most sexy application, but databases are an important aspect of a lot of software design, including android.

Let's get to it. As usual, we're creating a package:

package dreamincode.tutorials.dic_tut4;

...and we'll import the usual suspects.

import android.app.Activity;
import android.os.Bundle;

Now there's quite a lot that we're going to import that hasn't already been covered, but do not panic. I'll explain it all as we go along, and it's honestly nothing to worry about. First up, a ListView. This is just another widget, and it's... well, it's a list.

import android.widget.ListView;

Next, we'll import the stuff related to the menu. We'll be using a Menu, and a couple of MenuItems.

import android.view.Menu;
import android.view.MenuItem;

The next bits are related to the database. The first is self-explanatory, it's a database. The second is a Cursor. A cursor is a way we can store temporary results from a table in memory. (We select stuff into it, explained a little later... keep reading!)

And finally, we're going to import an ArrayList to add our results into, and an ArrayAdapter to basically plug our ArrayList into the list. Again, this will be explained later, at this point all we really have is an overview.

import android.widget.ArrayAdapter;
import java.util.ArrayList;

On to the main part of the code, now. First off, we'll open up our class and add a few variables. We'll have a ListView, an SQLiteDatabase, and a few ints for the IDs of our menu items.

So, the start is nothing new. Nothing to worry about here. Then we initialise the list. Nothing new here, either. It's exactly the same as when we created a dynamic layout in part 3. Then we do some new things with our database. First we use openOrCreateDatabase("dic_tut4", MODE_PRIVATE, null);
-- this creates a database if it doesn't exist, or opens it if it does.
-- parameter 1 is the name of the database we're looking to create/open.
-- parameter 2 is the mode, doesn't require much explanation at this point. (Don't want to give you an information overload)
-- parameter 3, you don't even need to worry about. If you're interested, do some digging on CursorFactory.

Then another new line: this.db.execSQL("CREATE TABLE IF NOT EXISTS table_of_dics(value REAL)");
-- this will create a table if it doesn't already exist
-- it will add a single field into the table, called value. It is of type REAL, which is just like a double in Java.

And finally, this.update_list(); ... this is actually a custom method we'll be writing to query the database and populate the list. More info later, for now let's focus on the menu.

Creating a menu
Simple stuff actually. We use onCreateOptionsMenu(), which is called when the user hits the menu button.

We're simply adding 2 items: "Add", and "Quit". We also set the icons for them using some default android resources. If you want a full list, this site is pretty cool.

The next method we'll be using is onOptionsItemSelected(), which is called when the user selects an item in the menu. We use the IDs we assigned in onCreateOptionsMenu() MENU_ADD and MENU_QUIT to decide what to do.

So, we're using a switch() to find the ID of the menu item which called this method. If it's add, we're going to perform some SQL and insert a random value into the table. Then we'll update the list, just like we did in onCreate(). (Remember, we're defining this method ourselves later)

If the user selected the "Quit" option, we'll simple close the database and finish the activity.

NOTE: The SQL used here, INSERT INTO will insert a random value into the table. Remember to refer to the links at the beginning if you get stuck on the SQL.

Lastly, we're going to define our method for updating the list. It's quite simple, but the code is quite different to what we've done so far. If you've ever done any database work through software/web before, you may notice a few similarities here. We basically execute the query. Then loop through each row in the result, adding it into an ArrayList. Lastly, we set the adapter for the list to display the arraylist we created. The code looks a tad complex, but it's really quite simple:

I'll break this one up:ArrayList<String> db_results = new ArrayList<String>(); -- create an ArrayList, where we'll store the results to display in the list.

Cursor cursor = db.rawQuery("SELECT value FROM table_of_dics ORDER BY value", null); -- create a cursor based on the query (this select statement will return all the values stored in the table, and order them)

while(cursor.moveToNext()) { -- while there is still something in the cursor to read (while we still have another row to read)

db_results.add(String.valueOf(cursor.getDouble(cursor.getColumnIndex("value")))); -- add the value from the row into the arraylist we created.

} -- this is self explanatory, really.

cursor.close(); -- close the cursor: we're done with it now. We have all our results in the ArrayList, which we can now set as the adapter:

this.list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, db_results)); -- set the adapter.
- parameter 1: the activity we want it for
- parameter 2: the layout type (we can add multiple widgets to a list item, but simple_list_item_1 is a default in android: simply a single TextView.
- parameter 3: the arraylist which we created and populated earlier.

Great tutorials! Are there any other resources for learning Android/java coding? Most of the books/tutorials and examples I've tried elsewhere fail due to version issues with either java, android, or eclipse. Every one of the dreamincode tutorials work flawlessly but I'm still a little fuzzy about the java syntax for Android. I created an app with AppInventor and I wanted to develop it more with Eclipse but the AppInventor doesn't let you see the java code so I would have to rewrite the app from scratch and I'm running into a mountain of problems primarily caused by syntax errors. Part of my app require some simple math but I have no idea how to multiply a preset value by a dynamic (user entered) value. Unfortunately my programming experience is limited to VB and C++. I'm a visual and hands-on learner so just reading a bunch of terms and jargon doesn't quite get me there. Thanks for your time and suggestions on where to go to get a better grip on java/android coding and syntax.

I was wondering how I could get my hands on a table made like in your example with something like this. I am not able to find neither the damn table nor the database that holds it with the file explorer the app gives me

You need to provide us with some more info.
I recommend you to start a new topic about you problem in the Android forum.
This way, more of us Android people will see your post and can help you.
And to spear you some time, post information from the logcat

Unfortunately there isn't much else I can say on the issue. All the code is exactly the same as it is in the tutorial but when I run the program in avd it goes directly to giving me the following error message:

I don't get any other error messages or hints in Eclipse, just the error in avd when the program is executed.

That's why I am a bit baffled! The only thing that is different to the tutorial is that my AVD is set to 2.2 rather than 1.5, but I can't see that making much of a difference. If I had any more info I'd be glad to share it!

The logcat will tell you where the exception is and what fired it.
But yes, i can be because you are using a other android version.
every new version uses a new API level.
the same functions are still there, but maybe accessible vid other methods.