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

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.

Related

I have this json response.... i want to add it to my android spinner? How i add this

{"state":[{"state_id":"101","state_name":"Haryana"},{"state_id":"102","state_name":"Punjab"}]}
this is my json response.... i want to add it to my spinner in android.
Create a list and add all the values you need to show in your spinner
List<String> list = new ArrayList<String>();
List<String> listID = new ArrayList<String>();
list.add("state name");
list.add("state id");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Create an adapter and set the adapter to your spinner.
spinner.setAdapter(dataAdapter);
now get the spinner selected item position
int position= spinner.getSelectedItemPosition();
with this position value you can get state code in the arraylist

Spinner that trigger other spinner

I want to create an application with two Spinners. When you select one of the items inside the first spinner, the second spinner will get only a few items (depends on which item selected).
For example: in the first spinner I select "Mazda", and then on the second I will be able to see only Mazda's models, not BMW, Ford etc. Can I do something like this?
I tried to create a spinner without items, and set the entries of the spinner on the XML when item selected, but there is no method to do this.
I don't create Lists. I want to create string-array resources in my strings.xml, and give that array to the second spinner.
Of course it is doable and pretty straightforward. Spinners works with the model provider, which in Android it is called Adapter pattern. So what you can do is put in your first spinner an adapter that holds all the brands of your cars, and listen on the first spinner for changes using setOnItemSelectedListener.
When the item changes, you can create a new adapter instance for the second spinner with the only values that are valid in this case.
Try following code. I've organized the sample data in a HashMap but you can do it in your own way.
// hashmap object containing data of spinner1 as 'keys' with relevant
// data of spinner2 in List<String> object as 'values'
final Map<String, List<String>> data = new HashMap<>();
data.put("A", Arrays.asList("1","2","3","4"));
data.put("B", Arrays.asList("4", "5"));
data.put("C", Arrays.asList("6", "7", "8", "9"));
data.put("D", Arrays.asList("10", "11", "12"));
data.put("E", Arrays.asList("13", "14"));
// obtaining a string array containing keys(data of spinner1) of above hashmap
final String[] dataSpinner1 = new String[data.keySet().size()];
data.keySet().toArray(dataSpinner1);
// initializing an string type, ArrayAdapter for spinner1
// you will need to pass activity context, layout for the spinner item and
// spinner content(as string array) as arguments to create an array adapter
final ArrayAdapter<String> spinner1Adapter = new ArrayAdapter<String>(context, R.layout.spinner_layout, dataSpinner1);
spinner1.setAdapter(spinner1Adapter);
// setting listner for spinner1 to trigger when an spinner item is being
// clicked by the user
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// obtaining relevant data for spinner2
List<String> dataSpinner2 = data.get(dataSpinner1[position]);
// crating an setting array adapter for spinner2
ArrayAdapter<String> spinner2Adapter = new ArrayAdapter<String>(context, R.layout.spinner_layout, dataSpinner2);
spinner2.setAdapter(spinner2Adapter);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});

how to bind values to single spinner from one web service, another default value in android

String[] s={"1","2","3","4"};
ArrayAdapter<String> adp=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_dropdown_item,s);
spin.setAdapter(adp);
Now I want to get "4" at zero position of Spinner.
AFAIK, you can't change the position of the items inside a Spinner after you have set its Adapter.
If you want 4 to be the first item, you have to order your list accordingly:
String[] s={"4","1","2","3"};
ArrayAdapter<String> adp = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, s);
spin.setAdapter(adp);

What type of Object does Spinner.getItemAtPosition(...) return?

What will this statement return?
parent.getItemAtPosition(position)
Where parent is a parent view for a spinner and position is the selected position from the spinner view.
I assume the "parent" you are talking about is a Spinner. In this case:
Spinner.getItemAtPosition(pos);
will always return the type of object that you filled the Spinner with.
An example using a CustomType: (the Spinner is filled with Items of type "CustomType", therefore getItemAtPosition(...) will return CustomType)
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
CustomType [] customArray = new CustomType[] { .... your custom items here .... };
// fill an arrayadapter and set it to the spinner
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, customArray);
spinner.setAdapter(adapter);
CustomType type = (CustomType) spinner.getItemAtPosition(0); // it will return your CustomType so you can safely cast to it
Another example using a String Array: (the Spinner is filled with Items of type "String", therefore getItemAtPosition(...) will return String)
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
String[] stringArray= new String[] { "A", "B", "C" };
// fill an arrayadapter and set it to the spinner
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, stringArray);
spinner.setAdapter(adapter);
String item = (String ) spinner.getItemAtPosition(0); // it will return your String so you can safely cast to it
It will return object of dataType you are displaying in spinner.
suppose you are displaying String array then it will return String.
if you are displaying Integer array then it will return Integer and so on.

How do I set spinner value to string

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);

Categories

Resources