1

Apologies for the terrible title, but couldn't think of a better one. So I have numerous string-array's in my strings.xml file. I want to be able to reference this by passing my intent the resource id (e.g. R.array.mystringarray) as a string, and then using this string in my class to get my String[]. Here's what I have attempted so far to give you a better idea of what I'm trying

Intent intent = new Intent(Main.this, Categories.class);
intent.putExtra("category", "R.array.Categories");
startActivity(intent);

Then in Categoris.class:

myArrayID = (String) bundle.get("category");
myArray = getResources().getStringArray(myArrayID)));

Is my method even possible? Or is there another way I've overlooked. First app and still learning the ropes

EDIT - sorry, my head is fried tonight and I totally confused myself here. I have re-written the question - Converting "R.id.myID" from a String to the int value R.id.myID?

Community
  • 1
  • 1
Shane
  • 2,315
  • 3
  • 21
  • 33

3 Answers3

2

Not sure why you wanna do this, but you can pass the int ID directly:

Intent intent = new Intent(Main.this, Categories.class);
intent.putExtra("category", R.array.Categories); //R.array.Categories is an int value
startActivity(intent); 

Then..

int myArrayID = bundle.getIntExtra("category", 0); 
myArray = getResources().getStringArray(myArrayID)));
dmon
  • 30,048
  • 8
  • 87
  • 96
0

Try this in Categoris.class

String myArrayID[];
myArrayId=bundle.getStringArray("category");
Rasel
  • 15,499
  • 6
  • 40
  • 50
-2

Not much of an answer, but that is correct: see getStringArray. You will want to cast it correctly, but otherwise you got it right.

Femi
  • 64,273
  • 8
  • 118
  • 148
  • The Resource getStringArray() method takes an int as input. If she wants to use a String, she needs to use the Bundle getStringArray() method. – glorifiedHacker Aug 19 '11 at 04:17
  • No, `Bundle.getStringArray` does NOT return a Resource array. The type being put into the Intent extra is incorrect, but the method of choice I listed IS is correct. Not sure why the downvotes, but I stand by the method I pointed to. – Femi Aug 19 '11 at 10:54