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);
Related
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) {
}
});
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?
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) {
}
});
I want to show the value of spinner in string instead of integer. Right now it is showing integer like '0','1'.
Setting activity:
public class Setting extends Activity {
bloodtype = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,getResources().getStringArray(R.array.blood_groups_arrays));
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
bloodtype.setAdapter(adapter2);
bloodtype.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
/**
* Called when a new item is selected (in the Spinner)
*/
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
sharedpreferences.edit().putString(BloodType, parent.getItemAtPosition(position).toString()).commit();
}
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing.
}
});
if(sharedpreferences.contains(BloodType)) {
bloodtype.setSelection(sharedpreferences.getString(BloodType, "DEFAULT"));
}
}
Calling its value in main activity:
if(sharedpreferences.contains(Setting.BloodType)) {
show.setText(""+sharedpreferences.getString(Setting.BloodType, "DEFAULT"));
}
In order to store a value of array you populated your spinner with in SharedPreferences use the following line in onItemSelected() method:
sharedpreferences.edit().putString(BloodType, parent.getItemAtPosition(position).toString()).commit();
Note: BloodType has to be of String type, because it plays a role of a key in key-value pair of SharedPreferences.
Taken from my working project it should work:
adapterSpinner = new ArrayAdapter<String>(yourContenxt,
android.R.layout.simple_list_item_1, yourDataSource);
spinnerProfileType.setAdapter(adapterSpinner);
As you are using a string array you can set the spinner items in xml too by using:
android:entries = "#array/your_string_entry"
I am trying to get a Spinner to work in Android. It displays fine and I can select any one of the options in the list. But how do I transfer that to a string?
I would have thought in the code below that 'selected' would hold the selected string, but I get an 'Illegal modifier for the local class YourItemSelectedListener; only abstract or final is permitted' error on the 'YourItemSelectedListener'.
What am I doing wrong?
Many thanks for any help.
Spinner spinnerFPS = (Spinner) findViewById(R.id.sp_FPS);
ArrayAdapter adapter = ArrayAdapter.createFromResource(
this, R.array.framesps, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerFPS.setAdapter(adapter);
spinnerFPS.setOnItemSelectedListener(new YourItemSelectedListener());
public class YourItemSelectedListener 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.
}
}
ArrayAdapter adapter = ArrayAdapter.createFromResource(
this, items, android.R.layout.simple_spinner_item);
You will have to add the CurrentActivityName.this. This will fix the issue. You just can't pass the argument context as this. You will have to put ActivityName.this.
Since you are using an array resource for spinner create a resource handle
with local array declaration with getResources().getStringArray(R.array.framesps);
and then use that handle to access the selected item using position variable:
items[pos]
Heres a code edit:
Spinner spinnerFPS = (Spinner) findViewById(R.id.sp_FPS);
String[] items=getResources().getStringArray(R.array.framesps);//handle to your arrays
ArrayAdapter adapter = ArrayAdapter.createFromResource(
this, items, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerFPS.setAdapter(adapter);
spinnerFPS.setOnItemSelectedListener(new YourItemSelectedListener());
public class YourItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
String selected =items[pos]; // use handler to access select item
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}