简体   繁体   中英

How do you store app data locally using android studio?

I'm just getting started in android studio and app development overall. I've done a few tutorials to learn the interface and basics for Android Studio (activities, textviews, buttons, etc) and I've taken a college course in Object Oriented Programming in Java. Now, here's what I'm trying to do:

I'm making a very basic app that's meant to help the user budget their money. The app "acts" as your account and you add the amount of money you've recently been paid. From here, you can allocate your money to savings, checking, and so on. The data I need to store would be the numbers representing dollar values, the different allocations the users made, and the respective dollar amounts to those allocations.

I've managed to contain the data relatively fine within the Java data structures (I developed some algorithms in eclipse to get started). Do I need to serialize this data? Or try writing it to a text file? I've looked around for some tutorials and most low level ones are confusing or require advanced techniques with SQL. I'm interested in simply locally storing just as you would with a to-do list app that keeps record of tasks you entered.

Any and all insight is appreciated.

If I were you I would go the sql route. It's really not hard to get started with sqlite for android. If you haven't already seen Google's own tutorial on the subject, check it out here .

That said though, you can also look at the other methods mentioned here if you feel a database is overkill. Realistically the other choices would be internal storage or external storage if it's something like a todo list. Shared preferences are most often used for persisting user settings.

If you choose to go the file route and don't want to save the data as something like a csv then you need to make your java objects implement the Serializable interface and write them to the file with an ObjectOutputStream .

For example:

FileOutputStream fos = new FileOutputStream("/some/file/path/filename.ser", true);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(yourSerializableObject);

First important thing is that Activities, TextViews, Buttons and other view components are not part of Android Studio. They are part of android framework. Android Studio is IDE as Eclipse, NetBeans and etc.

Regarding your requirement, there are more ways of persisting data in android. You can use files, SharePreferences and sql (sqlite).

Please take a look at following document:

https://developer.android.com/guide/topics/data/

Also i would suggest you to follow Google android official documentation. https://developer.android.com/guide/

Best Regards, Milan

There are several ways to store your data locally on the device:

  • SharedPreferences - usually they are used to store app settings (Such as first name, last name, etc.).
  • Databases - used to store structured data, best solution when app needs to store large amount of data. Also, thay has powerful and flexible mechanisms of read/write operations.
  • Files - Not the best way to save structured data, no build-in mechanisms. Also app needs additional permissions to write/read operations (if you decides to save data on external storage), which can be denied by user(if your app is targeting api 23 and greater you should use runtime permissions )

The most effective way to store app data in your case is databases.

Google has developedRoom Persistence library , which provides abstraction level over SQlite and makes all things simple and easy to understand.

Here is a nice example on Medium, which provides easy to understand tutorial for Room Persistence library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM