Bhushan Baviskar
Bhushan Baviskar

Reputation: 23

How to use string.xml files values in Android app

This is the code..

string.xml

<?xml version="1.0" encoding="utf-8" ?> 
- <resources>
- <string-array name="countries_array">
  <item>Bahrain</item> 
  <item>Bangladesh</item> 
  <item>Barbados</item> 
  <item>Belarus</item> 
  <item>Belgium</item> 
  <item>Belize</item> 
  <item>Benin</item> 
  </string-array>
  </resources>

Layout

list_item.xml

<?xml version="1.0" encoding="utf-8" ?> 
  <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" android:textSize="16sp" />

main.xml

<?xml version="1.0" encoding="utf-8" ?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
  <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> 
  </LinearLayout>

.java file

package examples.com;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class HelloListViewActivity extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      String[] countries = getResources().getStringArray(R.array.countries_array);
      setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, countries));

      ListView lv = getListView();
      lv.setTextFilterEnabled(true);

      lv.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
        }
      });
    }
} 

This is code of generating a dyanamic list of some names as in xml file. It takes list of names as input from xml file and display it dyanamicaly. While fetching data from xml file i am getting an error of undefined symbol as-

error is in this line

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

cant find symbol array in R.

countries_array is in string.xml file. If this method of accessing it is wrong then how to access it?
Thanx in advance.

Upvotes: 2

Views: 8652

Answers (1)

devunwired
devunwired

Reputation: 63293

Put your array declaration into a file titled arrays.xml in the res/values directory. This is where the system is looking when you try to type R.array.countries_array as a resource identifier. In other words,

res/values/arrays.xml

<?xml version="1.0" encoding="utf-8" ?> 
<resources>
  <string-array name="countries_array">
    <item>Bahrain</item> 
    <item>Bangladesh</item> 
    <item>Barbados</item> 
    <item>Belarus</item> 
    <item>Belgium</item> 
    <item>Belize</item> 
    <item>Benin</item> 
    </string-array>
</resources>

HTH!

Upvotes: 2

Related Questions