Chironex
Chironex

Reputation: 819

How to store small amounts of data for frequent lookup in android?

I have the following data tuple describing the user of my application:

(userID, name, email, contact number) 

and I want to access this data frequently throughout my application. I have a contacts table in my database in which tuples of this type are stored to describe other contacts. The reason I don't want to store the tuple describing the user is because it seems wasteful to have an attribute to mark which data is "self", and also I use the table directly for inviting users, so I don't want users to see themselves on the list of contacts! I thought about using shared preferences to store "self", but I may decide to include more data in the future, and piecing together a tuple from several key:value lookups seems like a very messy solution. What's the best way to store these "shards" of data so they're easily accessible and not wasteful?

Thank you :)

Upvotes: 2

Views: 1434

Answers (3)

Here are your choices for storing data:

Android Data Storage

I would try using the external file if you don't want to use shared preferences or a database. XML and JSON files are both good choices that can be easily parsed. If you try a JSON file you could access it using GSON in your code.

Upvotes: 1

UltimateBlob
UltimateBlob

Reputation: 84

Another option is to create a subclass of Application which has fields (and get/set methods) for your variables. Use this class in your <application> tag in the manifest. Then use e.g. ((MyApplication)getApplicationContext()).getUserId()

If the fields are static you might find they persist across application instances. I haven't tried this though.

Upvotes: -1

Related Questions