I have a Tabel with Buttons, 9 rows and 5 columns -> 45 Buttons. Each Button sould have the properties:
row, week, time, day, name, description, colour, room.
What is the best way for the storage? Do I have to use the complicated SQLite, or is there a easier solution? I'm new in Android, therefore SQLite is not easy for me.
Personally I would take some time to implement a ContentProvider and use a SQLite db...
However, you can use a lot of different approaches, you can save all your information in a private file (so you can format them as you wish) or use the SharedPreferences mechanism.
You should have a look here to read about the different methods
Assuming that it is static set of 45 buttons and does not require permanent persistence. There are two days you can solve it
Extend Button class and define the properties you need
Use setTag to store carry in-memory data objects along with view objects. So in your case create a Bundle object for each button and store it in the button view with setTag.
Related
I need to work with a persistent String Array (n Rows, 1 column).
* On first running the app, the String Array needs to be created empty.
* On subsequent app executions the Array will be populated from a File and the contents need to be available throughout the rest of the app.
* As the app is executed, the Array needs to be able to 'grow' in row count
* As the app is executed, the Array rows need to be able to grow in length
* The user will have the option to Clear the Array of previous entries.
* At the end, the String Array contents will be written back to a File.
I find a lot of references to Putting and Getting from an existing SharedPreferences String[] but, in the newness of my Android development, I am struggling with how to proceed.
EDIT Follows...
The data itself suggests using an Array
Example:
MAIN ST. F55 63 KY08:57 12142015--------KY11:24 12142015345TMH KY13:57 12142015
MAIN ST. F56 WYE123 IN08:57 12142015--------KY11:24 12142015--------KY13:57 12142015
1ST ST. F57 --------KY08:57 12142015--------KY11:24 12142015789FPF KY13:57 12142015
1ST ST. F58 456FPF KY08:57 12142015998FPF KY11:24 12142015--------KY13:57 12142015
1ST ST. F59 789TTM KY08:57 12142015--------KY11:24 121420151234DG KY13:57 12142015
I first need to have this data in a File
Then in one GUI I check for the existence of the file.
If one exists, fine
If none exists, I create one.
Then, in subsequent GUI's, I must check for the existence of parameters
If they do not already exist, add them to the existing data lines.
If they already exist, notify the user
And so on and on.
Then when all of the current 'pass' data has been collected via multiple, separate GUI's, I have to write out the whole data-set into the file.
My reason for thinking that I need a SharedPreference approach is the need to find and check data from GUI to GUI as the user progresses through the app.
If that 'belief' is wrong, I am open to better approach suggestions.
EDIT 2 follows....
On further study of web references, I am beginning to think that perhaps the best approach for this data and how the data needs to change might be to use a SQLite approach. Any ideas about this?
Any assistance/suggestions you might have would be greatly appreciated.
i would discourage you from using sharedpreferences for anything else than preferences. means things that change rarely - really rarely and are really lightweight. do not put much data in there. less is better. the data structures underlying sharedpreferences are not a database.
another note. it is not a string list, but it would be a string set. sets are not necessarily ordered, nor do they necessarily keep their order. means - it is not rows. its a collection of strings that can come back in any fun order (usually there is some, but that depends on the implementation which i do not know)
now you could go and make your own list, your own data structure, save it into a string and read it out, use json to do exactly that or something similar, or better - use a database, which would exactly do that.
http://developer.android.com/training/basics/data-storage/databases.html
explains it, but as you'll see its something that might take some time.
now dont get me wrong, but i have to warn you about the following approach. it is valid, but has many problems and is far from thread safe. it will not be a problem as long as you only open it from the ui thread and do not keep anything in memory to cache - if you do it will create lots of problems.
your problem of adding a line and clearing can be solved by using a file. just a simple file
look here
http://developer.android.com/training/basics/data-storage/files.html#WriteInternalStorage
the change is to append when writing:
openFileOutput("filename", Context.MODE_APPEND);
see the context mode? now you can basically just write one line and append every time.
if you wanna clear the file, just deleteFile("filename")
this is as said not threadsafe, but can be a viable option if used carefully.
Please follow this step to achieve what you want with sharedPreference
create the class Parent for SharePreference
Create your empty Array
Convert Your empty array to String and put it on SharedPreference
to call your empty array from sharedPreference
Call your sharedPreference using your key
Convert the String to array
You get your array from the sharePreference
Hope it helps, and maybe this link will help you :
http://www.androidhive.info/2012/08/android-session-management-using-shared-preferences/
You can use my open-source library called prefser, which solves this problem and uses SharedPreferences and Gson under the hood. It's basically wrapper for default Android mechanism.
You can use it in the following way:
Prefser prefser = new Prefser(context); // create Prefser object
prefser.put("key", Arrays.asList("one", "two", "three")); // save array of Strings
String[] value = prefser.get("key", String[].class, new String[]{}); // read array of Strings
For more information check repository of the project, tests and README.md file.
Link: https://github.com/pwittchen/prefser
Please note, SharedPreferences have some limitations and shouldn't be used for storing large amount of data. If you expect a lot of data, consider using SQLite database or another type of database (e.g. with NoSQL or similar approach if you strive for simplicity).
OK, based on the data, how it needs to be manipulated and the pros and cons of using a SharedPreferences approach, I have decided to go with a SQLite approach.
With that approach I should be able to readily check:
* if the necessary table exists (if not create it)
* if the necessary Field1 + Field2 exists (if not create a new record)
* and I will be able to modify the record's Field3 contents as needed
Then when the user's actions are complete I can convert the SQLite table 'records' into strings and write them out as a File and then either DROP or PURGE the associated SQLite table (until needed next time).
I sincerely appreciate all of your suggestions.
Thank you.
I have some scens with and I want save their state of view when app is closed. Some times it's one thing like "is that scene has opened shop window?" but sometimes ther is much more info which I need to store. So is better to use for it UserDefaults/SharedPreferences (json to string) or create model, serialize those info to model then save it to DB?
I will be thankful for Your opinion.
I use SharedPreferences when i have to save a variable, such as UserName, Country of Origin, access token, location, UsersCurrentLevel, UsersCurrentHealth etc.
I use Database when i have data that requires much manipulation or has more specifications to it. Such as questions for users,
video ad's details(seenState , directoryPath, urlToDownloadVideo, hasVideoBeenAlreadyDownloaded etc.), lists of data, etc.
So i would suggest you use a mix depending on the data.if it has a single entry use SharedPreference else if it has a list/ multiple data for a single format put it in a DB. Hope this helps.
My app is using api 7. I don't know where to start with this challenge.
I have SQLite DB with some numbers stored between 1-99. Now I would like to make number picker for this range which would also remove numbers that are already in DB.
Create a list off the numbers 0-99, then do a query on your database. For every row in the results from the database, check if the list contains is (something like list.contains(number). If it is there, remove it (list.remove(item)) then proceed to the next row
You'll probably have to make your own widget. This will be somewhat involved, and since you are an Android beginner you might better spend your time coming up with a different input method.
In case you do decide to write your own widget, I would recommend extending LinearLayout, then inside of the constructor, doing something like this psuedocode:
setOrientation(VERTICAL);
addView ImageButton arrowUpButton;
addView EditText numberEditText;
addView ImageButton arrowDownButton;
arrowUpButton.setOnClickListener {
int num = myListOfInts.get(currentIndex++);
numberEditText.setText(num);
}
//vice versa for arrowDownButton
you'd also have to create a setter for the myListOfInts.
Good luck!
The thing is I need to store something like:
100 strings
20-30 ints
As you can see, it's a pretty large number, and you might say "Use sql"! I would use sql but for me it looks kinda alien. It's a lot of code, and after that I can't find a tutorial that shows you: look, you make a database with tables like this, and then you read it like this, you can also search in it like this...
Can you please give me the best method to save that data? And if you say sql, please give me a tutorial or something that shows me how to do what I said above.
I would say you should look into SQLite and try to learn that. But if you really want to stay away from SQL you could use putStringSet to store your strings in SharedPreferences. Now there isn't a thing for putIntSet but you could convert your ints to strings using String.valueOf(myInt) and save it using the same putStringSet. I would like to be clear though, I think that a database is the way to go on this but this method should work for you.
If you use SQl, look at ORMLite, which will let you store objects in the db (http://ormlite.com/). For 100 strings, if they're basically name/value pairs, just use SharedPreferences http://developer.android.com/reference/android/content/SharedPreferences.html. Could also use static string resources, as mentioned.
Is it possible to use AdapterView with text fields in android?
My query returns a set of values and for each I want to place that within a textfield, so that the user may edit the value.
Also, I want to click a button to create a new empty field, so that I may insert a new entry.
If you know of good example, then please let me know!
EDIT 1
I would prefer to use XML to define ui and I found this informations:
"In this case we create a new id called text1. The + after the # in the id string indicates that the id should be automatically created as a resource if it does not already exist, so we are defining text1 on the fly and then using it." Source http://developer.android.com/resources/tutorials/notepad/notepad-ex1.html
Will this + allow me to autocreate as many fields as needed? Is there a way I can generically specify the layout in XML and then create fields adhoc based on db response?
Many thanks,
Mith
Is it possible to use AdapterView with
text fields in android?
Yes. It will be complex for you (and possibly also for the user), so I would not recommend it unless you have a few months' Android programming experience, but it should work.
Also, I want to click a button to
create a new empty field, so that I
may insert a new entry.
That will get a little complicated.
Will this + allow me to autocreate as
many fields as needed?
No, that is not how you use ListAdapters for ListView rows.
Is there a way I can generically
specify the layout in XML and then
create fields adhoc based on db
response?
Use a CursorAdapter.
Here is a free excerpt from one of my books that describes creating custom adapter classes. Here is a sample project that shows creating a custom CursorAdapter to display the results of a database query.