将数据从一个选项卡活动拉取到另一选项卡活动
我读到的有关意图的所有内容都谈到使用它们来推送数据,或从另一个活动启动一个活动。我想从已经运行的活动中提取数据。
选项卡布局教程位于 http://developer.android.com/resources/ Tutorials/views/hello-tabwidget.html 说明了我想要做的事情。 (我的应用程序正在执行一些工程计算,但教程代码为我的应用程序提供了一个很好的类比。)本教程创建一个具有三个选项卡的应用程序,每个选项卡托管一个单独的活动。
为了扩展教程中的示例,假设我在“艺术家”选项卡/活动中选择了一位艺术家。我希望能够选择“专辑”选项卡/活动并让它显示该艺术家的所有专辑。
在我看来,我需要使用 Intent 来做到这一点。我发现的所有教程都假设我将在“艺术家”选项卡/活动中创建一个“查看专辑”按钮,并且按下该按钮将执行启动“专辑”活动并传递艺术家名称的 Intent。
我不想创建该按钮。艺术家布局上的空间非常宝贵,而且我有一个非常好的专辑选项卡,并且 HelloTabWidget 活动已经包含创建专辑选项卡的意图。
此外,用户可能希望在专辑和艺术家之间来回跳转以更改艺术家选择,而选项卡是实现此目的的完美方法。无需使用其他按钮使 UI 变得复杂。
那么,当选择“专辑”选项卡(或显示专辑布局)时,如何让“专辑”活动从“艺术家”活动中拉出“艺术家名称”,而不是让“艺术家”活动“启动专辑”并推送“艺术家名称”?
我可以从其他编程世界中想到等效项:
全局变量。对 Android 开发感到沮丧,对吗?如果它们确实存在,它们叫什么?
吸气剂,如
artistName = Artists.getArtistName();
。我感觉这并不容易。写入和读取文件 - 即大容量存储器或非易失性存储器。我不需要 ArtistName 值是永久的。每次用户启动应用程序时,它都会重置为 null。
那么在Android世界中它是如何实现的呢?我是否使用意图 - 如果是,如何使用?
Everything I've read about Intents talks about using them to push data, or to start one Activity from another Activity. I want to pull data from an Activity that's already running.
The Tab Layout tutorial at http://developer.android.com/resources/tutorials/views/hello-tabwidget.html illustrates what I want to do. (My app is doing some engineering calculations instead, but the tutorial code provides a good analogy to my app.) The tutorial creates an app with three tabs, and each tab hosts a separate activity.
To expand on the example in the tutorial, suppose I select an artist in the Artists tab/activity. I want to be able to select the Albums tab/activity and have it display all the albums featuring that artist.
It seems to me that I need to use an Intent to do this. All of the tutorials I've found assume that I would create a "See albums" Button in the Artists tab/activity, and that pressing the Button would execute an Intent that starts the Albums activity and passes artistName.
I DO NOT want to create that Button. Real estate on the Artists layout is precious, and I have a perfectly good Albums tab, AND the HelloTabWidget activity already contains an intent to create the Albums tab.
Besides, a user will want to skip back and forth between Album and Artist in order to change artist selections, and the tabs are a perfectly good way to do this. There's no need to complicate the UI with another button.
So how can I have the Albums activity PULL artistName from the Artists activity when the Albums tab is selected (or the Albums layout is displayed), rather than have the Artists activity START Albums and PUSH the artistName?
Equivalents I can think of from other programming worlds:
Global variables. Discouraged in Android devt, right? And if they do exist, what are they called?
A getter, like
artistName = Artists.getArtistName();
. I get the feeling that it's not that easy.Writing to, and reading from, a file - that is, mass storage or non-volatile memory. I don't need the artistName value to be permanent. It will be reset to null every time the user launches the application.
So how is it done in the Android world? Do I use an Intent - and if so, how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
全局变量是正确的答案。
我以为 Java 不鼓励使用它们,但是出现在该窗口右侧的“相关”链接中的几个链接直接提到了它们。一个是“Android:如何声明全局变量?”,另一个是"如何在两个选项卡之间传递值安卓”。两者都指向应用程序类作为定义全局变量和方法的地方。有了这些新知识,我找到了一篇名为 " 的文章Android 应用程序类”,位于 Xoriant 博客上,该博客扩展了 StackOverflow 的答案。
最好先查看这三个链接。我需要对这些作者所说的内容添加一些提示。
您的应用程序类必须位于其自己的单独文件中。 (对于某些人来说,这可能是一个“废话”,但不是每个人。)下面是一个名为 Something.java 的示例的良好框架:
我在 Windows XP 中使用带有 ADT 插件的 Eclipse。如果直接编辑 XML 代码,Eclipse 并不总是能够正常运行,因此最好打开 AndroidManifest.xml,然后选择“应用程序”选项卡并在“名称”字段中输入应用程序名称。您不需要在名称前面添加点或句点。只需输入您的类的名称,例如“Globals”或“MyApplication”或其他名称。 (请注意,这是清单中的默认应用程序。您不必创建单独的

标记。在实际的 Android 设备上可能不需要此步骤,但对于模拟器:您需要在每个
onCreate()
以及每个将访问全局变量和方法的方法中使用getApplicationContext()
命令。我尝试将其与其余活动范围变量一起放在onCreate()
之外,但它不起作用。将其放入每个方法中似乎很浪费,但模拟器和 Android 设备都可以通过这种方式正常工作。这是一个展示我如何使用它的示例:这是我遇到的唯一问题。我列出的三个参考资料放在一起,在其他方面都相当完整。
Global variables were the right answer.
I thought Java discouraged their use, but a couple of links that appeared in the "Related" links on the right margin of this window mentioned them directly. One was "Android: How to declare global variables?" and the other was "how to pass value betweeen two tab in android". Both pointed to the Application Class as the place to define global variables and methods. Armed with this new knowledge, I found an article called "Android Application Class" on the Xoriant blog that expanded on the StackOverflow answers.
It's best to review those three links first. I need to add some tips to what those authors have said.
Your Application class has to be in its own separate file. (That might be a "duh" to some people, but not to everybody.) Here's a good framework for an example called Something.java:
I'm using Eclipse with the ADT plug-in, in Windows XP. Eclipse doesn't always behave properly if you edit XML code directly, so it's best to open AndroidManifest.xml, then select the Application tab and enter your application name in the Name field. You don't need to put a dot or period in front of the name. Just type in the name of your class, like "Globals" or "MyApplication" or whatever. (Note that this is the default application in your Manifest. You don't have to create a separate

<application></application>
tag.This step may not be necessary on an actual Android device, but it was necessary for the emulator: you need to use the
getApplicationContext()
command in everyonCreate()
and every method that will be accessing the global variables and methods. I tried to put it outside ofonCreate()
with the rest of my activity wide variables, and it didn't work. Putting it inside every method seems wasteful, but both the emulator and the Android device work fine with it that way. Here's a sample showing how I used it:Those were the only hiccups I encountered. The three references that I have listed, taken together, are otherwise pretty complete.