I have a need to add 3 related spinners where the selected content in the first affects what is displayed in the second and then what is selected in the second affects the content in the third. Has anyone already faced this problem and how did you over come it?
I was thinking of maintaining a number of arrays in the resources which are numbered and then loaded into the adapter based on the choice selected in the previous spinner. Is this a good way?
Thanks,
m
I would have done this with something like that:
spinner1.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
{
SpinnerAdapter adapter = ...create or load the second adapter based on selected item...
spinner2.setAdapter (adapter);
spinner3.setAdapter (..create empty adapter...);
}
#Override
public void onNothingSelected(AdapterView<?> parentView)
{
spinner2.setAdapter (..create empty adapter...);
spinner3.setAdapter (..create empty adapter...);
}
});
spinner2.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
{
SpinnerAdapter adapter = ...create or load the third adapter based on selected item...
spinner3.setAdapter (adapter);
}
#Override
public void onNothingSelected(AdapterView<?> parentView)
{
spinner3.setAdapter (..create empty adapter...);
}
});
Related
RecyclerView not saving Spinner values.
I have tried available examples all on stackoverflow, I could not find a working solution.
Please find my code of my RecyclerView Below:
ArrayAdapter<CharSequence> countryAdapter = ArrayAdapter.createFromResource(context, R.array.spinnerlist, android.R.layout.simple_spinner_item);
countryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
valueselected.setAdapter(countryAdapter);
valueselected.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
valueselected.setSelection(i, true);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
valueselected.setAdapter(countryAdapter);
valueselected.getSelectedItemPosition();
Once scrolled, the selected value of the spinner needs to remain.
i have to select items from spinner. Actually i am making gpa calculator.I have given grades in spinner and person can select from that. I have to assign grades with like A will 4, B+ will be 3.5 and so on. That's why i have to extract value after user has selected from spinner. But i am getting that spinner cannot be applied to adapter view.
setonitemclicklistener (android.widget.AdapterView.onitemclicklistener) in spinner cannot be applied to (anonymous android.widget.AdapterView.onitemselectedlistener)
you can do :
Spinner spinner = new Spinner(getApplicationContext());
spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
android.util.Log.d(TAG, "position: " + position);
}
});
hope this will help you.
You should use setOnItemSelectedListenerto do something on an item selected like below:
yourSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Do Something
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
I am trying to retrieve the spinner's selected position. The values inside the spinner are of type String and I am using setOnItemSelectedListener to change a class String variable to the one that has been selected and use it later on:
spinner = (MaterialBetterSpinner) myToolbar.findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,socialMediaOptions);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
socialMedia = parent.getItemAtPosition(position).toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
private void sendPost(){
Log.v(TAG, socialMedia);
}
The problem is that the variable does not change when an item is selected, it seems like onItemSelected is not being called when there is a change in the spinner selection
See this thread:
https://github.com/Lesilva/BetterSpinner/issues/42
There said that BetterSpinner extends EditText, that's why onItemSelectedListener is not being called. You should use TextChangedListener instead.
Hope your socialMediaOptions are string array like this
socialMediaOptions={"FB","TWIT","IN"}
then
// Specify the layout to use when the list of choices appears
add this line
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
socialMedia = socialMediaOptions[position];
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
What is wrong with this code?
I'm having two spinners. One for 'category' and the second for 'subcategory'. When an item is chosen in the category spinner I want to re-load the relevant sub-categories accordingly.
mSpnrCategory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// SubCategories
String[] aSubCategory = new String[SUBCATS.length];
... Some code for preparing aSubCategory.
ArrayAdapter<String> dataAdapterSubCategories = new ArrayAdapter<String>(NewRequestActivity.this, android.R.layout.simple_spinner_item, aSubCategory);
dataAdapterSubCategories.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpnrSubCategory.setAdapter(dataAdapterSubCategories);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
when the activity is loaded this code is called and it works fine loading the subcategories of the first category. However, when I change category manually this code is called but results with empty spinner for subcategories. I can't figure out why. Any ideas?
Define your subcategory adapter (mSpnrSubCategory) as a global variable.
Then whenever category spinner click, change the data of subcategory then notifyDataSetChanged();
ArrayAdapter<String> mSpnrSubCategory;
ArrayList<String> aSubCategory;
mSpnrCategory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// SubCategories
...
mSpnrSubCategory.notifyDataSetChanged();
}
});
I suggest to use ArrayList<String> instead of String[]
Hope this help
I've tried this a few different ways and all the code does is print out the top value of the spinner.
final String [] equipment=new String[]{//items
}
ArrayAdapter<?> ad=new ArrayAdapter<Object>(this,android.R.layout.simple_spinner_item,equipment);
ad.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner spin=(Spinner)findViewById(R.id.spinneritem);
spin.setAdapter(ad);
int position = spin.getSelectedItemPosition();
final String equipmentitem = equipment[position].toString();
From what I can tell that should be able to take the value of the array that the user has selected, but no matter what I select in the list, it only select the first item, and ignores the actual selection.
You probably need to add a listener to the spinner.
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView,
View selectedItemView, int position, long id) {
equipmentitem = equipment[position].toString();
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
}
});