'How to set the spinner position to selected item from choice selected in different fragment
this is my first time asking a question and I am a new coder and been trying out android with kotlin and java. I have an app that allows a user to add an item of clothing the options are set up with spinners. The issue is when the user selects an item from the spinner it saves and when I want to update the values like the image shows the blue words saying brown is the option selected form the add clothing item spinner but the spinner is showing grey is there a way I can set the spinner to be on brown and not the default value.
Solution 1:[1]
Yes. You need to use the getItemAtPosition() method. From your user interface, the Object you are trying to retrieve is a String. So if you wrote your code in Java. Knowing you've used a TextView to display the selection
Java
TextView clotheColor = findViewById(R.id.whatever_the_id_is);
String selectedColor = "";
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedColor = parent.getItemAtPosition(position).toString();
Log.i(TAG, selectedColor);
switch (selectedSpinnerItem) {
case "Brown":
case "Red":
case "Blue":
clotheColor.SetText(selectedColor);
break;
default:
Log.i(TAG, "");
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
Log.i(TAG, "Nothing has been selected");
}
});
KOTLIN
You can convert the code to Kotlin from here. Read this. How can I convert a part of Java source file to Kotlin?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Damola Obaleke |
