android - 无法从嵌套在微调器中的另一个 Arraystring 的位置中设置 ArrayString 的文本
问题描述
我正在编写一个计算应用程序,我希望用户从 materialNames 数组列表(微调器的适配器)中选择一个项目。所以在微调器中,我将获取位置并在其他数组列表中的相同位置获取相关值,这称为allowableStress。之后,我想在 Textview (allowableStressText) 中显示该值。但是使用下面的代码我无法实现我的目的。您能否检查一下天气,我的编码逻辑是否有问题。
public class MainActivity extends AppCompatActivity {
TextView allowableStressText;
String allowableStressValue;
String spinnerValue;
ArrayList<String> allowableStress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
allowableStressText = findViewById(R.id.allowableStressText);
//Spinner kodu başlıyor.
Spinner spinnerMaterial = (Spinner) findViewById(R.id.spinnerMaterial);
//Material Spinner için data kaynağı
ArrayList<String> materialNames = new ArrayList<>();
materialNames.add("Other Material than listed");
materialNames.add("API5L A25 Pipe Type: BW, ERW, S");
materialNames.add("API5L A Pipe Type: ERW, S, DSA");
materialNames.add("API5L B Pipe Type: ERW, S, DSA");
materialNames.add("API5L X42 Pipe Type: ERW, S, DSA");
materialNames.add("API5L X46 Pipe Type: ERW, S, DSA");
allowableStress = new ArrayList<>();
allowableStress.add("Enter SMYS value");
allowableStress.add("172");
allowableStress.add("207");
allowableStress.add("241");
allowableStress.add("290");
allowableStress.add("317");
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, materialNames);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerMaterial.setAdapter(adapter);
}
public class SpinnerActivity extends MainActivity implements AdapterView.OnItemSelectedListener{
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id){
spinnerValue = (String) parent.getItemAtPosition(pos);
allowableStressValue = allowableStress.get(Integer.parseInt(spinnerValue));
allowableStressText.setText(allowableStressValue);
}
public void onNothingSelected(AdapterView<?> parent){
Toast.makeText(getApplicationContext(), "Please select a material", Toast.LENGTH_LONG ).show();
return;
}
}
}
解决方案
你应该在你的方法上添加这个onItemSelectedListener
尝试:onCreate
spinnerMaterial.setAdapter(adapter);
spinnerMaterial.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
spinnerValue = (String) parent.getItemAtPosition(pos);
allowableStressValue = allowableStress.get(Integer.parseInt(spinnerValue));
allowableStressText.setText(allowableStressValue);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
推荐阅读
- python - 我可以在计算的同时得到未来的未来和计算状态吗?
- c# - MODEL和DAL之间的循环依赖
- postgresql - 为什么 psql --version 在 zsh macOS 中不起作用?
- flutter - 为什么我的 ListView.builder() 不可滚动?
- python - .predict() 仅在 CPU 上运行,即使 GPU 可用
- sap-ase - 如何在 Sybase ASE 中查找列的默认值
- windows - 脚本适用于 windows 10 但不适用于 windows 7
- node.js - 使用 Express.js 在 if 语句中等待
- oauth - 如何将我自己的 OAuth 服务器添加到 OAuth.tools?
- highcharts - 如何在highchart的全屏视图中将图表设置在中心