Java – use strings xml w / Android

I read a lot of things, but it seems that my app didn't get strings The value of a string in XML It actually seems to be blank I think there is a problem with initialization

strings. In XML

<string name="itb_cityID">2</string>
        <string name="itb_city">New York</string>

constants. Java excerpt:

public class ConstantData {

public static String cityID="2";
public static String city="New York";

How to set cityid = r.strings itb_ Cityid and city = ITB_ The right way for city?

Solution

String yourString = Context. getResources(). getString(R.string.your_string);

Note: you cannot use context statically If you are in an activity, just use it or call getresources () directly Otherwise, you need to get the handle of the application context through getapplicationcontext()

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>