我正在尝试创建一个能给我价值的应用 Spinner1 / Spinner2 *(Spinner3 * 8) 但是,我无法弄清楚如何为我的微调器中的项目赋值。请帮帮我:
我对此很陌生,所以如果我改变太多,我害怕搞乱代码。
package com.example.test.fmpu;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements
AdapterView.OnItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Spinner Element
Spinner spinner = (Spinner)findViewById(R.id.speedspin);
Spinner spinner2 = (Spinner)findViewById(R.id.tilespin);
Spinner spinner3 = (Spinner)findViewById(R.id.boxspin);
// Spinner click listener
spinner.setOnItemSelectedListener(this);
spinner2.setOnItemSelectedListener(this);
spinner3.setOnItemSelectedListener(this);
// Speed spinner drop down elements
List<String> speed = new ArrayList<>();
speed.add("0.5 m/s");
speed.add("1.0 m/s");
speed.add("1.5 m/s");
speed.add("2.0 m/s");
speed.add("2.5 m/s");
speed.add("3.0 m/s");
speed.add("3.5 m/s");
speed.add("4.0 m/s");
speed.add("4.5 m/s");
// Tile spinner drop down elements
List<String> tile = new ArrayList<>();
tile.add("16 fliser");
tile.add("18 fliser");
tile.add("20 fliser");
tile.add("22 fliser");
tile.add("24 fliser");
// Box spinner drop down elements
List<String> box = new ArrayList<>();
box.add("1 kasser");
box.add("2 kasser");
box.add("3 kasser");
box.add("4 kasser");
box.add("5 kasser");
box.add("6 kasser");
box.add("7 kasser");
box.add("8 kasser");
box.add("9 kasser");
box.add("10 kasser");
box.add("11 kasser");
box.add("12 kasser");
box.add("13 kasser");
box.add("14 kasser");
box.add("15 kasser");
box.add("16 kasser");
box.add("17 kasser");
box.add("18 kasser");
box.add("19 kasser");
box.add("20 kasser");
box.add("21 kasser");
box.add("22 kasser");
box.add("23 kasser");
box.add("24 kasser");
box.add("25 kasser");
box.add("26 kasser");
box.add("27 kasser");
box.add("28 kasser");
box.add("29 kasser");
box.add("30 kasser");
box.add("31 kasser");
box.add("32 kasser");
box.add("33 kasser");
box.add("34 kasser");
box.add("35 kasser");
box.add("36 kasser");
box.add("37 kasser");
box.add("38 kasser");
box.add("39 kasser");
box.add("40 kasser");
// Adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, speed);
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, tile);
ArrayAdapter<String> dataAdapter3 = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, box);
// Drop down layout style - list view
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
spinner2.setAdapter(dataAdapter2);
spinner3.setAdapter(dataAdapter3);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
// Showing selected spinner item
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
}
}
答案 0 :(得分:0)
好的,所以任何有兴趣的人,我想出来了,我最终创建了整数和双打的列表,如下所示:
<div class="formulir">
<form action="php/upload.php" method="post" enctype="multipart/form-data">
<div class="inputBtnSection">
<input id="uploadFile" class="disableInputField" placeholder="Choose File" disabled="disabled" />
<label class="fileUpload">
<input id="uploadBtn" type="file" class="upload" name="fileToUpload" />
<span class="uploadBtn">Browse File</span>
</label>
</div>
<label class="fileUpload2">
<input id="uploadtn2" type="submit" value="Upload Image" name="submit" class="submit">
<span class="uploadBtn2">upload</span>
</label>
</form>
</div>
然后我通过将列表转换为 for 循环来缩短代码,如下所示:
List<Integer> tile = new ArrayList<>();
因此使代码更短
最后,为了得到实际计算,我将变量分配给整数,如下所示:
for (int i2=16; i2 <= 24; i2+=2)
{
tile.add(i2);
}
让他们像这样计算:
int kasser = Integer.parseInt(spinner3.getSelectedItem().toString());
int fliser = Integer.parseInt(spinner2.getSelectedItem().toString());
double fart = Double.parseDouble(spinner.getSelectedItem().toString());
double resultat = kasser * fliser / (fart * 8);
所以我希望有人可以使用它,如果他们自己也有同样的问题!