0

I've got multiple buttons, when each one is clicked they all launch the same activity. Depending on the button clicked the second activity will load a different String Array, how do I pass and catch a string array via an intent? Note the String Arrays are saved in an .xml file.

At the moment the second activity is just loading a specific array

    res = getResources();
    arraytoload = res.getStringArray(R.array.ninetyseven);

How would I get the button to send a message containing for example (R.array.twentyfive) and then have that load as arraytoload.

First activity

    one.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            Intent intent = new Intent("com.example.test.MainActivity");
            intent.putExtra("id", "ninetyseven");
            startActivity(intent);
        }
    });

Second activity

private String[] arraytoload;
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String idString = getIntent().getExtras().getString("id");
    int id = getResources().getIdentifier(idString, "array", getPackageName());
    arraytoload = res.getStringArray(id);

}
SpunkTrumpet
  • 133
  • 4
  • 11

3 Answers3

0

In your main activity, in the onClick() method for your button.

String s = "hello!";
Intent intent = new Intent(this, NewClass.class);
intent.putExtra("extraInfo1",s);
startActivity(intent);

In NewClass.class, in the onCreate() method.

Intent intent = getIntent();
String passedString = intent.getStringExtra("extraInfo1","doesn't exist!");
System.out.println(passedString);

This will print "hello!"

The second argument in getStringExtra() is the default value, if not string is found along with the intent with the given tag (in this case, "extraInfo1" is the tag). passedString will be set to "doesn't exist!" if you start the intent somewhere else and don't attach the string with the tag.

Alex K
  • 8,269
  • 9
  • 39
  • 57
0

Based on the button clicked, send the array name with the intent like:

Button 1:

i.putExtra("id", "ninetyseven");

Button 2:

i.putExtra("id", "ninetyeight");

then retrieve it in the Second Activity:

String idString = getIntent().getExtras().getString("id");

then in order to get the array you can use the following code:

String idString = getIntent().getExtras().getString("id");
int id = getResources().getIdentifier(idString, "array", getPackageName());
arraytoload = res.getStringArray(id);

this line of code:

getIdentifier(idString, "array", getPackageName());

is to generate this: R.array.ninetyseven

hope this help you solve your issue.

Coderji
  • 7,655
  • 5
  • 37
  • 51
  • This is helping a bit however I'm getting a null pointer at arraytoload = res.getStringArray(id);, not sure why though. – SpunkTrumpet Nov 28 '14 at 02:43
  • oh sorry.. please change "id" to "array" check the edit part within `getIdentifier(idString, "id", getPackageName());` to `getIdentifier(idString, "array", getPackageName());` – Coderji Nov 28 '14 at 02:44
  • here is a [link](http://stackoverflow.com/questions/7565108/android-referencing-string-array-using-another-string-with-getidentifier-and) that worked with somebody.. can you `Log.d("id", ""+ id);` before you assign `arraytoload` so we check the generated id – Coderji Nov 28 '14 at 03:11
0

you can simply parse the array resource id as an extra in intent.

Intent intent = new Intent(this, SecondActivity.class)
intent.putExtra("arrayResourceId", R.array.ninetyseven);
startActivity(intent);

in your second activity, onCreate method

int arrayResourceId = getIntent().getIntExtra("arrayResourceId", -1);
arraytoload = res.getStringArray(arrayResourceId);
Rene Xu
  • 1,093
  • 1
  • 9
  • 14