简体   繁体   中英

How to Fetch text from array.xml in android

I am trying to fetch text from array.xml which i have added in strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>    
    <string name="app_name">RawFetc</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>

    <string-array name="array" >
        <item>Hello I am List</item>
        <item> I am als a List</item>

    </string-array>    
</resources>

And the java class for this is

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String[] arr = getResources().getStringArray(R.array.array);

    }
}

I want to know the next step after this, to show the text when application get starts. Please help

Now since you have fetched array using

String[] arr = getResources().getStringArray(R.array.array);

You can fetch string from arr using indices like arr[0], arr[1], ... so on.

Declare a TextView in activity_main.xml which will show the text from the array.

public class MainActivity extends Activity {

private TextView txtView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    txtView = (TextView)findViewById(R.id.txt); //The id could be different 
    String[] arr = getResources().getStringArray(R.array.array);
    txtView.setText(arr[0]); //arr[1], arr[2] , whatever you want to set

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM