首页 > 解决方案 > 无法从嵌套在微调器中的另一个 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;
        }

    }

}

标签: androidarraylistandroid-spinner

解决方案


你应该在你的方法上添加这个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) {

        }
    });

推荐阅读