2

I need to dynamically add extras to an intent based on the size of an array. I tried concatenating the name of the key with the value of the counter but to no avail

for(int i = 0; i < pos_list.size(); i++) {
    int pos = pos_list.get(i);
    String EXTRA_NAME = "DetailActivityFragment.EXTRA_NAME_" + Integer.toString(i);
    data.putExtra(EXTRA_NAME, tablet_list[pos].getTabletName());
}

Anyway I could get this to work? Is it even possible?

estmit
  • 133
  • 3
  • 11
  • "DetailActivityFragment.EXTRA_NAME_" seems like a constant, but you are using it as a string that says "DetailActivityFragment.EXTRA_NAME_", this is intentional? – milez Jul 27 '15 at 06:41
  • @milez I defined a bunch of const strings in DetailActivityFragment to be used as keys. I'm basically trying to achieve the effect of calling putExtra(DetailActivityFragment.EXTRA_NAME_0, ...), putExtra(DetailActivityFragment.EXTRA_NAME_1,...) etc with the for loop – estmit Jul 27 '15 at 07:01

2 Answers2

2

The error that this piece of code is prone to is on this line

String EXTRA_NAME = "DetailActivityFragment.EXTRA_NAME_" + Integer.toString(i);

Since you are using " " your key becomes literally DetailActivityFragment.EXTRA_NAME_n where n is an integer. Since DetailActivityFragment contains a bunch of constants, you should remove " " to actually access them

String EXTRA_NAME = DetailActivityFragment.EXTRA_NAME_ + Integer.toString(i);

Also make sure when you are retrieving the values you either use " " if you used them when putting values or do not use them if you wish to use the static values defined in the Fragment.

EDIT: Since DetailActivityFragment contains several fields and you wish to retrieve those based on the Integer, a potential solution would be to create get() methods for them inside DetailActivityFragment. For example in that Fragment define (you might also need to make this static unless you could reference an object of your fragment instead of static reference)

public String getExtraName(int index)
{
   if (index == 0)
{
    return EXTRA_NAME_0;
} else if (index == 1)
{
    return EXTRA_NAME_1;
} //etc
}

and now when you put your extras do

String EXTRA_NAME = DetailActivityFragment.getExtraName(i);

and when you retrieve values use the same method for getting keys.

Another solution would be to use a Map<Integer, String> to retrieve the values. What you originally tried to do is (as far as I know) called reflection and I have no knowledge in it, but you can check out this question.

Community
  • 1
  • 1
milez
  • 2,201
  • 12
  • 31
  • But EXTRA_NAME_ doesn't exist in DetailActivityFragment because I actually defined and initialized EXTRA_NAME_0, EXTRA_NAME_1, EXTRA_NAME_2 etc – estmit Jul 27 '15 at 07:14
0

Try this,

replace

String EXTRA_NAME = "DetailActivityFragment.EXTRA_NAME_" + Integer.toString(i);

with,

String EXTRA_NAME = "DetailActivityFragment.EXTRA_NAME_" + i;

"+" this is concatenation, it inherently converts i to string.

Ankit
  • 101
  • 5