After a thorough search and quite a lot of thinking, I couldn't find a solution to the following problem in AndroidStudio:
I have 2 spinners (input and output). I want to pass the value of the input spinner to a method that is called upon selection of a value of the output spinner (onItemSelected). The regarding code passage looks as follows:
private void setupSpinnerListeners() {
spinnerLengthInput.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String itemSelectedInSpinnerLengthInput = parent.getItemAtPosition(position).toString();
checkIfConvertingFromMeter(itemSelectedInSpinnerLengthInput);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
spinnerLengthOutput.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String itemSelectedInSpinnerLengthOutput = parent.getItemAtPosition(position).toString();
updateOutputTextfield(itemSelectedInSpinnerLengthInput, itemSelectedInSpinnerLengthOutput);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
I want the String itemSelectedInSpinnerLengthInput (that gets its value from the input spinner) to be available in the onItemSelected method of the output spinner. How to accomplish this?
Any help is greatly appreciated.
EDIT: Create a global variable INSIDE the setupSpinnerListeners Method, that is an array with length 1. The it'll work as I had intended.
I recommend you to use OnItemSelectedListener.
Then create a globalVariable to get the String to your first Spinner as follows :
String FirstValue = "";
Then you'll need to call this :
spinnerLengthInput.setOnItemSelectedListener(this);
spinnerLengthOutput.setOnItemSelectedListener(this);
Of course you'll need to implements OnItemSelectedListener
Then inside you can do the same that you were doing.
#Override
public void onItemSelected(AdapterView<?> spinner, View view, int position,long id)
{
FirstValue = spinner.getItemAtPosition(position).toString();
checkIfConvertingFromMeter(itemSelectedInSpinnerLengthInput);
}
Then in your other Spinner use the FirstValue value.
You should change itemSelectedInSpinnerLengthOutput as a global variable. After that, you can easy access to it in the onItemSelected method of the output spinner
String itemSelectedInSpinnerLengthInput; // global variable
private void setupSpinnerListeners() {
spinnerLengthInput.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
itemSelectedInSpinnerLengthInput = parent.getItemAtPosition(position).toString();
checkIfConvertingFromMeter(itemSelectedInSpinnerLengthInput);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
spinnerLengthOutput.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String itemSelectedInSpinnerLengthOutput = parent.getItemAtPosition(position).toString();
if(itemSelectedInSpinnerLengthInput != null){
updateOutputTextfield(itemSelectedInSpinnerLengthInput, itemSelectedInSpinnerLengthOutput);
}else{
Toast.makeText(getApplicationContext(), "please select input", Toast.LENGTH_LONG).show();
...
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
Hope this help
Related
ArrayAdapter<CharSequence> adapter_height = ArrayAdapter.createFromResource(this,R.array.Height,android.R.layout.simple_spinner_item);
adapter_height.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
height_spinner.setAdapter(adapter_height);
height_spinner.getOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
The "getOnItemSelectedListener" method is not working with the spinner.Please suggest an edit and solve my problem
[Error is shown as above]
https://i.stack.imgur.com/jym1L.png
You have to use setOnItemSelectedListener instead of getOnItemSelectedListener. like the following.
height_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
In my app, i have one spinner and two autocompletetextview. If i select value from spinner then only allow to type first autocompletetextview, If some value in first autocompletetextview then only allow to type second autocompletetextview.
How to do that.
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
autocompletetextview.setInputType(InputType.TYPE_CLASS_TEXT);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Please anyone help me!
Thanks in advance...
Try this code
autocompletetextview.setEnable(false);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
autocompletetextview.setInputType(InputType.TYPE_CLASS_TEXT);
autocompletetextview.setEnable(true);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
autocompletetextview.setEnable(false);
}
});
autocompletetextview.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
autocompletetextview2.setEnable(s.toString().trim().length() > 0 ? true : false);
}
#Override
public void afterTextChanged(Editable s) {
}
});
I'm new to Android and I'm trying to get a String from spinner. I've made some research but I couldn't find anything useful. Without trying to get String the code is working properly. This is the code which is working:
assert staticSpinner2 != null;
staticSpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String spinnerLanguage2 = staticSpinner2.getSelectedItem().toString();
}
public String getSpin(String spinnerLanguage2) {
return spinnerLanguage2;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
I tried to use a getter method as follows where I didn't get any errors:
assert staticSpinner2 != null;
staticSpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String spinnerLanguage2 = staticSpinner2.getSelectedItem().toString();
}
public String getSpinnerString(String spinnerLanguage2) {
return spinnerLanguage2;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
I'm trying to set this String to a text view using:
assert staticSpinner2 != null;
staticSpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String spinnerLanguage2 = staticSpinner2.getSelectedItem().toString();
}
public String getSpinnerString(String spinnerLanguage2) {
return spinnerLanguage2;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
MainActivityClassName AnObject = new MainActivityClassName();
text.setText(AnObject.getSpinnerString());
Where I get an error saying "cannot resolve method getSpin()". I do realise I'm not passing any parameters to the method but I don't know how I can do it here. I appreciate any help or any other advice to solve the problem in a different way. Thanks in advance.
This is the full code if that helps:
public class MainActivity extends AppCompatActivity {
TextView text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_json);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
text = (TextView) findViewById(R.id.textView);
// spinner 1
final Spinner staticSpinner1 = (Spinner) findViewById(R.id.spinner1);
// Create an ArrayAdapter using the string array and a default spinner
ArrayAdapter<CharSequence> staticAdapter1 = ArrayAdapter
.createFromResource(this, R.array.lang,
android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
staticAdapter1
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
staticSpinner1.setAdapter(staticAdapter1);
// spinner 2
final Spinner staticSpinner2 = (Spinner) findViewById(R.id.spinner2);
staticSpinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
String spinnerLanguage = staticSpinner1.getSelectedItem().toString();
switch (spinnerLanguage) {
case "Afrikaans":
// Create an ArrayAdapter using the string array and a default spinner
ArrayAdapter<CharSequence> staticAdapterAF = ArrayAdapter
.createFromResource(getBaseContext(), R.array.af,
android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
staticAdapterAF
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
staticSpinner2.setAdapter(staticAdapterAF);
break;
case "French":
// Create an ArrayAdapter using the string array and a default spinner
ArrayAdapter<CharSequence> staticAdapterFR = ArrayAdapter
.createFromResource(getBaseContext(), R.array.fr,
android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
staticAdapterFR
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
staticSpinner2.setAdapter(staticAdapterFR);
break;
case "English":
// Create an ArrayAdapter using the string array and a default spinner
ArrayAdapter<CharSequence> staticAdapterEN = ArrayAdapter
.createFromResource(getBaseContext(), R.array.en,
android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
staticAdapterEN
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
staticSpinner2.setAdapter(staticAdapterEN);
break;
case "Turkish":
// Create an ArrayAdapter using the string array and a default spinner
ArrayAdapter<CharSequence> staticAdapterTR = ArrayAdapter
.createFromResource(getBaseContext(), R.array.tr,
android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
staticAdapterTR
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
staticSpinner2.setAdapter(staticAdapterTR);
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
assert staticSpinner2 != null;
staticSpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String spinnerLanguage2 = staticSpinner2.getSelectedItem().toString();
}
public String getSpin(String spinnerLanguage2) {
return spinnerLanguage2;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
MainActivity AnObject = new MainActivity();
text.setText(AnObject.getSpinnerString());
}
}
You can do this:
String selectedText = null;
staticSpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedText = parent.getItemAtPosition(position).toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Now you can use the selectedText variable to pass on to whatever method you want. But, keep in mind, unless you select something in the spinner, it's value would be null.
If you really have to use it outer, you can do this :
public void setTextView(String text){
text.setText(text);
}
and call this method inside your onItemSelected:
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
setTextView(staticSpinner2.getSelectedItem().toString());
}
Having read that piece of code, why don't you extract "spinnerLanguage2" to your main class and then access it from there? Assigning the value inside the OnItemSelected method.
EDIT:
My previous answer is wrong. Try to implement an interface in order to communicate with the other class. Thus, calling a method of that interface you can pass the value. I give you a link with this (It explains it for fragments, but yo can communicate your classes too):
http://developer.android.com/intl/es/training/basics/fragments/communicating.html
as demonstrated here by jalopaba, I already created a new class: How do you get the selected value of a Spinner?
public class MyItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
String selected = parent.getItemAtPosition(pos).toString();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
and registering this to the spinner in the original class:
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
However, I still can't use that selected string yet to fill in my code in the same class:
textView.setText(selected);
I'm new to this Android anyway, so this question may be too dummy to some of you
Add the setText code in the onItemSelected:
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
yourTextView.setText(parent.getSelectedItem().toString);
}
Try this code,I hope It's help you.
final CharSequence[] array_min = {"No
Min","100","200","300","400", "500","600","700","800","900","1000",
"1100","1200","1300","1400","1500","1600","1700","1800","1900","2000","2500","3000","3500" };
Spinner s = (Spinner) layout.findViewById(R.id.viewSpin);
adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, array_min);
s.setAdapter(adapter);
s.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View arg1,
int arg2, long arg3)
{
selItem = parent.getSelectedItem().toString();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Use global variable..make following changes in your code
String selected="";
public class MyItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
selected = parent.getItemAtPosition(pos).toString();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
textView.setText(selected);
After register spinner ,you can get selected item from getSelectedItem() method on any action such ocClick()
regType = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.regType, android.R.layout.simple_spinner_dropdown_item);
regType.setAdapter(adapter);
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnSave:
intent.putExtra("regtype",regType.getSelectedItem().toString());
startActivity(intent);
break;}}
I have this code:
hubSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
final MediaPlayer mp2 = MediaPlayer.create(Textbox.this, R.raw.hero);
mp2.start();
}
public void onNothingSelected(AdapterView<?> parentView) {
}
});
(The code basically runs when a new item is selected of a spinner and then plays a song, which later will be a variable based on what was picked, but I'm fine as it is for now).
Problem: I want to be able to use 'mp2' out of this public void, (I want a button which pauses it). How can I do this?
Move the variable mp2 to the instance of the parent class. This will keep a running reference to it which you can interact with whenever you please. You will need to remove the final qualifier though if MediaPlayer.create(...) will be called more than once and stored.
edit:
I was referring to something along the lines of this:
class SomeClass {
private MediaPlayer mp2 = null;
private void whateverFunctionYouAreIn() {
hubSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
SomeClass.this.mp2 = MediaPlayer.create(Textbox.this, R.raw.hero);
SomeClass.this.mp2.start();
}
public void onNothingSelected(AdapterView<?> parentView) {}
});
//TODO: put this in an onClickListener:
if (this.mp2 != null) {
this.mp2.pause();
}
}
}
I'm not sure what happens when you call MediaPlayer.create(Textbox.this, R.raw.hero), but assuming it has no immediate effects, you can just create the object outside of the listener.
Edit1: OK, then how about this?
MediaPlayer currentPlayer;
methodA()
{
hubSpinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parentView,
View selectedItemView, int position, long id)
{
MediaPlayer mp2 = MediaPlayer.create(Textbox.this, R.raw.hero);
mp2.start();
setPlayer(mp2);
}
public void onNothingSelected(AdapterView<?> parentView) {
}
});
setMediaPlayer(MediaPlayer player)
{
currentPlayer = player;
}