What is the best place for SQLite default entires? - android

I am creating (in android studio) an app which has a couple basic tables. One will be a bunch of exercises (pull ups, push ups ect) and I would like to put a ton of common ones into the table by default.
Where in my code would be the logical place to do that? I have made a databaseHelper class which extends SQLiteOpenHelper. Just not sure if I should..
insert them all in onCreate()
make a databaseHelper method which inserts them all and call it elsewhere
other?

Create a database with your desired data, package it as an asset, and use SQLiteAssetHelper to automatically unpack it into the proper spot for you when you first try to work with the database. This will be faster than running your own transaction(s) to insert the data.

Related

ContentProvider jUnit testing with dependencies to static method and other ContentProvider

I'm newbie writing unit tests for my Android app. Currently I'm testing ContentProviders (like here) and I recently finished one and all fine doing CRUD operations tests for a "satellite" SQLite table ("satellite"=no foreign keys). Now I have to test another ContentProvider that perform operations in other table that have a foreign key to "satellite" table AND filesystem (images) files.
My issues are (questions reference to the ContentProvider being tested):
How to test ContentProvider if I need data from "satellite" table and (images) files? I need to create (in setUp() method) whole scenario (satellite data and images files) before running tests? And how? Think about this scenario: select * from table join satellite where satellite.id = ? (query return a path to image column too). I mean, tables and file system have to have some data to ensure query return rows and test if file exists, right?
ContentProvider is designed as an endpoint to trade data for SQlite table and images files in this way: insert() method receive temporal image file path and user data associated to the image. This ContentProvider move temporal image file to it's own logic inside custom path; and insert user data and previous path to SQlite table. Next query() method return image path and user data as a row. While inserting, I have to call an utility static method that return custom path, for instance this method: Uri origPath( Context context, long idC, long idP, String fileName ) called using FileUtility.origPath(...) inside ContentProvider#insert() (Context param came from testing ContentProvider). When testing, I have a
java.lang.UnsupportedOperationException
at android.test.mock.MockContext.getExternalFilesDir(MockContext.java:186)
I know that external/system calls by default throw an exception like that, but how to test my ContentProvider if it do a call to a static method from utility class to work? (like this) I need to refactor my code to adapt it to test work? or I have to change my test code for something else?
Can I call a ContentProvider from other ContentProvider? or is a bad design?
Is more useful to create a FileProvider and deal with all user camera images with that provider?, By design it have to be called inside ContentProvider anyway (like 3.)
Feel free to explain how my design can change for sake of code lord, or if there is a pattern for that, or I missing something.

Activity is getting too large and becoming more and more difficult to work with. Solutions to solve this issue?

I am working on my second Android Application, first being, hello world. The application code is quite crazy looking because I love to test new libraries and ideas in it. I have been working on this application for well over 3 months and one of my activities is getting way to large and difficult to work with. I find myself getting lost in the code and it is taking longer to do simple things. There might be simple solutions to solving this issue. I really want to split my activity into two and reference each other if possible. Is there are any suggestions to simplifying and organizing code that would be greatly helpful. Even example will help me very much.
Part of my activity is adding a ton of data into a database and the other part is a long equation with multiple values. Another part is implementing the HoloGraphLibrary (Which I love). It is also implementing a listView with custom adapter. It also has a custom dialog............ I can go on and on. I hope you get my point.
EDIT
Going to work with this.
HoloGraphHelper holoGraph = new HoloGraphHelper();
holoGraph.initialize();
Try creating classes for each responsibility.
A Database Helper that has functions to insert data too:
DatabaseHelper database = new DatabaseHelper();
database .insertData(whatever);
A HoloGraphHelper that initializes the HoloGraph
HoloGraphHelper holoGraph = new HoloGraphHelper();
holoGraph.initialize();
And so on.
Break into multiple files. First classes defined in the Activity like the adapter. Change anonymous classes to classes defined in their own file. Look for ways to break out other related code into a class.
Right click on src folder of your Project and select new - class to create a new class. You can use a class for storing methods but you won't be able to display anything on screen.
To display contents to user, you can create a new Activity bu pressing Ctrl + N and selecting Android - Android Activity.
The best way is modularise your code.
I.e split your code into various related modules, for example a separate class for each part that your testing. So you could have a database entry class, a class for Gui testing, i.e. for your custom dialog. That class does all the work for that test, into various functions, I always try to keep functions as small as possible as they are easy to read.
As an example for your database entry, you could have a function which checks the database if the record already exists and then insert it. But a better way would be your insert function only performs the insert code and instead within this function it calls CheckIfDatAlreadyExists function which can return a bool so you know whether you should go ahead and insert the record. This would keep the code tidy and clean to manage.
Then from your main activity all would need to do is instantiate the relative class and call the relevant method.

Android: Create Database from SQL Script File WITHOUT Returning Data to Activity

I've done my homework; I know all the ways to query an SQLite db from an Activity.
The problem is that all the examples ASSUME that i want to "load" data from the db onto the FIRST Activity screen.
But when my app's FIRST Activity is loaded I DON'T WANT TO GET ANY DATA FROM THE DB; i just want to:
(1) Check if the db file has already been created (if the setup routines have already run).
(2) If the db exists, load the SECOND Activity (with ContentProvider/Loaders, etc.) so the user can start adding data.
OR
(2) If the db DOESN'T exist, WHILE STILL IN THE FIRST ACTIVITY run the setup routines (create the db/tables from an *.sql file & INSERT the dummy data where needed)...then load the SECOND Activity (with ContentProvider/ Loaders, etc.) so the user can start adding data.
To me, the simple operation of creating the db/tables shouldn't require all the OVERHEAD of a ContentProvider and a bunch of Cursors and Loaders.
Is there anybody who could point me to a SIMPLE solution? Thanks!
Yaqub's link was helpful...
...what i did was create public static final String arrays in a DBConstants class containing the commands to create the Database on first run.

Access DB outside an activity on android

I am trying to access my DB outside an activity and I get an error.
My architecture is having a wrapper for the DB, so when I want to display something in the activity I just create a class and take the information from it.
for example I want to create a HUMAN class that will access the DB through the db adapter class that I created. in the adapter class I return a cursor to the relevant row/s.
I tried doing so using startManagingCursor and I get an error saying it is undefined, when I try using this method in activity everything seems fine.
basically please try and give me an example of how to handle my DB in the right way.
I have a very big DB and I thought that creating a class for each table on the DB and than let the table get it's information is the right way, but I am not sure how to do that.
what I thought is lets create an instance of human with the id of 3 than in the constructor of Human (which is not an activity) go to the db get the information in a cursor and save all the information in the data members.
Thank you

Android AsyncTask - One SubClass Per Database Operation?

I have an activity for initialising a game, that does multiple selects and inserts from a number of SQLite tables.
I'm trying to understand AsyncTask, but, from all the examples I've read so far, I'm wondering if I am going to have to subclass AsyncTask for every single different data operation I need to do?
For example, my NewGame Activity does the following:
1) Insert new player record into PLAYER table
2) Insert new player's pet record into PET table
3) Select cursor of n records from INVENTORY
4) Insert array of ranomly chosen inventory items into PLAYER_OWNED table
5) ....more things of a similar nature
There are going to be a few more selects and inserts for various things too, so having an individual subclass for each one is going to get crazy. Not to mention that there will be about 8 activities for this game, all relying heavily on database reads and writes.
So, basically, how do I best use AsyncTask to carry out a number of different SQLite operations?
You can pass parameters to a AsyncTask, even more, if you use nested clases, you can use global variables from inside the AsyncTask class, by using one of the above or both mentioned aids you should be able to use the same class and have it do diferent things depending on the parameter you pass. I see no real need to define multiple AsyncTasks.
You will need to define a AsyncTask in every activity.
I wrote need, because you really dont have to, but its comfortable to do it this way, and its easy to read/write code, as the AsyncTask is asociated to the activity only. This is of course suposing you use nested clases, I see no point in writing a separate class file just for an AsyncTask.

Categories

Resources