How to update one spinner based on second spinner choice - android

I have to develop the application based on a spinner.
I have taken the spinner1 as the product choice and I have to give the spinner2 the data, which is in that particular product category so I have to update spinner2 on the base choice of spinner1.
I have search a lot but I can get any sure example code or resource for it.

I agree with #user639183, there are lot's of similar questions... however, there some explanation on how you do it:
Create member variables for the arrays containing your options to display.
Populate your first spinner with your category values.
Hook on to the Spinner's OnItemSelected event by using spinner1.setOnItemSelectedListener(...)
In the event listener, populate your second Spinner with the corresponding values.
Example for step 1:
private String[] spinner1values = new String[] { "cat1", "cat2" };
private String[][] spinner2values = new String[][] {
new String[] { "a1", "b1", "c1" },
new String[] { "a2", "b2" }
};
Population of spinner1 as follows:
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, spinner1values);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
This has so far been absolutely straight forward, if you read the documentation and examples for Spinners!
Next, hook on to the OnItemSelectedListener:
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// create a new adapter with the corresponding values
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(arg0.getContext(),
android.R.layout.simple_spinner_item, spinner2values[position]);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// set adapter
((Spinner)findViewById(R.id.spinner2)).setAdapter(adapter2);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// nothing selected, so set empty options
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(arg0.getContext(),
android.R.layout.simple_spinner_item, new String[0]);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
((Spinner)findViewById(R.id.spinner2)).setAdapter(adapter2);
}
});
Pay attention, that the order of the arrays in spinner2values is corresponding to the order of the category values!

Related

Sorting the values in Spinner depending on the condition

I want to make two spinners, the first spinner have the list of states and second spinner contains the list of cities . If i select the particular state from the first spinner then next spinner must show only the cities on the selected state only .
My android code
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner state = (Spinner) findViewById(R.id.spinnerState);
ArrayAdapter<CharSequence> stateadapter = ArrayAdapter.createFromResource(
this, R.array.item_state, android.R.layout.simple_spinner_item);
stateadapter.setDropDownViewResource(R.layout.spinner_layout);
state.setAdapter(stateadapter);
Spinner city = (Spinner) findViewById(R.id.spinnerCity);
ArrayAdapter<CharSequence> cityadapter = ArrayAdapter.createFromResource(
this, R.array.item_city, android.R.layout.simple_spinner_item);
cityadapter.setDropDownViewResource(R.layout.spinner_layout);
city.setAdapter(cityadapter);
}}
I have created all my for the state and cities.
There are lots of ways you can achieve this, for example:
ArrayAdapter<CharSequence> stateadapter;
switch(state)
{
case "Florida":
{
stateadapter = ArrayAdapter.createFromResource(this, R.array.cities_florida, android.R.layout.simple_spinner_item);
} break;
}
(kinda hardcoded)
The most optimal solution is to define it on an xml file (maybe you can get this somewhere on the internet) and write a class that reads the file and return all the cities on the selected state.
read:
https://developer.android.com/training/basics/network-ops/xml.html
Add a listener to the Spinner state
state.setOnItemSelectedListener(this);
Implement the listener:
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selectedState = state.getSelectedItem().toString();
List<String> citiesInState = new ArrayList<>();
// add all cities in selectedState to this list using citiesInState.add();
// this will depend upon how you are storing the cities and states
ArrayAdapter<String> cityDataAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_item, citiesInState);
cityDataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
city.setAdapter(cityDataAdapter);
}

How to Implements multiple spinner with different item list and different action on click in the same Activity

I want to implement two different spinner in Android, the spinner have different data set
This is the spinner with the age, that uses a defined String array with all age ranges (es 18-20, 19-21 etc.)
<Spinner
android:id="#+id/spAge"
android:layout_width="match_parent"
android:layout_height="35dp"
android:entries="#array/age_array"
tools:listitem="#android:layout/simple_spinner_item/>
And this is the spinner with the sex, that show only the two items Male and Female
<Spinner
android:id="#+id/spSex"
android:layout_width="match_parent"
android:layout_height="35dp"
android:entries="#array/sex_array"
tools:listitem="#android:layout/simple_spinner_item />
For each selected item the my activity should set the associated selected items values to the two Objects:
String selectedAge;
String selectedItem;
The sample that I have seen doesn't contains multiple spinner with different items set and different actions on item selected, and I don't know how to solve the problem.
write your Code as below to do different actions on item selected.
spinner1.setOnItemSelectedListener(this);
spinner2.setOnItemSelectedListener(this);
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
switch(arg0.getId()){
case R.id.spAge :
//Your Action Here.
break;
case R.id.spSex :
//Your Another Action Here.
break;
}
}
Try this
ArrayAdapter<CharSequence> adapterAge;
ArrayAdapter<CharSequence> adapterSex;
String[] AgeArr = {"18-20", "19-21"};
String[] sexArr = {"male", "female"};
Spinner ageDrp =(Spinner)findViewById(R.id.spAge);
Spinner sex1Drp =(Spinner)findViewById(R.id.spSex);
adapterAge = new ArrayAdapter<CharSequence>(this,android.R.layout.simple_spinner_item,AgeArr);
adapterAge.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ageDrp.setAdapter(adapterAge);
adapterSex= new ArrayAdapter<CharSequence>(this,android.R.layout.simple_spinner_item,sexArr);
adapterSex.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sexDrp.setAdapter(adapterSex);
String selectedAge = ageDrp.getSelectedItem().toString();
String selectedSex = sexDrp.getSelectedItem().toString();
System.out.println(selectedAge+" "+selectedSex);// check the output in logcat
Try This method
spinner1 = (Spinner) findViewById(R.id.spinner);
spinner2 = (Spinner) findViewById(R.id.highschoolspinner);
List<String> categories = new ArrayList<String>();
categories.add("Qualification");
categories.add("High School");
categories.add("Higher Secondary/PUC");
categories.add("Diploma");
categories.add("Degree");
categories.add("Master Degree");
List<String> list = new ArrayList<String>();
list.add("Plus One");
list.add("Plus Two");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.spinnertext, categories);
adapter.setDropDownViewResource(android.R.layout.select_dialog_item);
spinner.setAdapter(adapter);
ArrayAdapter<String> dataAdapter12 = new ArrayAdapter<String>(this,
R.layout.spinnertext, list);
dataAdapter12.setDropDownViewResource(android.R.layout.
simple_spinner_dropdown_item);
dataAdapter12.notifyDataSetChanged();
highschool.setAdapter(dataAdapter12)
Am used custom adapter to load data you can use built in adapter like
ArrayAdapter dataAdapter11 = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, list);

Sorting ListBiew by Spinner in android?

I have one ListView and one Spinner. The Spinner is situated at the right top of the layout. My ListView contains some names (eg. country names) and the spinner has some alphabets (a-z). Suppose if I choose the letter "f" from the Spinner, my ListView must show the country names that starts only with the letter "f" . I want to sort my ListView by values from the Spinner?
You need to implement a Filter for your ListView. Everytime the OnItemSelectedListener of your Spinner gets called you need to filter the items. If you're not sure how to implement a filter in your ListView's adapter have a look at this: Filtering ListView with custom (object) adapter
I guess there won't be any way around implementing an own ListAdapter (it's easy).
Try to use the getFilter() method of the ListView adapter:
String[] filterL = { "a", "b", "c" }; //etc
//...
Spinner spin = (Spinner) findViewById(R.id.spinner1);
final ArrayAdapter<String> aspin = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, filterL); //the adapter for the Spinner
aspin.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(aspin);
final ArrayAdapter<String> aa = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, countries); //the adapter for the list
setListAdapter(aa); //set the adapter for the list(if you extend LisActivity) or call setAdapter on the ListView element
//add the listener:
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
boolean status = false;
public void onItemSelected(AdapterView<?> parent, View view,
int position, long arg3) {
if (!status) {
status = true;
return;
}
aa.getFilter().filter(filterL[position]);
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});

how to get the information from first spinner to second spinner in android?

i have 2 spinners :
FIRST SPINNER :
array_spinner=new String[3];
array_spinner[0]="Color";
array_spinner[1]="Model";
array_spinner[2]="Price";
Spinner s = (Spinner) findViewById(R.id.select);
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item,
array_spinner);
s.setAdapter(adapter);
SECOND SPINNER :
array_color=new String[3];
array_color[0]="Black";
array_color[1]="Pink";
array_color[2]="Green";
array_model=new String[3];
array_model[0]="Ipod";
array_model[1]="mp3";
array_price=new String[3];
array_price[0]="1000-2000";
array_price[1]="3000-4000";
Spinner val=(Spinner)findViewById(R.id.select_val);
ArrayAdapter adapter1 = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, array_color);
val.setAdapter(adapter1);
When the user clicks Model in the First Spinner , immediately the Second Spinner should display the models which are in the array_model ... Please Help !!!!
use setOnItemSelectedListener for the first spinner. In the onItemSelected() set a new arrayAdapter for the second spinner with the new data set
I think you must put a listener on the spinner.
Then You create the good second spinner.
example :
s1.setOnItemSelectedListener(
new OnItemSelectedListener() {
public void onItemSelected(
AdapterView<?> parent, View view, int position, long id) {
if(array_spinner[int]=="model"){
//Do your second spinner
}
}
public void onNothingSelected(AdapterView<?> parent) {
}
});

Create spinner without <Spinner in xml

I was using a spinner in a TableRow and it was working perfectly well except that I didn't like spinner icon is stretching out according to the selected item. I tried to delete Spinner section in xml and create it on my code.
To create a spinner in OnCreate():
selectArea = /*(Spinner)this.findViewById(R.id.spinner);*/new Spinner(this);
String[] ss = getResources().getStringArray(R.array.countries);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ss);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
selectArea.setOnItemSelectedListener(new MyOnItemSelectedListener());
selectArea.setSelection(prefInt);
To handle a selection event:
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Log.d(TAG, "onItemSelected() " + id);
}
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing.
}
}
It worked almost perfect. TableRow show no spinner icon, on touching TableRow, it pops up items to be selected. My only problem is that, on selecting one tiem, I never get my onItemSelected() called..
What could be wrong?
please change this line
spinner.setAdapter(adapter);
into the line
selectedArea.setAdapter(adapter);
see i have created spinner here
Spinner selectArea= new Spinner(Activity.this);
String[] ss = getResources().getStringArray(R.array.countries);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ss);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selectArea.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selectArea.setAdapter(adapter);
linearLayout1.addView(selectArea);//to add your spinner
selectArea.setOnItemSelectedListener(new MyOnItemSelectedListener());

Categories

Resources