res / values / strings.xml中的字符串数组

我想知道如何在strings.xml中创建一个字符串数组,以及如何在java代码中访问它。 我需要这个才能使用setText()。 请帮忙。

strings.xml中

   // name of the string array Mercury // items Venus Earth Mars   

在你的活动课上。

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

getResource需要活动上下文。 如果它在非活动类中,您将需要可以传递给非活动类构造函数的活动上下文。

资源 @

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中创建一个String数组,如下所示:

  first string second  

然后您可以使用以下方法访问它:

 String[] str = getResources().getStringArray(R.array.my_string_array);