Reputation: 36008
I'm making a simple android app.
I've gotten the first point done by storing few items in list and presenting them in ListView
. I am wondering how can users add new items to the list and how would I persist?
I've seen several tutorials on internal storage and sql lite. Which one would be best for my needs in this scenario?
I would really benefit from a sample app on github that kind of shows how to accomplish this.
Upvotes: 2
Views: 1059
Reputation: 7976
You also know :
Internal Storage
Store private data on the device memory.
By default, files saved to the internal storage are private to your application and other applications cannot access them (nor can the user). When the user uninstalls your application, these files are removed.
People always use internal storage
when they want to read/write the large file via InputStream
/OutputStream
.
SQLite Databases
Store structured data in a private database.
Please notice structured data
, it will be good for you to access the Sqlite database via name
, so it will be fast.
P/s: If you need inflate data to the list item. You should use Sqlite
since it fast and easily to access the data via name.
Upvotes: 1
Reputation: 19484
It really depends on how you want to use the data. SQLite is easy to use on Android and runs fast. I'm using it for a fairly large database in my app.
I also suggest putting it in internal storage. That way, it is protected from other apps spying on the user's data (unless he "roots" his phone). One caveat on using internal storage is that it's hard to see your data. To get to the db you either need to run Android on an emulator (where you will have the privilege to see all files) or copy the db (by writing some code to copy a file) to external storage. Or simply temporarily put the db in external storage while you are debugging.
I don't have any github references for you, but can give you a quick outline. Subclass SQLiteOpenHelper and add methods for your CRUD actions.
public class DbHelper extends SQLiteOpenHelper
{
private static final int DB_VERSION = 1;
@Override
public void onCreate(SQLiteDatabase db)
{
String[] ddl = {"... all the tables you need to create..."};
db.beginTransaction();
try
{
for (int i = 0, limit = ddl.length; i < limit; i++)
db.execSQL (ddl [i]);
db.setTransactionSuccessful();
}
finally
{
db.endTransaction();
}
}
@Override
public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion)
{
db.beginTransaction();
try
{
for ( ; oldVersion < newVersion; oldVersion++)
{
if (oldVersion == 1)
onUpgrade1to2 (db);
else if (oldVersion == 2)
...
}
db.setTransactionSuccessful();
}
finally
{
db.endTransaction();
}
}
public void insertItem (SQLiteDatabase db, Item item) // Item is one of your objects
{
ContentValues values = new ContentValues();
// id
values.put ("id", item.id);
values.put ("subject", item.subject);
...
db.insertOrThrow ("Items", null, values);
}
... and so on ...
}
Let me know if you need info on how to query, update, and delete...
Upvotes: 0