得到一个字符串数组资源与一个字符串变量字符串、数组、变量、资源

2023-09-07 11:15:20 作者:油炸小可爱

我试图让这个code一个字符串数组资源:

I am trying to get a stringArray resource with this code:

String continent = "europe";  
getResources().getStringArray(R.array.continent);

如果我输入R.array.europe,其确定,但我想这是动态的,所以我在寻找一种方法,在这里使用我的字符串变量。

If I type R.array.europe, its ok, but I want this to be dynamic, so I'm looking for a way to use my string variable here..

这可能是一个简单的Java问题,但我都在Java和Android编程相当小白,我没有找到答案。

This is probably a simple java problem, but I'm quite a noob both in java and android programming, and I didn't find the answer..

在此先感谢;

编辑:所以这里是我的完整onCreate方法,我有没有做错事

edit: so here is my complete onCreate Method, did I do something wrong?

protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
       String continent = (String) getIntent().getExtras().getString("continent");

       int holderint = getResources().getIdentifier(continent, "Array",
               this.getPackageName());

       String[] items = getResources().getStringArray(holderint);


        setListAdapter(new ArrayAdapter<String>(this, 
                android.R.layout.simple_list_item_1, 
                items));

    }

我不知道有关defPackage部分虽然...

I'm not sure about the defPackage part though...

推荐答案

随着code动态创建的资源的名称,你可以看看使用

With the name of the resource created dynamically in code, you could look at using

getResources().getIdentifier(String name, String defType, String defPackage);

这将返回一个ID为指定资源。

This will return an id for a specified resource.