问题描述
我需要把多个纺纱到我的活动。它们的数目将被动态定义的,这将是2-7项
I need to put multiple spinners into my activity. Number of them would be defined dynamically, it would be 2-7 items.
目前,我有这样的:
-- clip:
Spinner spinnerOne = (Spinner) findViewById(R.id.spinnerBrowse);
spinnerOne.setAdapter(new Adapter(Browse.this, R.layout.browse_spinner_rows, picsIds));
spinnerOne.setOnItemSelectedListener(this);
Spinner spinnerTwo = (Spinner) findViewById(R.id.spinnerBrowse);
spinnerTwo.setAdapter(new Adapter(Browse.this, R.layout.browse_spinner_rows, picsIds));
spinnerTwo.setOnItemSelectedListener(this);
-- clap.
这些微调的内容是一样的,他们只能通过名称各不相同。是它在某种程度上可以通过这些名字迭代,就像把名字到数组{SpinnerOne,SpinnerTwo,SpinnerThree,...},只是产生所需微调的项目数量在一个循环?
Contents of these spinners is the same, they only vary by names. Is it somehow possible to iterate by these names, like putting names into array { "SpinnerOne", "SpinnerTwo", "SpinnerThree", ... } and just generate number of needed spinner items in a loop?
推荐答案
当然你的布局定义一个容器,然后dinamically添加纺纱
Sure define a container on your layout and then add spinners dinamically
int numOfSpinners;
LinearLayout container = (LinearLayout)findViewById(R.id.container);
for(int i=0;i<numOfSpinners;i++)
{
Spinner spinner = new Spinner(this);
spinner.setAdapter(new Adapter(Browse.this, R.layout.browse_spinner_rows, getPicsIds(i)));
spinner.setOnItemSelectedListener(this);
container.addView(spinner);
}
在这里getPicsIds()获取每次迭代正确的项目
where getPicsIds() fetches the right items for each iteration
这篇关于在活动中多纺纱 - 能不能重构,使其动态生成的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!