Skip to content Skip to sidebar Skip to footer

View Sqlite Databases In Android Listview?

I am sure there is an easy solution to this one but I figured I would check with all the folks here first. I am working on a database creation and management application for androi

Solution 1:

Here's a good tutorial on SQLite databases and displaying contents in a ListView: http://www.vogella.com/articles/AndroidSQLite/article.html#databasetutorial

It doesn't go over editing that much, but it's easy to see where he puts the values into the database.

thenewboston on YouTube is a good resource for Android tutorials and he goes over SQLite databases: http://www.youtube.com/watch?v=gEg9OdufXmM

It's pretty comprehensive and slow if you already kinda know what you're doing so here is where he goes over inserting data/editing the database if you just wanna jump to that: http://www.youtube.com/watch?v=S3Z4e7KgNdU


Solution 2:

I know this below can be optimized but for now just create such a method to do it automatically. The method...

  1. Creates an empty database of a random name for a second,
  2. Saves the new database's location - getDatabasePath,
  3. Quikly deletes the empty database,
  4. Removes filename form the saved path to get the directory path olny,
  5. List all files in the database path excluding '-journal' files.

And it goes like this:

ArrayList<String> arr_list_of_db_files = getDBFILES();

pivate ArrayList<String> getDBFILES() 
  {
   ArrayList<String> arr = new ArrayList<String>;
   String db_path, rand_name, str_tmp;

   //ad.1-2. random file name for db   
   rand_name = new Random().nextInt((4000000-2000+1)+2000).toString()+".db";
   db_path = openOrCreateDatabase(rand_name, MODE_PRIVATE, null).getPath();

   //ad.3.
   deleteDatabase(rand_name);

   //ad.4.
   db_path = db_path.replace("/" + rand_name, "");

   //ad.5.
   File [] files = new File(db_path).listFiles();
   if (files == null) { return null; }

   //so now we get the filenames one by one
   for (int i = 0; i < files.length; i++)
       { 
          str_tmp = files[i].getName();
          if (!str_tmp.endsWith("-journal"))
             {  arr.add(str_tmp); }
       }
   return arr;
  }

Btw, I cant test the code, but I hope it is fine and some of you find it useful.

Edited: I have optimized the above code.


Post a Comment for "View Sqlite Databases In Android Listview?"