tags:

views:

61

answers:

1

In my application, after the user presses the appropriate button, an activity starts that creates a database with one table with two columns. The thing I can't seem to get is: I would like one of the columns to automatically fill with the days of the week so that when I return a cursor over the database for the listActivity, the days are shown. I can get the database to build but I can't seem to get the column to fill with the days. I tried this

public void installDays()
{
   String[] day = new String[7];;
   ContentValues initialValues = new ContentValues();
   day[0] = "Monday";
   day[1] = "Tuesday";
   day[2] = "Wednesday";
   day[3] = "Thursday";
   day[4] = "Friday";
   day[5] = "Saturday";
   day[6] = "Sunday";
   for(int i = 0; i < 7; i++)
   {
    initialValues.put(KEY_DAY, day[i]);
   }
 menuDb.insert(DATABASE_TABLE, null, initialValues);
 }

but that didn't work. Then I tried this:

if(rowId == 0)
        {
            menuDbHelper.createMenu("Monday", "", "", "");
            menuDbHelper.createMenu("Tuesday", "", "", "");
            menuDbHelper.createMenu("Wednesday", "", "", "");
            menuDbHelper.createMenu("Thursday", "", "", "");
            menuDbHelper.createMenu("Friday", "", "", "");
            menuDbHelper.createMenu("Saturday", "", "", "");
            menuDbHelper.createMenu("Sunday", "", "", "");
            //menuDbHelper.installDays();
            rowId = 1;
        }

That works but its no good, if you hit the back button and come back to that activity it just adds them all over again and then I have fourteen days. What am I doing wrong here? I am new to Java so take it easy on me.

A: 

You're inserting records into your database every time you call that ListActivity. Do you have to use a database in this case? If you're truly storing days of the week (assuming this isn't an example), then you can simply store those values in an array that is initialized whenever that ListActivity is created. The docs state that the internal ListView in ListActivity can take an array as well as a cursor:

ListActivity hosts a ListView object that can be bound to different data sources, typically either an array or a Cursor holding query results. ListActivity

McStretch
No, I would really like to use a database because as the user progresses through the app, I will need to add to the the "day" row that the user chose and then when they come back to this screen be able to display the day and the items entered in the other columns.
Will
Does your menuDbHelper object contain a SQLiteOpenHelper class declaration and instance? If not take a look at http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html and follow the NotePad sample application. If you do have an instance of it, and you want to insert those seven columns only once, then include the create methods (INSERT) in your SQLiteOpenHelper's onCreate override. Essentially you'd execute the SQL to create the table, and then run another statement to insert the initial values. Then on further calls the the DB, onCreate() will be ignored.
McStretch
Ah, brilliant. I can't try it right now because I am at work, but great idea. Thanks for that!
Will
No problem. If it doesn't work out, please keep posting. If it does, please mark correct!
McStretch
Alright, I tried what you said, or at least what I interpreted from what you said and inserted the values in the onCreate method of the Database helper class like this:
Will
@Override public void onCreate(SQLiteDatabase db) { db.execSQL(DATABASE_CREATE); ContentValues initialValues = new ContentValues(); initialValues.put(KEY_DAY, "Monday"); initialValues.put(KEY_DAY, "Tuesday"); initialValues.put(KEY_DAY, "Wednesday"); initialValues.put(KEY_DAY, "Thursday"); initialValues.put(KEY_DAY, "Friday"); initialValues.put(KEY_DAY, "Saturday"); initialValues.put(KEY_DAY, "Sunday"); }
Will
I think the ContentValue is meant to hold data for a single row, so you would have to create seven ContentValues for this case. You could also run a for loop over the days (if they're Enumerated types, in an array, etc.), and execute the same db insert method with the differing days as the values to insert.
McStretch
Why won't something like this work? I did some research on sqlite syntax and thought I could get it to work with a db.execSql in the overridden onCreate method. private static final String DATABASE_INSTALL = "insert into menu (day) value ('Monday');";
Will
You could definitely do that as well; either way you have to add insert seven times (either in the String or through ContentValues). There are multiple ways to accomplish this task, whatever you're comfortable with is fine. Objects like ContentValue help other developers see the values to insert at a quick glance -- it's not all contained in a large String. That being said, it's a pain to create seven ContentValue and then call db.insert() seven times -- that's probably less readable in fact.
McStretch
Alright, well my plan with the String didn't work for some reason. Did I mess up the syntax somewhere? I found a site that gave a quick tutorial on sqlite syntax, but it must be a little different in Android. I got all excited too, thought I had finally figured it out, shux.
Will
Change "value" to "values". http://www.sqlite.org/lang_insert.html
McStretch
Cool, I am going to try it when I get home. Thanks for taking the time to walk me through all this stuff, I really appreciate it.
Will
Ha, ha! It worked! I ran home for lunch and it worked! I can't thank you enough! Now onto the really fun stuff, the rest of the functionality.
Will
Awesome! Good luck with the app.
McStretch