I am newbie i want to remove the selected item from the spinner and also add the new item to the spinner.How can i do that.What i am trying is
adapter = ArrayAdapter.createFromResource(this,R.array.slot , android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
slotTime.setAdapter(adapter);
slotTime.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
selectedTime = adapterView.getSelectedItem().toString();
adapter.remove((String) slotTime.getSelectedItem());
adapter.notifyDataSetChanged();
}
I got the error like this....
java.lang.UnsupportedOperationException
Anyone kindly help me to overcome this problem
When you pass an array in the ArrayAdapter class it converts the array into an AbstractList. This is an implementation of the List interface where elements cannot be added nor removed, i.e, immutable. What you should do is pass in an ArrayList by converting the array to a list. Arrays.asList method should do the trick.
your data is immutable (R.array.slot)
So you can modify it
You need to store data in List<String>, pass it to ArrayAdapter. Now you can modify data by modify List<String>
used below code for remove spinner value and notify.
here in strings as List.
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
strings.remove(arrstrings.indexOf(spinner.getSelectedItem()));
// spinner.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
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?
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 have some data coming from server and i have to show that value in Android Spinner.
Adapter that is attach to Spinner also getiing from server.
please let me know how to show initial value coming from server.and show adapter value after click on Spinner.
Spinner mySpinner = (Spinner) findViewById(R.id.householdspinner);
mySpinner.setAdapter(new ArrayAdapter<String>(Edit_Voter_Information.this,
android.R.layout.simple_spinner_dropdown_item,
householdIncome));
mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0,
View arg1, int position, long arg3) {
String item = arg0.getItemAtPosition(position).toString();
house_Hold_Income = item;
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
Initially i stored some values in String[] names. when activity is run spinner position 0(zero)is selected by default.
Check my code below:
ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.spinner_style, names);
spinner.setAdapter(arrayAdapter);
spinner.getSelectedItemPosition();//which returns names[0] initially.
if click other position(i) it returns names[i], where i =0,1,2,..
I have an ArrayList of custom object. This custom object contains zoneCode(Integer) and zoneName(String), I want to set this data in an Spinner so that the zoneName comes in the list and onItemSelected, I can get the zoneCode of corresponding selection. How it is possible?
try zoneCod.get(position)
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int
position, long id) {
zoneCod.get(position);
}
}
As the data of both zoneName and zoneCode are in array list and you know how to get the position of selected item. if yes, then you can get the zoneCode as following:
zoneCode[position];
override toString method in your custom object class and make it return zoneName
class CustomObject{
public String zoneName;
public int zoneCode;
public String toString(){
return zoneName;
}
}
setting up the spinner
CustomObject[] objects = new CustomObject[10];
//initialize each object
ArrayAdapter<CustomObject> adapter = new ArrayAdapter<CustomObject>(this,android.R.layout.simple_spinner_item, objects);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
i have spinner with array that i create in a string.xml which called array.spinner_title
how can i do the on item select her so i can get the item selected
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( SendTeacher.this,
R.array.spinner_title, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
// Do something
}
#Override
public void onNothingSelected(AdapterView<?> adapter) {
}
});
thanks
As I understand your question , you want to get the selected item.
In:
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
CharSequence item = (CharSequence)adapterView.getAdapter().getItem(position);
}
For custom adapter which can contain any other objects (not only CharSequence), see this link for example