简体   繁体   English

在Android中添加Spinner View而不使用XML布局

[英]Adding Spinner View in Android without using an XML layout

I am trying to create a spinner in my code without using an xml layout at all. 我试图在我的代码中创建一个微调器,而根本不使用xml布局。 It seems that you need an ArrayAdapter with the spinner but the constructors all want a resource id in its set of constructors. 似乎您需要一个带有微调框的ArrayAdapter,但构造函数都希望在其构造函数集中使用一个资源ID。 Well, when not using XML layout i dont see how I can get a resource id. 好吧,当不使用XML布局时,我看不到如何获取资源ID。

        Spinner spinner = new Spinner(this);

        List<String> spinnerList = new ArrayList<String>();
        spinnerList.add("Ice Cream Sandwich");
        spinnerList.add("Hushpuppies");
        spinnerList.add("Pickled Pigs Feet");
        spinnerList.add("Cupcakes");
        spinnerList.add("Chocolate Covered Pretzels");

        ArrayAdapter<String> mySpinnerAdapter = new ArrayAdapter<String>(this, ??);

        //spinner.setAdapter(mySpinnerAdapter);

how should i instantiate an ArrayAdapter here? 我应该如何在此处实例化ArrayAdapter? thanks all in advance and hopefully i didn't make anyone too hungry. 提前谢谢大家,希望我不会让任何人都饿。 :) :)

EDIT: i think i have it now - will test and see... 编辑:我认为我现在拥有它-将测试并看到...

 ArrayAdapter<String> mySpinnerAdapter = new ArrayAdapter<String>(this,  android.R.layout.simple_spinner_item, spinnerList);
 mySpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

 spinner.setAdapter(mySpinnerAdapter);

我能够使用android.R.layout和一个子类来解决我在这里需要的

Try this 尝试这个

Spinner comboAutos = (Spinner) findViewById(R.id.spinnerCarros); comboAutos.setAdapter(new ArrayAdapter<String>( MyActivity.this, android.R.layout.simple_spinner_dropdown_item, list));

where list is a List<String> list with the values that you want :) 其中list是具有所需值的List<String>列表:)

Hope it helps you! 希望对您有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM