我如何preserve数据对象的Andr​​oid / Java的? [英] How do I preserve data objects in Android/Java?

查看:125
本文介绍了我如何preserve数据对象的Andr​​oid / Java的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个谁是工作在一个非常小的琐事游戏为Android平台的计算机爱好者。

I'm a computer hobbyist who is working on a very small trivia game for the Android platform.

在这个游戏中,我移动多个活动/视图页面(条目,问题网页,回答页,最终比分页)之间的球员。我创建了一个包含(问题,四个答案,指向正确的答案,相关评论,响应玩家的猜测时,计算机发出)与一个单一的琐事问题的所有数据问题的对象,我已经成功地填充一个ArrayList问题与我的DOM从XML文件解析的数据对象。

In this game, I move the player between multiple activity/view pages (entry, question page, answer page, ultimate score page). I've created a question object that contains all data related to a single trivia question (question, four answers, pointer to correct answer, related comments that the computer makes when responding to the player's guess), and I've successfully populated an ArrayList of Question objects with data that I DOM parsed from an XML file.

什么是随着比赛的进行传递这些数据来回活动之间的一种可接受的方式?我怎能当用户移动通过游戏这些数据对象还活着吗?

What is an acceptable way of passing this data back and forth between activities as the game progresses? How can I keep these data objects alive as the user moves through the game?

据我所知,全局变量非常气馁。我还了解到,Singleton设计模式带来了许多说要与全局变量相关的问题。我难倒当谈到寻找替代这些方法。

I understand that global variables are highly discouraged. I have also learned that the Singleton design pattern poses many of the problems said to be associated with global variables. I'm stumped when it comes to finding alternatives to those approaches.

据我所知,MVC方法的基础知识。我的活动* .java文件是控制器的文件,他们正在连接到布局(意见)我已经与XML创建的。我有需要维护和修改两个模式的对象随着比赛的进行:(1)上述(2)某种PlayerProgress对象中提到的问题,银行的ArrayList,其中包含有关球员的进步的所有数据游戏。

I understand the basics of the MVC approach. My activity *.java files are the controller files, and they're linked to the layouts (views) I've created with XML. I have two "model" objects that need to be maintained and modified as the game progresses: (1) the question-bank ArrayList mentioned above and (2) some sort of "PlayerProgress" object that contains all data related to the player's progress in the game.

这些数据对象被第一次实例在游戏的开始,但我不知道如何让他们活着,活动之间的用户移动。我知道我可以活动之间传递信息的临时演员,但EXTRA似乎并没有被用于这一目的。即使EXTRAS工作了玩家的进步性,我不认为我可以用它们来传递活动之间25-50问题对象的ArrayList。

These data objects are first instantiated at the beginning of the game, but I don't know how to keep them alive as the user moves between activities. I know that I can pass information between activities as EXTRAS, but EXTRA don't seem to be intended for this purpose. Even if EXTRAS worked for the player progress properties, I don't think I can use them to pass an ArrayList of 25-50 question objects between activities.

我已经调查系列化和parcelable,但它似乎不可思议(也可能是低效的),基本分解,然后重新我的数据模型对象的每个用户不同的活动/视图之间来回移动的时间。如果其中之一就是实现这一目标的可接受/常见的方式,我可以高兴地投身前进,但我想先与他人进行检查。

I have looked into serialization and parcelable, but it seems weird (and possibly inefficient) to basically decompose and then recompose my data model objects every time the user moves back and forth between different activities/views. If one of these is an acceptable/common way to accomplish this, I can happily plunge forward, but I wanted to check with others first.

我把我撞头这在不同的编程语言,并怀疑有就是我没有理解画面的一些更大的作用。我已经通过许多不同的资源对象和应用程序生命周期的描述看,但我还没有设法找出解决这个基本问题。

I keep butting my head against this in different programming languages, and suspect that there is some bigger part of the picture that I'm failing to understand. I've read through the descriptions of objects and application life cycles in many different resources, but I still haven't managed to figure out the solution to this basic question.

我问这个问题(什么是活动之间传递数据对象来回随着游戏的进行的一种可接受的方式)在我无聊的琐事游戏的背景,但我真的很关心的是得到一个手柄在大局观。如果人们没有拼出来的东西让看似基本的时间,或许你可以点我在您认为有用其他书籍的描述? (我有几个插槽我书架上的Safari打开,并能追踪几乎任何技术出版物的副本。)

I'm asking this question ("What is an acceptable way of passing data objects back and forth between activities as the game progresses") in the context of my silly trivia game, but I'm really concerned with getting a handle on the bigger picture. If folks don't have the time to spell out something so seemingly basic, perhaps you can point me to descriptions in other books that you found useful? (I've got a few slots open in my Safari bookshelf, and can track down copies of almost any technical publication.)

感谢。

推荐答案

我没有得到足够的票数直接在323go的有关静态变量后回答......但实施的正是辛格尔顿。我想指出这一点,因为你在你的问题,你的表示还了解到,Singleton设计模式带来很多问题说成是全局变量

I haven't got enough votes to answer directly in 323go's post about static variables... but that implementation is exactly Singleton. I wanted to point this out because you stated in your question that you also learned that the Singleton design pattern poses many of the problems said to be associated with global variables.

我承认额外的,如果你用来调用一个函数或它的参数内嵌的程序相当怪异的第一次,但似乎它在Android上的通常的方式。
我看不出有什么用额外的问题,因为它几乎是透明的你,如果你的模型对象都算不上复杂。我的意思是,你只需要实现可序列化界面,它的完成。但是也有一些不序列化的工作本身(如表),你需要实现Parcelable某些数据类型,但是你可以声明数组insted的。这似乎是Parcelable更有效的,所以,如果你想更好地执行,您需要反正实现Parcelable有人在这里说:
<一href=\"http://stackoverflow.com/questions/3323074/android-difference-between-parcelable-and-serializable\">Android: Parcelable和序列化之间的区别?

I admit extras are quite weird first time if you're used to call a function or a program with its parameters inline, but it seems it's the usual way in Android. I don't see what's the problem using extras, since it's almost transparent for you if your model objects are not really complex. I mean you just need to implement "Serializable" interface and it's done. There are some data types that are not working "by itself" with Serializable (as lists) and you'll need to implement Parcelable, but you can declare arrays insted. It seems Parcelable is "far more efficient", so if you wanna better performing you'll need to implement Parcelable anyway as someone said here: Android: Difference between Parcelable and Serializable?

我不认为共享preferences意图被用作传递参数的工具,因为它不会那么有效,因为它具有读写物理内存,使用了什么是硬工作中的任何计算机系统;而exras将通过内存堆栈发送(声明:我没有测试过,但它是什么逻辑告诉我)。我用dannyroa的方式(储存JSON对象)的时间持续的问题作为用户preferences,互联网检索几乎更新的数据,刻痕,游戏存档......

I don't think Shared Preferences are intended to be used as a "passing arguments tool" since it wouldn't be so efficient as it has to write and read to physical memory, what use to be "hard job" in any computer system; while exras would be sent through the memory stack (Disclaimer: I didn't tested, but it's what logic tells me). I use dannyroa's way (storing json objects) for time-persisting issues as user preferences, internet-retrieved hardly-updated-data, scorings, savegames....

和,最后...也许你可以调整你的code。你打开一个新的活动,每一个问题?也许你可以只使用一个活动,并使用片段。然后,你可以有你的全局变量在活动和内部的任何片段可以访问有 getActivity()

And, finally... maybe you can restructure your code. Are you opening a new activity for each question? Maybe you can use just one Activity and use fragments. Then, you can have your global variable in the activity and any fragment inside can access there with getActivity().

((MyActivity)getActivity()).myGlobalVariable();

这篇关于我如何preserve数据对象的Andr​​oid / Java的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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