Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare the List?

Tags:

android

list

I save all the application character in R.string. Now I want to save the R.string character in a List.

How to declare the List?

I use

List<String> list = new ArrayList<String>();
list.add(R.string.helloworld);

And

List<integer> list = new ArrayList<integer>();
list.add(R.string.helloworld);

But I can not add it.

How to do this?

like image 942
xiaoyanmei Avatar asked Jan 17 '13 09:01

xiaoyanmei


2 Answers

Your declaration is correct, there are problems with R.string.helloworld. This is in fact a static int (declaration of it is in R.java file). Remember to import java.util for List and ArrayList. If you are in Eclipse, press Ctrl + Shift + O to organize imports.

Use getString(R.string.helloworld):

String Resources

So your code will look like:

import java.util.ArrayList;
import java.util.List;    

List<String> list = new ArrayList<String>();
list.add(getResources().getString(R.string.helloworld));
like image 151
Adam Stelmaszczyk Avatar answered Oct 26 '22 01:10

Adam Stelmaszczyk


    List<String> list = new ArrayList<String>();
    list.add(getResources().getString(R.string.helloworld));

    List<Integer> list1 = new ArrayList<Integer>();
    list1.add(R.string.helloworld);
like image 25
Manmeet Singh Batra Avatar answered Oct 26 '22 02:10

Manmeet Singh Batra