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
Related
i create in a two way data binding spinner in Android, and added value into with a result of a REST GET.
I need now to add the first value of the spinner as an empty string that when it will be selected from the spinner, the value will be null.
In the fragment class i wrote this code:
//spinner
appCompatSpinner = binding.spinner;
spinnerAdapter = new SpinnerAdapter(getActivity(), viewModel.usersDetailsList );
appCompatSpinner.setAdapter(spinnerActorAdapter);
appCompatSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
viewModel.onSelected(position);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
mAdapter.notifyDataSetChanged();
Can someone please help me to achieve that?
When I click a spinner item,I would like list view to shows more details on that item.
i.e if I select Student from Spinner then list view should show student names.
spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(SpinnerActivity.this, parent.getItemAtPosition(position)+" ", Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
What is the right way to do it ?
Create an ArrayList with Strings(Student names). Now create an ArrayAdapter in onCreate() which contains the Students ArrayList.
In the EventListener(onItemSelected) of the Spinner, check for the Spinner-item text. If it's "Students" set the ArrayAdapter of the Spinner to the Students-ArrayAdapter which you initialized in onCreate();
onCreate():
arrayList = new ArrayList<String>();
arrayList.add("Bob");
adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, arrayList);
onItemSelected EventListener:
if(view.getText().toString().equals("Students"){
listView.setAdapter(adapter);
}
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) {
}
});
I wanted to display my data into android spinner from database.
I used two spinner..
2nd spinner should reflect once 1st spinner item is selected, every thing is working fine..data is loaded into the second spinner, but not displaying into 2nd spinner when 2nd spinner item is selected.
Spinner1 = (Spinner)findViewById(R.id.createProfileCitySpinnerId);
Spinner2 = (Spinner)findViewById(R.id.createProfileStateSpinnerId);
//for 1st spinner.....(working)
final List<String> list1 = new ArrayList<String>();
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,list1);
Spinner1.setAdapter(adapter1);
//for second spinner...
final List<String> list2 = new ArrayList<String>();
Spinner1.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
if(//some condition using id)
{
list2.add(stateCursor.getString(1));
}
}
#Override
public void onNothingSelected(AdapterView<?> parent)
{
}
});
//everything is working data is loading, but not display once item is selected on 2nd spinner
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,list2);
stateSpinner.setAdapter(adapter2);
You need to add the element directly to the adapter (not to list2), the ArrayAdapter keeps its own internal data. Try this code on your onItemSelected:
adapter2.add(stateCursor.getString(1));
adapter2.notifyDataSetChanged();
Call adapter2.notifyDataSetChanged();
such as
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
if(//some condition using id)
{
list2.add(stateCursor.getString(1));
adapter2.notifyDataSetChanged();
}
}
see http://developer.android.com/reference/android/widget/ArrayAdapter.html for more information.
public void notifyDataSetChanged () Since: API Level 1
Notifies the attached observers that the underlying data has been
changed and any View reflecting the data set should refresh itself.
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...);
}
});