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. However, what I need is to apply attribute data to the xml list and pass THAT instead; 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. OR just pass the data I want to display through 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. I feel as though I am right there, but am missing something.
By request, here is the XML I am inflating. I added the second array (content) so I may match the content to what was pressed in the 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? By “button” you mean list item, correct? It sounds like you are trying to store data in the XML item nodes as attributes, correct?
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.
(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))…
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.