在水库/价值/ strings.xml中字符串数组 [英] String Array in res/values/strings.xml

查看:149
本文介绍了在水库/价值/ strings.xml中字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何创建一个字符串数组中的strings.xml,以及如何访问它的Java code。我需要这一点是为了使用的setText()。请大家帮帮忙。

I would like to know how to create an array of strings inside strings.xml, and how to access it in java code. I need this in order to use the setText(). Please help.

推荐答案

的strings.xml

strings.xml

 <?xml version="1.0" encoding="utf-8"?>
 <resources>
<string-array name="planets_array"> // name of the string array
    <item>Mercury</item> // items
    <item>Venus</item>
    <item>Earth</item>
    <item>Mars</item>
</string-array>
</resources>

在您的活动类。

  Resources res = getResources(); 
  String[] planets = res.getStringArray(R.array.planets_array);

                 or

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

的getResource 需要活动范围内。如果其在非活动类则需要其可以被传递到非活动类构造的活性上下文

getResource requires activity context. If its in a non activity class you will need the activity context which can be passed to the non activity class contructor.

源@

http://developer.android.com/guide/topics/resources/string-resource.html#StringArray

编辑:

  new CustomAdapter(ActivityName.this);

然后

  class CustomAdapter extends BaseAdapter
  {
      Context mContext;
      public CustomAdapter(Context context)
      {
           mContext = context;
      }
   ...// rest of the code
  }

使用 mContext.getResources()

这篇关于在水库/价值/ strings.xml中字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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