当用户在编辑文本中输入数字时,我的应用程序会创建并显示那么多微调器。然后,用户将从每个微调器中选择一个项目,然后单击保存按钮。我在互联网上查看过,但无法找到从微调器中检索位置的方法。这是我创建微调器的方法:
//numStockTanks is the number of spinners to be created
numStockTanks = Integer.parseInt(numOfStockTanks.getText().toString());
// creates all the fields
for(int i = 1; i <= numStockTanks; i++) {
TableRow tR = new TableRow(this);
// creates the textView
tV1 = new TextView(this);
tV1.setText("Stock Tank #" + i + " size: ");
// add spinner to row
spinner = new Spinner(this);
ArrayAdapter<CharSequence> adapterStockTankSize2 = ArrayAdapter.createFromResource(
this, R.array.StockTankSize, android.R.layout.simple_spinner_item);
adapterStockTankSize2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapterStockTankSize2);
spinner.setId(1000 + i);
// add the TextView and the editText to the new TableRow
tR.addView(tV1);
tR.addView(spinner);
// add the TableRow to the TableLayout
tL.addView(tR,new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
} // end for statement
} // end getNumberCreateSpinner ()
我尝试了几种不同的方法来获得微调器的位置,但我似乎无法弄清楚。这是我目前拥有的:
ArrayList<Integer> mArraySpinner = new ArrayList<Integer>();
for (int i = 0; i < numStockTanks; i++) {
spinner = (Spinner) findViewById(1000 + i);
if (spinner.getSelectedItemPosition() == 1) {
mArraySpinner.add(100);
} else if (spinner.getSelectedItemPosition() == 2) {
mArraySpinner.add(200);
} else if (spinner.getSelectedItemPosition() == 3) {
mArraySpinner.add(210);
} else if (spinner.getSelectedItemPosition() == 4) {
mArraySpinner.add(295);
} else if (spinner.getSelectedItemPosition() == 5) {
mArraySpinner.add(400);
} else mArraySpinner.add(0);
} // end for statement
谢谢你的帮助
编辑:在第一个答案后更新为我的最新尝试
编辑编辑:当我调试“spinner = (Spinner) findViewById(1000 + i);”行时 它说 null 所以我知道我没有正确指向我的微调器这是我需要帮助的
编辑:凹凸