Search code examples
javaandroidandroid-fragmentsandroid-listviewandroid-listfragment

Unable to use string resources for list view items


I'm trying to use string resources for my 2-line list view items rather than hard-coding them but I get this error. How can I fix that?

Before using string resources

public class ListData {

    public static final String[][] items = {
            {"America","America Description"},
            {"Europe","Europe Description"},
    };
}

After using string resources

public class ListData {

    public static final String[][] items = {
            {R.string.america,R.string.america_description},
            {R.string.europe, R.string.europe_description},
    };
}

Error

Incompatible types. Required: java.lang.String | Found: int


Solution

  • it is because R.string.america is an integer which represent a string inside strings.xml. So you should change the type for String[][] to int[][]. If you have to assign the value to a TextVIew android will take care of the look up in strings.xml.