Set position of spinner from variable - android

I have an activity that recieves two variables called l_source and l_destination and I want to set the default values of two spinners equal to those variables. To do so, I have made a search and I understand that I need to get the position of the value of the variable (will give a number) and then set the new position of the spinner. The problem is that I am trying to use getAdapterPosition and I am sent Cannot resolve method getAdapterPosition(l_source).
EDIT
I finally managed to do it, but it is only working in one spinner. Have you got any idea of why? Here is my code:
Spinner spinner = (Spinner) findViewById(R.id.lista_origen);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(Main2Activity.this,
R.array.l_source, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setSelection(adapter.getPosition(l_source));
Spinner spinner2 = (Spinner) findViewById(R.id.lista_destino);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(Main2Activity.this,
R.array.l_source, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
spinner2.setSelection(adapter2.getPosition(l_target));
Thank you for your time.

get position of selected item is spinner by int spinnerPosition = spinner.getSelectedItemPosition();

Related

One spinner showing correctly a default value and the other not

I have got two spinners that receive a default value as a variable from another view. The first one is working fine but the second one not (it shows position 0 as the default value). Here is my code:
Spinner spinner = (Spinner) findViewById(R.id.lista_origen);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(Main2Activity.this,
R.array.l_source, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setSelection(adapter.getPosition(l_source));
Spinner spinner2 = (Spinner) findViewById(R.id.lista_destino);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(Main2Activity.this,
R.array.l_source, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
spinner2.setSelection(adapter2.getPosition(l_target));
l_target and l_source are the variables and they are correctly sent from the previous view to this one.
I would be grateful if you could find what and why is failing.
Thank you for your time.

How to programmatically change spinner item name at a given position android

I would like to programmatically change a name of one of my spinner item but I am not sure how I can do this.
For example given the code below
Spinner spinner = (Spinner) MenuItemCompat.getActionView(item);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.spinner_list_item_array,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
I would like to
Change spinner ITEM name at position 1 to "NEW TEXT"
You should change the data in R.array.spinner_list_item_array.
After you have changed the data you will need to then call adapter.notifyDataSetChanged() and then adapter.setAdapter(adapter).
CharSequence[] fiilliste = getResources().getStringArray(R.array.spinner_list_item_array);
ArrayAdapter<CharSequence> adapter =
new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_dropdown_item, fiilliste);
spinner.setAdapter(adapter);
fiilliste[1] = "NEW TEXT";
adapter.notifyDataSetChanged();

Android set spinner position

I'm trying to set my spinner position on activity entry but i get this msg on logcat
Attempt to invoke virtual method 'void android.widget.Spinner.setSelection(int)' on a null object reference
I have and array delcared as:
<string-array name="genres">
<item>Action</item>
<item>Animation</item>
</string-array>
in the activity i have
movieGenre = (Spinner) findViewById(R.id.dd_genre);
Log.e("ListView set Item", String.valueOf(movieItem.getaNGenre()));
movieGenre.setSelection(movieItem.getaNGenre());
logcat shows me the number correctly:
05-09 12:46:35.624 4940-4940/simpleapps.movierandomizer E/ListView set Itemīš• 3
but when i set the position, it wont accept the number. I dont understand why it says "on a null object reference" when i have declared it.
Did you populate the spinner with the items (probably the string array in this case?)
Also, if the item number (3 in this case) that you are trying to set,is more than the spinner items, it will throw a run time error.
You have to set the adapter for the spinner first. After setting up the adapter it won't throw the exception that you have mentioned.
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.planets_array, 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);

Android programming spinner swap elements of spinner

need to do so when you click on a specific button (application type translator and arrow key) changed one of the selected items to another spinner itam swapped Well, here is how it can be implemented?
here is the code but it does not work!
int spinner1Index = spinner.getSelectedItemPosition();
spinner.setSelection(spinner2.getSelectedItemPosition());
spinner2.setSelection(spinnerfirst);
can offer something similar or fix!
You would have to change the adapter of your spinner and call notifyDataSetChanged()
I would suggest adding this to your switch case for when the item is selected.
Set a custom adapter for the spinner on item click.
//An array of what you want to populate the spinner with
Array spinnerArray [] = {"One", "Two", "Etc.."};
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, spinnerArray);
spinnerArrayAdapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
//Before setting the adapter, clear and notify the previous ArrayList used
arrayList.clear();
arrayList.notify();
spinner.setAdapter(spinnerArrayAdapter);

Android application crashes when populating spinner

I'm trying to populate the spinner, but the application crashes on spiner.setAdapter(adapter)...
final Context c=this;
ArrayList<CountryItem> countriesArray = GetCountries1();
ArrayAdapter<CountryItem> adapter = new ArrayAdapter<CountryItem>(
c,android.R.layout.simple_spinner_item, countriesArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
final Spinner spiner=(Spinner)findViewById(R.id.spinner1);
spiner.setAdapter(adapter);
I can't find any errors here, and eclipse debug mode does not show anything usefull...
Here is the example from google...is it so much different than my code?
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
What error do you get?
Did you check if the countriesArray has elements or if it is empty?
Is it working when you use, e.g., a simple string array? If yes, then the problem lies with the CountryItem objects.
Other than that, the only difference I see is that you pass the context as a final variable, but I don't know if this causes the error. Try to pass just this and see if it works.

Categories

Resources