Using strings.xml w / Android

I looked and tried a lot of things, but it seems my application does not get the values ​​for the strings in strings.xml. It actually seems empty. I think the problem is initialized there.

strings.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 install cityID = R.strings.itb_cityIDand the city=itb_cityright way?

+3
source share
3 answers

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

Note. Static parameter Contextcannot be used. If you are inside an Activity, just use thisor call getResources()directly. Otherwise, you will need to get the context handle of your application through getApplicationContext().

+13
source

This example may be useful.

  • XML, res/values ​​/strings.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="hello">Hello!</string>
    </resources>
    
  • XML

    <TextView
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="@string/hello" />
    
  • :

    String string = getString(R.string.hello);

    getString (int) getText (int) . getText (int) , .

http://developer.android.com/guide/topics/resources/string-resource.html

+7

, . , , R.java . "", "". . , , Context., getResources(). ( ). , , , strings.xml, .

0

All Articles