There are 2 edittext, 2 spinner and a button with a textview. This is the idea. Enter 2 values ​​into edittext on one spinner, select a valid brand, and in the second, the size and everything counted when the button is pressed ....

dlina = (EditText)findViewById(R.id.dlina); shirina = (EditText)findViewById(R.id.shirina); tip = (Spinner)findViewById(R.id.tip); razmer = (Spinner) findViewById(R.id.razmer); rasschet = (Button)findViewById(R.id.rasschet); otvet = (TextView)findViewById(R.id.otvet); tip.setOnItemSelectedListener(this); rasschet.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if (!TextUtils.isEmpty(dlina.getText().toString())) { a = Integer.parseInt(dlina.getText().toString()); } else { a = 0; if (dlina.getText().toString().length() == 0) dlina.setError(getString(R.string.dlina)); } if (!TextUtils.isEmpty(shirina.getText().toString())) { b = Integer.parseInt(shirina.getText().toString()); } else { b = 0; if (shirina.getText().toString().length() == 0) shirina.setError(getString(R.string.shirina)); } d = a * b * c; otvet.setText(Integer.toString(d)); } }); @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { String tips = String.valueOf(tip.getSelectedItem()); pos = tip.getSelectedItemPosition(); if (pos == 0) { c = 2; } if (pos == 1) { c = 4; } if (pos == 2) { c = 6; } //Toast.makeText(this, tips, Toast.LENGTH_SHORT).show(); if (tips.contentEquals("ballon")) { List<String> list = new ArrayList<String>(); list.add("120,7 × 19,3"); list.add("128,8 × 18,6"); ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list); dataAdapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item); dataAdapter.notifyDataSetChanged(); razmer.setAdapter(dataAdapter); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } 

With this code, when I press the button, I get an answer because the letter C in the formula is crammed under the first spinner, and I need the letter C in the formula to react to the second spinner into which I am dimensioning .... Help the citizens, atoms have already broken their head ... Thanks in advance

  • 2
    I did not understand. But it seems you just need different handlers on different spinners to hang. - Yuriy SPb
  • My first spinner is a brand, when I chose any brand on the second spinner the dimensions of this brand appear ... so the letter C from the formula in my code reacts to the spinner with the brand, but I need the size - user3711927 V
  • Well, you haven’t assigned a listener to the second spinner ... Use an anonymous class for the second spinner that is created directly in the assigning-listener method. something like spinner.setOnItemSelectedListener(new OnItemSelectedListener(){...}); - YurySPb
  • Thank. It helped .... - user3711927
  • one
    Again, I do not really understand you. Did you remove the work from C from the handler of the first spinner? You need to act this way: in the first spinner, on the position, create data for the second spinner, fill its adapter and assign it to the listener, in which you must work with C .. - Yuri SPb

1 answer 1

You need different spinners to process in different handlers. The algorithm should be something like this:

In the handler of the first spinner when selecting an item, by position:

  1. create data for the second spinner
  2. fill his adapter and assign him a listener
  3. in which you should fill the variable C with data.

     spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { public void onItemSelected(AdapterView<?> arg0, View v, int position, long id) { List<String> list = new ArrayList<String>(); ArrayAdapter<String> dataAdapter; //тут по позиции создаёте данные для второго спиннера switch(position) { case 0: list.add("120,7 × 19,3"); list.add("128,8 × 18,6"); dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list); dataAdapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item); dataAdapter.notifyDataSetChanged(); razmer.setAdapter(dataAdapter); //тут назначайте слушатель второму спиннеру. break; case 1: //создавайте другие данные для другого типа list.add("42 × 42"); list.add("over9000 × over9000 "); dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list); dataAdapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item); dataAdapter.notifyDataSetChanged(); razmer.setAdapter(dataAdapter); //тут назначайте слушатель второму спиннеру. break; } public void onNothingSelected(AdapterView<?> arg0) { } });