0

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!

sczhg
  • 43
  • 6

3 Answers3

3

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

JoxTraex
  • 13,423
  • 6
  • 32
  • 45
  • Yes it's a JSON string. Just drag it in res/assets? And how to read it, maybe just like reading a file? – sczhg Oct 28 '16 at 08:29
  • Yep, just like that. Refer here https://developer.android.com/guide/topics/resources/accessing-resources.html also some more details http://stackoverflow.com/questions/9544737/read-file-from-assets – JoxTraex Oct 28 '16 at 08:30
  • Didn't forget, but the network between sf and China is too slow......even hard to refresh – sczhg Oct 28 '16 at 08:46
2

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.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
0

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>
Sergey Nikitin
  • 807
  • 8
  • 23