使用动态数组名称填充 android spinner

发布于 2024-11-27 23:59:59 字数 549 浏览 0 评论 0原文

我想做这样的事情,请帮助我,

    Spinner spinner = (Spinner) findViewById(R.id.spinner5
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this,**R.array.country_array**, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter5);

我需要使 R.array.country_array 成为动态的,例如将country_code更改为另一个数组名称,如city_array或stat_array。

我试图写“R.array.”+dynamic_variable_array_name

但我失败了..任何帮助

i want to do something like this,Plz help me

    Spinner spinner = (Spinner) findViewById(R.id.spinner5
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this,**R.array.country_array**, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter5);

i need to make R.array.country_array to be dynamic for example to change country_code to another array name like city_array or like stat_array.

i tried to write "R.array."+dynamic_variable_array_name

but i failed..any help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薄荷梦 2024-12-04 23:59:59

为什么不在将数组引用到 ArrayAdapter 之前初始化数组?

Array myArray = getMyArray();

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, myArray, android.R.layout.simple_spinner_item);

[更新]
好的,我刚刚注意到 ArrayAdapter.createFromResource 不适用于非资源。但这会起作用:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, myArray);

Why don't you initialize your array before you refer it to the ArrayAdapter?

Array myArray = getMyArray();

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, myArray, android.R.layout.simple_spinner_item);

[Update]
Ok, i've just noticed ArrayAdapter.createFromResource doesn't work with non-resources. But this will work:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, myArray);
九局 2024-12-04 23:59:59

试试这个

Spinner countrySpinner = (Spinner) findViewById(R.id.search_country);
CharSequence[] myArray = new CharSequence[]{
                "Qatar","UAE","India","America"
        };
ArrayAdapter<CharSequence> countryAdapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item,myArray);
        countryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
countrySpinner.setAdapter(countryAdapter);

Try This

Spinner countrySpinner = (Spinner) findViewById(R.id.search_country);
CharSequence[] myArray = new CharSequence[]{
                "Qatar","UAE","India","America"
        };
ArrayAdapter<CharSequence> countryAdapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item,myArray);
        countryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
countrySpinner.setAdapter(countryAdapter);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文