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.