我正在尝试构建一个应用,该应用从2个微调器中获取2个输入的氧化还原对,以显示有关由这些化学物质制成的电池的信息,例如用作阳极/阴极的物质,每种物质的还原电位半反应和细胞等。
我的问题是,我无法找到任何方式来获取两个微调器的输入以进行计算,只是让两个微调器执行相同的操作。
我当前的代码是这样:
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
Spinner spinner1, spinner2;
ArrayAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = ArrayAdapter.createFromResource(this, R.array.pairs, android.R.layout.simple_spinner_item);
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setAdapter(adapter);
spinner1.setOnItemSelectedListener(MainActivity.this);
spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner2.setAdapter(adapter);
spinner2.setOnItemSelectedListener(MainActivity.this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String choice1 = parent.getItemAtPosition(position).toString();
double first = 0;
double second = 0;
double anode;
double cathode;
if (choice1 == getResources().getString(R.string.MnO4)){
first = 0.60;
Toast.makeText(parent.getContext(), choice1, Toast.LENGTH_SHORT).show();
} else if (choice1 == getResources().getString(R.string.Cl)) {
first = 1.36;
Toast.makeText(parent.getContext(), choice1, Toast.LENGTH_SHORT).show();
}
if (first > second) {
cathode = first;
anode = second;
} else {
cathode = second;
anode = first;
}
double cellValue = anode - cathode;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
我是android studio的新手,因此非常感谢您的帮助。 谢谢
答案 0 :(得分:0)
我建议您使用匿名类来处理onItemSelected
。这样,您可以独立处理每个Spinner
的选择。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = ArrayAdapter.createFromResource(this, R.array.pairs, android.R.layout.simple_spinner_item);
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setAdapter(adapter);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Do what you need to do with spinner1 info
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner2.setAdapter(adapter);
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Do what you need to do with spinner2 info
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
现在,您可以删除Override方法: @Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
,您可以删除工具:
implements AdapterView.OnItemSelectedListener