Entering and retrieving data from SQLite for an android List View
- by Infiniti Fizz
Hi all,
I started learning android development a few weeks ago and have gone through the developer.android.com tutorials etc. But now I have a problem.
I'm trying to create an app which tracks the usage of each installed app. Therefore I'm pulling the names of all installed apps using the PackageManager and then trying to put them into an SQLite database table. I am using the Notepad Tutorial SQLite implementation but I'm running into some problems that I have tried for days to solve.
I have 2 classes, the DBHelper class and the actual ListActivity class. For some reason the app force closes when I try and run my fillDatabase() function which gets all the app names from the PackageManager and tries to put them into the database:
private void fillDatabase()
{
PackageManager manager = this.getPackageManager();
List<ApplicationInfo> appList = manager.getInstalledApplications(0);
for(int i = 0; i < appList.size(); i++)
{
mDbHelper.addApp(manager.getApplicationLabel(appList.get(i)).toString(), 0);
}
}
addApp() is a function defined in my AppsDbHelper class and looks as follows:
public long createApp(String name, int usage)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_NAME, name);
initialValues.put(KEY_USAGE, usage);
return mDb.insert(DATABASE_TABLE, null, initialValues);
}
The database create is defined as follows:
private static final String DATABASE_CREATE =
"create table notes (_id integer primary key autoincrement, "
+ "title text not null, usage integer not null);";
I have commented out all statements that follow fillDatabase(); in the onCreate() method of the ListActivity and so know that it is definetely the problem but I don't know why.
I am taking the appName and putting it into the KEY_NAME field of the row and putting 0 into the KEY_USAGE field of the row (because initially, my app will default the usage of each app to 0 (not used yet)).
If my addApp() function doesn't take the usage and just puts KEY_NAME into the ContentValues and into the database, it seems to work fine, but I want a column for usage.
Any ideas why it is not working? Have I overlooked something?
Thanks for your time,
InfinitiFizz