xiaoyanmei
xiaoyanmei

Reputation: 151

How to declare the 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?

Upvotes: 5

Views: 12781

Answers (4)

Adam Stelmaszczyk
Adam Stelmaszczyk

Reputation: 19837

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));

Upvotes: 6

NoNaMe
NoNaMe

Reputation: 6222

try this...

add import as well

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

List<String> lst = new  ArrayList<String>();

To Add

lst.add(getResources().getString(R.string.helloworld));

Upvotes: 1

Manmeet Singh Batra
Manmeet Singh Batra

Reputation: 467

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

    List<Integer> list1 = new ArrayList<Integer>();
    list1.add(R.string.helloworld);

Upvotes: 2

Pankaj Singh
Pankaj Singh

Reputation: 2311

Try this

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

may this helps you

Upvotes: 0

Related Questions