无法从嵌套在微调器中的另一个 Arraystring 的位置设置 ArrayString 的 Text

Can not setText of ArrayString from taking position of another Arraystring nested in spinner

我正在编写一个计算应用程序,我希望用户 select 来自 materialNames arraylist(微调器的适配器)的项目。所以在微调器中,我将获得位置并在其他 arraylist 中的相同位置获取相关值,称为 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) {

        }
    });