简体   繁体   English

Android:将正确的数据从一个活动获取到另一个活动

[英]Android: Getting the right data from one Activity to Another Activity

Need some assistance, please. 需要一些帮助。 I am attempting to pass data from one activity to another, and I know, there are tons of examples of this, and I have achieved the basics of loading a ListActivity from the strings.xml and pulling the button .text from the button and passing it along to the next activity. 我正在尝试将数据从一个活动传递到另一个活动,并且我知道有很多这样的示例,并且我已经实现了从strings.xml加载ListActivity并从按钮中拉出.text并传递的基本知识进行下一个活动。 However, what I need is to apply attribute data to the xml list and pass THAT instead; 但是,我需要将属性数据应用于xml列表,并通过THAT代替; so on the receiving activity I can receive an id that correlates to what was clicked so I can pull the relevant data I want to display from the strings.xml. 因此在接收活动中,我可以收到一个与单击的内容相关的ID,以便可以从strings.xml中提取要显示的相关数据。 OR just pass the data I want to display through putExtra. 或者只是通过putExtra传递我要显示的数据。 See code below: 参见下面的代码:

-- Sending Activity -- -发送活动-

onListItemClick(ListView l, View v, int position, long id) {
 . . .
Object o = this.getListAdapter().getItem(position);
String keyword = o.toString();
 . . .
Intent myIntent = new Intent(v.getContext(), Activity1.class);
String s = keyword;
Intent i = new Intent(activity.this, activity.class);
myIntent.putExtra("com.activity.Key", s);
startActivityForResult(myIntent, 0);
 . . .

-- Receiving Activity -- -接待活动-

onCreate(Bundle savedInstanceState) {
 . . .
String data = null;
Bundle extras = getIntent().getExtras();
if (extras != null) {
    data = extras.getString("com.activity.Key");
} else {
    data = "Extras Was Null";
}
 . . .

As you can see I am getting the text data from the button, which doesn't really help me get to the content that correlates to that button. 如您所见,我正在从按钮中获取文本数据,但这并不能真正帮助我获得与该按钮相关的内容。 I feel like I need to pass the Node's ID instead, but cannot figure it out. 我觉得我需要传递节点的ID,但无法弄清楚。 I feel as though I am right there, but am missing something. 我感觉好像我就在那儿,但是缺少了一些东西。

By request, here is the XML I am inflating. 根据要求,这是我要夸大的XML。 I added the second array (content) so I may match the content to what was pressed in the ListActivity (root_menu); 我添加了第二个数组(内容),以便将内容与ListActivity(root_menu)中按下的内容进行匹配; it is a work in progress... 这是一项正在进行的工作...

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, activity!</string>
<string name="app_name">Activity Tutorial</string>
<string-array name="root_menu">
    <item id="o1">Item 1</item>
    <item id="o2">Item 2</item>
    <item id="o3">Item 3</item>
    <item id="o4">Item 4</item>
</string-array>
<string-array name="content">
    <item>Content for Item 1.</item>
    <item>Content for Item 2.</item>
    <item>Content for Item 3.</item>
    <item>Content for Item 4.</item>
</string-array>
</resources>

Could you post the corresponding XML? 您可以发布相应的XML吗? By "button" you mean list item, correct? “按钮”是指列表项,对吗? It sounds like you are trying to store data in the XML item nodes as attributes, correct? 听起来您正在尝试将数据作为属性存储在XML项目节点中,对吗?

Unfortunately I don't think that is supported by Android - the resource XML files are precompiled and cannot be modified from there, and I don't believe you can arbitrarily define new attributes to be retrieved either. 不幸的是,我认为Android不支持该功能-资源XML文件是预编译的,因此无法从那里进行修改,而且我也不相信您可以任意定义要检索的新属性。

(However, what you can do if you only need to read (and not write to) resource XML is define new types of nodes, and retrieve their values accordingly (probably including attribute values, but I've yet to try it))... (但是,如果您只需要读取(而不是写入)资源XML,您可以做的是定义新的节点类型,并相应地检索它们的值(可能包括属性值,但我尚未尝试过)。 ..

However, this may not help you if all you're trying to do is retrieve the value of a list selection, which is usually done by just maintaining an array of values that match the list. 但是,如果您想做的只是检索列表选择的值,这可能对您没有帮助,这通常是通过仅维护与列表匹配的值数组来完成的。

Let me know if I'm totally missing the point of your question. 让我知道我是否完全遗漏了您的问题。

Edit: Looks like you're just trying to find out which list item was clicked, as opposed to the content of that item. 编辑:看起来您只是在试图找出单击了哪个列表项,而不是该项目的内容。 Use the "position" parameter of onListItemClicked to get the index. 使用onListItemClicked的“ position”参数获取索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将数据从一项活动转移到另一项活动 - getting data from one activity to another 重新启动应用程序时从一个活动获取数据并存储在另一个活动中 - Getting data from one activity and storing in another activity at app relaunch 显示来自android中ArrayList的数据从一个活动到另一个活动 - Display data from ArrayList in android from one activity to another activity 如何将数据从一个活动传递到 Android 中的另一个活动 - How to pass data from one activity to another activity in Android Android:将数据从一个活动传递到ListView中的另一个活动 - Android: Passing data from one activity to another activity in ListView LiveData 不会将数据从一个活动更新到另一个活动 - Android - LiveData not updating data from one Activity to another Activity - Android 无法将数据从一个活动获取到另一个活动 Android - Unable to get data from one activity to another activity Android 在Android中从一个活动转移到另一个活动 - Moving from one activity to another Activity in Android 将数据从一个活动传递到另一个活动Android - passing data from an Activity to another Activity Android 如何将数据从活动传递到另一个活动,然后再传递到Android上的另一个活动? - How to pass data from an activity to another activity and then to another activity on Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM