I'm pretty new to android can some one give me a better idea which one is a better way to save 2 edit texts on activity 1 and 24 edit texts on activity 2.
i need to save it with date / time stamp
and
be able to open by linking with onClick of a button.
what is the best option: shared preferences, sql db or file?
Please help me out here.
Really it depends on what your application is going to do with that information and how you intend to use it.
Based on what you've briefly described, I'd recommend using an SQLite DB (with or without a Content Provider) to store the information just based on the fact that you're looking for a date/time stamp. Plus, it gives you a lot of flexibility going forward and it's not like there is a ton of overhead in that approach.
Shared Preferences are great too, but mainly are for key/value pairs and aren't nearly as flexible.
Last but not least, this question has some good info on using SQLite DB on Android and the differences in using a content provider vs. not.
Related
I am building an app as part of a project and I am stuck at the moment.
Background: My team is making a "Time-Wasting" App, and the user can press buttons such as "study" to track their time spent on activities - so there will be lots of integers values in the growing ArrayList.
Task:
Store a growing Arraylist(integers) in the app store and retrieve the values for calculation and graph display purposes.
Problem:
I have looked so far how do to this, but I am a bit stuck. Do I use SharedPreferences, the internal app storage or something else. Also, I have a hard time finding code that I could look at and follow what it is doing.
I need:
An efficient way to store the ArrayList (integers) in a storage place and retrieve it for my app. Any suggestions what would be best and where would I find the code for that?
I looked for about 2 weeks through videos and a through stack overflow but I am still stuck and not closer to how I should store this data type in my app.
Thanks, any help much appreciated :)
My opinion avoid the array list and use a database.
It will allow your app to grow including storing extra information along with the time waster including when they chose to do it, how long , etc.
I recommend Room Database it is an ORM for Sqlite it is super easy to use and there is plenty of documentation on implementation as it's apart of the Android Jet Pack.
If you are really stuck on using a list you can use SharedPreferences it doesn't handle ordered lists but you can store your list as a json string.
I want to write an android calculator app like the one on my android phone. It saves history for operations and by clicking a button it shows last operations. Now my question is what is the best way to save operations? Is it reasonable to save them to a file in internal storage or what?
There's some options..
1) Include a SQLite Database, as others mentioned. This makes storing lots of information really easy. You can find tutorials on how to include one properly in your project, and don't hvae to care for much more. You can then work with content providers to read and store data.
http://developer.android.com/guide/topics/providers/content-providers.html
2) SharedPreferences. If you just intend to store like the last, or the last 3 Operations, you can just use shared Preferences. This is way less overhead than adding a Database, if it is a small project, albeit you will have to keep your data structured yourself.
http://developer.android.com/reference/android/content/SharedPreferences.html
3) If you just want to store the users current session you can just Keep a Stack of the used operations. On undo, or however you call it, you would just pop the stack.
By implementing onSaveInstanceState and Parcelable you can make sure that no data is lost on rotation / low memory and such.
I personally would advise you without knowing more about your project to use plain java objects and storing the state. A calculater would in most cases not need persistent storage. If you really want to know what the user did 2 weeks ago, you should use a Database.
I would recommend you to use database(SQLite) for storing the data.
If you don't know more about SQLite in android have a look at these
tutorials.
I think database should be handy for history if more than one operations has to be stored else for one operation you can use shared preference.
I have a custom object, "TimeSheet", which itself contains Calendar, DateFormat, and int fields. The App is designed to use several of these objects, so I'm planning on storing them in a List as they're created and I'd like the App to be able to save these objects to internal storage when the App closes and reload them when it opens.
I'm still something of a novice when it comes to Android development (I've only published one App so far), so I'm not entirely sure of the best way to go about this. I'm guessing an ObjectInputStream and its Output counterpart are probably the best options, but I'm not entirely sure. I'm completely willing to change my design strategy to store a collection of these TimeSheet objects in the easiest way possible.
Can anyone recommend a good direction to go from here, and if possible, provide brief, simple examples?
Thanks!
There is no single right answer for something like this. A lot of it depends on the amount of data that you are storing. If you don't have much data, used SharedPreferences, if you have lots of data and it is complex, use a database. I wouldn't use a database if you don't have much data. You want to keep things as simple as possible and adding a database can complicate things. Here is a link that talks about the different options. Check it out. Hope it helps:
http://developer.android.com/guide/topics/data/data-storage.html
There are 2 ways to do this
Save it in a SQLite database..
Save the objects in a json format in a file
See this discussion
I'd honestly recommend using a SQLiteDatabase to store them: write functions to map your 3 fields to the database (Calendar would become a NUMERIC, DateFormat would be a String, and the int fields would all be NUMERICs) and to rebuild your object fields from a row in the database. Its a bit heavy up front but will make the inevitable feature expansion much easier.
I've been making an app on my free time for Android. I was looking your advice on to how store some data objects I have.
Today the objects are stored in the shared preferences, but as more and more objects are added with more properties I would hate to keep filling my shared preferences.
In short, today I want shared preferences but later I might choose to store in the SQLite database.
I've looked around for ORM based projects for Android and found a few. However, most of them are tied with a database and not the shared preferences. I am not even sure if some are customizable enough to add my own persister.
I could go on my own and perhaps write my own persister scheme. In fact, that's what I've done so far but it seems to basic and not so powerful.
Any ideas on what would you do?
First and foremost, if you are going to use a database (SQLite) then you should consider using the DB controller API Android already provides. http://developer.android.com/guide/topics/data/data-storage.html#db now you don't have to use that and can tie into the DB yourself. This is not recommended due to what you are experiencing right now... how to evolve and grow.
The controller doesn't care where you get the data from, it might be a DB or a web service etc.., so you can change the where you get the data from without changing how the data is passed about and handled.
Sorry for the breavity, I am not at my desktop currently. Bottom line is, SharedPreferences are powerful for sure but they are just a tool and should be used as intended... used for storing simple key/value data.
I would like to build some simple application - for example Todo list - and I am thinking about the problem and its solving - how can I to store data in my own application on Android platform?
I should to use some text file, xml file or some database? What will be better for beginner on this field?
You'll have a VERY hard time getting anywhere with Android if you don't read through their website/dev resources. I would highly recommend visiting their site.
As far as data storage is concerned, that varies based on your need. Explained here
My suggestion is to use SQLite that comes with Android. http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
In my opinion, a SQLite database seems most appropriate for this kind of application. There is lots of support for using the SQLite database in conjuction with ListViews (which I imagine you'd want to use in your to-do list app).
In case you haven't already checked it out, see the page on the developer site:
http://developer.android.com/guide/topics/data/data-storage.html#db
Theres a few ways you can store data. I've created a few applications thats store data using shared preferences
They're quite handy for storing strings, ints, bool values etc. However if you have a large scaled application, that requires better database management, I would look into sqlLite.
Android has supported classes and functions to help access/store the information.
Theres a good tutorial on the android site called notepad that takes you through how to use sqlite.
That should get you started :)
Look at this Thread klick
Edit: its cool for less data, if you want to store and browse lots of data, you should use as SQLite Database
Using a file, database or xml based depends entirely on what kind of application you are building. For eg: If you parse an XML feed and store the results back in an XML file - it totally defeats the storage purpose!
Databases are used to store structured and related content like - news feed results, email client data, etc.
Files are more used for storing raw / binary content like storing images, attachments, etc.
BTW, if you are a beginner - you should try all of them! :)
Hope this helps!