How do I set spinner value to string - android

I have a spinner set up like this:
ArrayAdapter<String> states = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, getResources().getStringArray(R.array.stateabbrev));
states.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
state.setAdapter(states);
As you can see, the source is an array.xml file.
I want to know how to populate it if I know the array value. For instance, I am retrieving information from my database and the user is from "KY" so I have a string "KY" and I want the spinner selection to be on "KY"

at first we should get position of "KY"
int position = states.getPosition("KY");
after that, select in spinner with position
state.setSelection(position);

Related

How to set a string value in Spinner view..?

In spinner view values at andoid(i.e..one,two,three),now i selected two,then i change selected value two as string.after i will asign two(string value) as set to one spinner.
In order to have a spinner, follow this steps
Declare Spinner number
and String Number as global
In onCreate()
number = (Spinner) findViewById(R.id.yourSpinnerId);
addItemsOnSpinner()
Then write addItemsOnSpinner()
public void addItemsOnSpinner() {
List<String> list = new ArrayList<String>();
list.add("one");
list.add("two");
list.add("three");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
number.setAdapter(adapter);
}
How to set a string value in Spinner view..?
If I understand your question correctly, you want to set the selected value (2) as a string right ? You can write in this way
Number=number.getSelectedItem().toString()
The Number will now holds the spinner item, number as a string.

To set the positions of items in spinner

I am populating my spinner from database. I have a collection of mobile brands. I have added "Add a new brand" also... But when I am setting the spinner items from DB, it comes in somewhere middle.. I want it to go at the end.. Can i do it? if yes, how? please help, thanks in advance.
Spinner brand;
brand=(Spinner)findViewById(R.id.spinner_brand);
private void loadSpinnerData() {
// database handler
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
// Spinner Drop down elements
List<String> Brand = db.getBrands();
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, Brand);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
brand.setAdapter(dataAdapter);
}
Add time column in your brand table and on querying sort by time. So, you will get last added item at last in spinner.
Create an ArrayList and add all your database items to it. and then add your "Choose a brand" string to it. and then pass the ArrayList to Spinner.
Although I should warn you, you are "probably" doing it wrong. If you add "Choose a brand" item to spinner, it will also be selectable, which you might not want :)

how to update spinner dynamical and populate it with resourse String Array

In my application there are two spinners. The content of second spinner depends upon first spinner. So, I want to fill the second one from resource R.array.
if(state.getSelectedItemPosition()==2)
{
adapter = new ArrayAdapter<String> (MainActivity.this,android.R.layout.simple_spinner_dropdown_item,R.array.Panjab);
city.setAdapter(adapter);
}

Get and Set values form spinner

I'm looking for to get and set values form spinner loades from database but i don't find it.
private void loadSpinnerData() {
// database handler
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
// Spinner Drop down elements
List<String> lables = db.getAllCadenesAsList();
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, lables);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
}
The cursor returns _id and Name and the spinner load correctly (it seems..)
Now when I try to get de selected valued in onItemSelected, I the id and the position is the same, and is not a id of cursor, it is the posicion in de spinner.
How can I get de ID of selected value?
How can I set the value of spinner.
I need and I know the id of database, not the id/position of spinner.
Thanks in advance.
You're not getting a real database _id on onItemSelected of the spinner because you hooked the spinner up to an ArrayAdapter. This sort of adapter hookup is not in any way connected to your database but only the List<String> or other collection you passed into it. To get what you want, you need to set the spinner's adapter to a CursorAdapter instead.

how to set checkbox and spinner with repective values in android

I am trying to manipulating checkbox and with respective spinner and sending value to next page.
Here, If first check asia check box and choose on of the country from the first called asia spinner. same as check europe also checked and choose one of the country from the europe spinner.
If i click setdetails button, then go to next page and set all deatils of both checked country. and filly save all countries deatils on database.
Here some sample pictures, that display my problem.
First, you provide spinner with an adapter to fill it's values which looks like this:
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
To then get checked value you write:
String selectedParent = parentSpinner.getSelectedItem().toString();
To set the value you do this:
parentSpinner.setSelection(i);
where i is an index in the list you provided (i.e. 0 means "first element in the list").
To pass those values to the next activity you use set extras to Intent:
Intent i = new Intent(TaskViewActivity.this, TaskViewActivity.class);
i.putExtra("status", status);
TaskViewActivity.this.startActivityForResult(i, 0);
To fetch them from the activity you called you do this:
Bundle extras = getIntent().getExtras();
task_id = extras.getLong("status");
I hope that helps!

Categories

Resources