Yuen Tong Yuen Tong - 2 months ago 42
Android Question

Android - retrieve string array from resources

Below is the code that i made to retrieve the string array item:

String[] menuArray;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);

// Create an ArrayAdapter that will contain all list items
ArrayAdapter<String> adapter;

menuArray = getResources().getStringArray(R.array.menu);


for(int i = 0; i < menuArray.length; i++)
{
Button b = new Button(this);
b.setText(menuArray[i]);
ll.addView(b);
}

this.setContentView(sv);
}


This is the strings.xml file:

<string-array name="menu">
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>


However, the R.array.menu having this issue to compile:
As of ADT 14, resource fields cannot be used as switch cases.
Invoke this fix to get more information.

Answer Source
for(int i = 0;i<menuArray.length; i++) 
{
    Button b = new Button(this);
    b.setText(menuArray[i]);
    ll.addView(b);
}

Delete the below statement

 try {
        x = count();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

and try to give layout height and width to all Layouts..