Grant Espanet Grant Espanet - 3 months ago 17
Android Question

Can R.string. be used in a ArrayList?

When I build/run my app, "2131165360" appears in the list view position instead of "LegKick Block 1". Why does this happen when i enter this code?

arrayList.add(String.valueOf(R.string.legkick_block1));


I could solve my problem by doing this but I'd rather grab the value of the string rather than hard-coding it:

arrayList.add("LegKick Block 1");


Is there an approach to put a value of a string in a array list?

Answer

Use list.add(getString(R.string.legkick_block1));

R.string.any_string

returns a integer value to resource, you need to use getResource().getString(R.string.any_string) Or simply getString(R.string.any_string) which returns the string object mapped to the integer resource which you can add in list