public class MainActivity extends AppCompatActivity { private Spinner MainSpinner; private Button Button; String[] spinner1; int i=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button.setOnClickListener((View.OnClickListener) this); EditText EditText = (EditText) findViewById(R.id.EditText); // адаптер ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinner1); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); Spinner MainSpinner = (Spinner) findViewById(R.id.MainSpinner); MainSpinner.setAdapter(adapter); } public void onClick(View Button) { spinner1[i]=(EditText.getText()); i=i+1; } } 

You need to add an item to the spinner from EditText when you click the button. I swear at the word getText . Issues:

non-static method 'getText ()' cannot be referenced from a static context

What is the problem? Is it possible to add items at all to a spinner by pressing a button? How to implement it correctly?

  • Writes that the method is not static. This happens if you call a method without initializing the object. - Sanek Zhitnik

2 answers 2

An example for you.

 public class MainActivity extends AppCompatActivity { ArrayList<String> spinner1 = new ArrayList<>(); EditText editText; ArrayAdapter<String> adapter; Spinner mainSpinner; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); spinner1.add("IM YOU FIRST SPINNER NAME!"); editText = (EditText) findViewById(R.id.EditText); mainSpinner = (Spinner) findViewById(R.id.MainSpinner); // адаптер adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinner1); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); mainSpinner.setAdapter(adapter); } public void onClick(View v){ spinner1.add(editText.getText().toString()); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); mainSpinner.setAdapter(adapter); } } 
  • Thank you so much! Everything is working! Only one caveat: You forgot to initialize the button (Button). I initialized it myself and everything works fine! Thanks again! Good luck to you! - HybridTC25
  • You can impose a method (onClick) in the xml file for your button, then initialization is not necessary in the class. You are welcome. - Shwarz Andrei
  • Hello again! Today I wanted to add an if to the program for checking the value of EditText, and he refuses to work at point-blank. Although the condition is satisfied, although not - the result is zero. As if that if there is no in the program. Tell me, please, how to add a conditional statement correctly so that it works. Here's the code: 'public void onClick (View button) {if ((editText.getText (). ToString ()) == "12345") {Toast.makeText (getBaseContext (), "Testing", Toast.LENGTH_SHORT) .show (); ' - HybridTC25
  • Everything is no longer necessary! I entered a new variable CurrentText assigned to it a value from EditText and wrote like this: 'if (CurrentText.equals ("12345"))', and now everything works! Anyway, thanks for the help! Without you, I would not have written this program! Goodbye! - HybridTC25

Variable names are usually written in small letters. And for good reason. It is because of this that you have a problem. You called a variable of type EditText named EditText and the compiler now does not know what you are trying to tell it: call the static (and not existing) method of the EditText class or call it of your variable. And since He expects you to write the code correctly. He still interprets your order as a call to a nonexistent static method of the EditText class, and reports to you.


Also, you have an array for spinner points not initialized and the program will crash because of this during the attempt to add something to it. I think you should not use an array but a list ( ArrayList for example). and do not forget to initialize it. And the adapter spiner to notify about changes in it.

  • yes even if all this is done there will still be a bunch of mistakes - Shwarz Andrei
  • @ShwarzAndrei, for sure. But one has to start somewhere. But how can I do this with a spinner and I won’t figure it out) - YuriySPb
  • Well, yes))) there Edit does not even get visible, the button is not inits, then the leaflet is hung, but displayed outside the class, then with the adapter there would be errors and Yes EditText - Editable and needed String - Shwarz Andrei
  • one
    @ShwarzAndrei, yeah, everything is sad) - Yuriy SPb