初学Android - 在不同活动中使用变量或文件的方法? [英] Beginner Android - method to use variables or files across different activities?

查看:91
本文介绍了初学Android - 在不同活动中使用变量或文件的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



基本上它是一个简单的应用程序,它是一个简单的应用程序,并试图提出最好的(最简单的)方法。列车管理应用。其中一部分将跟踪列车上的汽车以及每辆单车的敞开/占用座位的数量。我认为一种方法是只有两个数组。一个整数数组,用于存放旅行车中的座位数量。这样,我可以在购买机票时弹出最后一个元素,并且始终具有正确的编号。另一个数组只会包含代表各个货车的int数组。
我考虑的另一种方法是存储并从文本文件中提取信息。我会认为第一种方法会容易一些。



所以有两个问题 - 1)我还缺少另一种方法吗?
2)(真正的问题)我如何在不同的Android活动之间操作/访问数组。一个活动列出了货车,另一个列出了座位,另一个列出了座位,另一个允许购买票据,要求我从阵列中移除一个元素。



对不起长度和感谢您的帮助。

解决方案

在活动之间共享数据的方法很少:



以上是来自android.com的指南,请查看,并选择适合您的程序。


I am writing a simple application and trying to come up with the best (most simple) approach to what I am trying to do.

Basically it is a simple train management application. One part of this will be keeping track of the cars on the train and also the number of open/occupied seats on each individual wagon.

I thought one approach would be to just have a two arrays. One integer array that holds the number of seats in a wagon. This way I can pop off the last element when a ticket is purchased and always have the correct number. The other array would just hold these int arrays representing the individual wagons. The other approach I considered would be to store and pull the info from a text file. I would think that the first approach would be easier.

So two questions - 1) Is there another approach that I am missing? 2) (the real question) How can I manipulate/access the arrays between different Android activities. One activity lists the wagons, while another lists the seats, and yet another allows a ticket to be purchased, requiring me to remove an element from the array.

Sorry for the length and thank you for any help.

解决方案

There are few ways for sharing data between activities:

  • SharedPreferences, where you store your data in key/value pairs (check here)

  • use internal storage and save your data in the file; you'd have to write methods to write/load your arrays to/from the file

  • use SQLite database - check here

The above are from a guide on android.com, check this out and choose what fits your program.

这篇关于初学Android - 在不同活动中使用变量或文件的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆