Android的 - 检索字符串数组的资源 [英] Android - retrieve string array from resources

查看:148
本文介绍了Android的 - 检索字符串数组的资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是code,我做检索字符串数组项:

 的String [] menuArray;

@覆盖
公共无效的onCreate(包savedInstanceState)
{
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    滚动型SV =新的滚动型(本);
    的LinearLayout LL =新的LinearLayout(本);
    ll.setOrientation(LinearLayout.VERTICAL);
    sv.addView(Ⅱ);

 //创建一个包含所有列表项的ArrayAdapter
    ArrayAdapter<字符串>适配器;

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


    的for(int i = 0; I< menuArray.length;我++)
    {
        按钮B =新的按钮(这一点);
        b.setText(menu​​Array [I]);
        ll.addView(B);
    }

    this.setContentView(SV);
 }
 

这是的strings.xml文件:

 <字符串数组名=菜单>
        <项目> 1< /项目>
        <项目> 2'; /项目>
        <项目>第3版; /项目>
        < /字符串数组>
 

然而,在 R.array.menu 有这个问题进行编译: 作为ADT 14,资源字段不能被用作开关的情况下。 调用此修复程序,以获取更多信息。

解决方案

 的for(int i = 0; I< menuArray.length;我++)
{
    按钮B =新的按钮(这一点);
    b.setText(menu​​Array [I]);
    ll.addView(B);
}
 

删除下面的语句

 尝试{
        X =计数();
    }赶上(IOException异常E1){
        // TODO自动生成的catch块
        e1.printStackTrace();
    }
 

和尝试给予布局的高度和宽度都布局。

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.

解决方案

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..

这篇关于Android的 - 检索字符串数组的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆