For the Android app that I'm working on I have to do some lengthly sqlite queries, and it was making my code hard to follow. Is there any way I can put the queries into an XML file and then load them from there?
I'm attempting to do something similar to this.
You could create an XML file like the one you linked to under the res/xml/ directory, or use the res/raw/ directory and store it in some other format of your choosing. Then read the resource in as you normally would with Android, parse it if necessary, and interact with your database directly by using SQLiteDatabase.execSQL.
For specifics, it really depends on your needs and application architecture.
Related
I am developing an Android app that will display a list of PDF documents. The document list is a JSON file (which is currently used in a web application). My question is: does it make more sense to store the JSON file as an asset or store the JSON as stings in a variable in a class? The JSON is static and I only read from the file, there's no writing.
I don't think it really matters - an assets file is a little more complicated to read, but it's not a huge deal.
But it might be easier to work with - you can just replace the file, edit it in whatever external editor you want, maybe generate it automatically from some other tool. That's easier with a separate file than something that has to edit a string within a Java file or whatever, you know? Plus JSON can be awkward to work with without a nice editor checking it for you
And it makes testing easier - you can write it so you provide a different source file, have a different set of assets etc, which you can't do so easily if you hardcode the JSON.
And you'll probably get better build times if you're just modifying an external assets file - no changes to the actual code files means they don't need to be rebuilt.
Most of this stuff probably doesn't matter! Just laying out the stuff that could be a consideration - but if the string form is working out fine for you, why complicate things?
I'm writing an android weather app, which needs a city information list.
The list is quite long, and spends long time to be downloaded from internet.
So I want to save it in my .apk file, and load it without internet connection, but don't know how.
I tried to save it in a java class, but it's toooo huge and costs much memory.
Are there some ways to solve it? Thx!
did you look at res/assets?
I assume that you probably have some sort of json/xml data that you want to pre-package with your app if so, then load the json/xml into the res/assets and it'll just be a file included with your apk.
Ref
https://developer.android.com/guide/topics/resources/accessing-resources.html
You can store it in SQLite database or use plain text then save it in raw or asset resource in your app project.
For SQLite, try Android SQLiteAssetHelper which will help you with storing data from a predefined database.
Here an excerpt from its README:
An Android helper class to manage database creation and version
management using an application's raw asset files.
This class provides developers with a simple way to ship their Android
app with an existing SQLite database (which may be pre-populated with
data) and to manage its initial creation and any upgrades required
with subsequent version releases.
It is implemented as an extension to SQLiteOpenHelper, providing an
efficient way for ContentProvider implementations to defer opening and
upgrading the database until first use.
Rather than implementing the onCreate() and onUpgrade() methods to
execute a bunch of SQL statements, developers simply include
appropriately named file assets in their project's assets directory.
These will include the initial SQLite database file for creation and
optionally any SQL upgrade scripts.
you may use arrays from xml resources. For example
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="cities">
<item>New York</item>
<item>London</item>
<item>Moscow</item>
....
</string-array>
</resources>
I am developing an android app and in that I want to use a XML file. I think its a kinda web services and i want to use it in my app. so first of all i am thinking that whenever I open my app it fetch the whole XML file from http://data.gov.in/sites/default/files/Date-Wise-Prices-all-Commodity.xml and save it into assets folder of my App and then after I will use this XML from my assets folder.
I know this stuff is not too much difficult but actually i am new to android that's why getting this type of problem.
Please help.
Thanks in Advance.
You can't. The assets folder is read-only at runtime.
Pick a different location to save your data, see Data Storage for more information.
As posted in other answer you have to choose different location.
Rest method is as following.
Make a method or class to make http request.
Get response from given request.
Make a xml parser (DOM Parser,Pull Parser or SAX)
these are the basic steps. hope it will help.
Check this tutorial
Basically, I'm trying to store some data (~300 rows, ~10 columns) for an android app. This data will not be changed by the app, just used in calculations and stuff.
However, everything I've found online (example) talks about using a database that is created at runtime. I do not want this, the data will be the same every time the app is run.
So is there a way of using a databse like this? (Or any other way of storing data in a table-like fashion?)
Generate the SQLite database as part of your build and keep it in your app's raw resources. Since all you need is the file's path and name to open it, you can still read it fine. Your open helper will still go through onCreate() the first time unless you include the table Android uses for its own bookkeeping, but that should be okay.
Make sure you only open it for reading, and you should be good to go.
Put your custom file in the assets folder under the project root.
To get a inputstream from the file, just do:
context.getAssets().open(file);
In this way you can store your static data in conma separated or any model you want.
If you want the data constantly changing, you can create a temporary file in the SDCard, by accessing and creating a new file under some path at:
Environment.getExternalStorageDirectory()
How about storing this data in raw file under Assets or Res/raw folder. You can either dump this data on the fly in Database or read it and process it [which may be costly]. Dynamic handling may be costly, test it and compare performance.
Thanks for making android a wonderful platform.
What is the best option for saving a well formatted document(a document with images) on a device?
I know this can be done by saving to a XML file or on a database, but which of these two is the most preferable.
Thanks in advance.
-Ernest
Usually using XML or SQL database are differents approches for differents needings. Typically a database solution is more efficent than XML.
But XML has a tree structure that is easier to map in a object oriented language. I'll suggest you to use XML for the following reason:
easier to load data from xml to an object into your program
Easier to modify your page structure (with sql without a ORM system maybe difficult)
you are saving a "well formatted document" with images which has a naturally XML representation
You are on a mobile device and i don't think you need something like concurrently access to the same XML file or things like that
SQL would require installing a database, XML are just a standalone format