2

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.

1 Answer 1

2

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!

3
  • i tried with strings.xml and replaced arrays with strings.. but it doesnt worked..!!!! Not working... Commented Dec 18, 2011 at 18:14
  • You must either have the array in arrays.xml and use R.array.contries_array as the identifier, OR put the array in strings.xml and use R.string.countries_array as the identifier...the two must match whatever you choose.
    – devunwired
    Commented Dec 18, 2011 at 21:14
  • @Devunwired: instead of countries names i try to write boolean.. what will be returned -- string as an output?
    – Aada
    Commented Mar 29, 2013 at 11:14

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.