view SQLite Databases in android listview?

…衆ロ難τιáo~ 提交于 2019-12-11 02:37:41

问题


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 android where the user creates and manages data along the line of what PHPMyAdmin does for regular computers.

I have the section where the user creates a database and can insert tables with the appropriate styled data into the system.

The next priority is selecting which DB to enter and modify its contents. Is there a way to display the available databases, along with its table contents, in the form of a list-view for the user to enter and edit the desired data??

I know that this is a rather dull question, but this is basically the last piece of the puzzle for me to fit into this app before it is operational in a raw format. If you need any further information, or any code to examine, I will be happy to provide.

Thanks again for everyone's assistance.


回答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




回答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.



来源:https://stackoverflow.com/questions/16138194/view-sqlite-databases-in-android-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!