I have 2 spinners in an activity.
Based on the selection of one item in spinner1, relevant data should be loaded in spinner2. Consider spinner1 has data related to country and spinner2 has data related to state.
I should be able to get this done once the activity is created and if the user changes the selection in spinner1.
But I am stuck with populating the spinner2 data based on the saved value of spinner1.
I am calling spinner1.setSelection(indexSaved) but since I am only loading spinner2 in the onItemSelectedOf of spinner1, setSelection of spinner1 is not firing the onItemSelected.
Please let me know how I can achieve this.
I have done same thing in my project without any problem:
Below is code may be it help you:
On Create Method filled state list:
//initialize spinners
spinnerStateList = (Spinner) findViewById(R.id.spinnerStateList);
spinnerDistrictsList = (Spinner) findViewById(R.id.spinnerDistrictsList);
fillStateList();
public void fillStateList()
{
states = manage_states.fetchAllStates(getApplicationContext());
//add new element for select name
states.add(0,new RowItem("-1", "Select State"));
String[] spinnerArray = new String[states.size()];
for(int i= 0;i<states.size();i++)
{
spinnerArray[i] = states.get(i).getName();
}
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,spinnerArray);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerStateList.setAdapter(dataAdapter);
spinnerStateList.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0,
View arg1, int position, long arg3) {
String selectedStateID = states.get(position).getId();
fillDistrictsSelectionChangeState(selectedStateID);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
public void fillDistrictsSelectionChangeState(String selectedStateID)
{
if(selectedStateID.equalsIgnoreCase("-1"))
{
linearLayoutDistrictsList.setVisibility(LinearLayout.GONE);
linearLayoutBlocksList.setVisibility(LinearLayout.GONE);
linearLayoutPHCList.setVisibility(LinearLayout.GONE);
linearLayoutSHCList.setVisibility(LinearLayout.GONE);
}
else
{
linearLayoutDistrictsList.setVisibility(LinearLayout.VISIBLE);
districts = manage_districts.fetchAllDistrictsByStateId(getApplicationContext(), selectedStateID);
//add new element for select name
districts.add(0,new RowItem("-1", "Select District"));
String[] spinnerArray = new String[districts.size()];
for(int i= 0;i<districts.size();i++)
{
spinnerArray[i] = districts.get(i).getName();
}
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,spinnerArray);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerDistrictsList.setAdapter(dataAdapter);
spinnerDistrictsList.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0,
View arg1, int position, long arg3) {
String selectedDistrictID = districts.get(position).getId();
fillBlocksSelectionChangeDistrict(selectedDistrictID);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
}
Related
I want to implement one code for three spinner where the user will fill there date of birth at register time when he/she will login and want to update there DOB, the DOB will pass on the spinner I have done it with day and month but there is one problem in year i.e adapter. adapter takes the (thisYear-18) year and print it but I want spinYear will take that value which I stored at registered time.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_details_fragment);
Spinner spinDay = (Spinner) findViewById(R.id.spinnerDay);
Spinner spinMonth = (Spinner) findViewById(R.id.spinnerMonth);
Spinner spinYear = (Spinner) findViewById(R.id.spinnerYear);
Button update = (Button)findViewById(R.id.update);
Intent intent =getIntent();
String str_DOB = intent.getStringExtra("dateofbirth");
Log.e("DOB************",str_DOB);
String[] d = str_DOB.split("-");
Log.e("d[0]-------",d[0].toString());
spinYear.setSelection (Integer.parseInt(d[0]));
spinMonth.setSelection (Integer.parseInt(d[1]));
spinDay.setSelection (Integer.parseInt(d[2]));
code for Year Adapter
ArrayList<String> years = new ArrayList<String>();
int thisYear = Calendar.getInstance().get(Calendar.YEAR);
thisYear=thisYear-18;
for (int i = thisYear; i >= 1967; i--) {
years.add(Integer.toString(i));
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, years);
spinYear.setAdapter(adapter);
spinYear.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long l) {
final String yearName = adapterView.getItemAtPosition(pos).toString().trim();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
If I understand you correctly you want to set the selection of the year spinner based on your passed in Intent.
setSelection(int position) Takes the index of the item in the spinner you want to select.
After you call setAdapter do as so:
spinYear.setAdapter(adapter);
int index = years.indexOf(d[0]);
if (index != -1) {
spinYear.setSelection(index);
}
you should check the year in onItemSelected() method because this method get the position 0 and set it first.try to use below code:-
spinYear.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long l) {
if((d[2]).equals(adapterView.getItemAtPosition(pos).toString()))
{
final String yearName = adapterView.getItemAtPosition(pos).toString().trim();
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
I am new in android ,I want work like, In Spinner if i select any other items form array,that item should get selected including with some text,For example,if My spinner having 10 items,if i select 6th item that item should display like "Your Item Is: Item6",This should be display in my spinner as first item,After select of items form the array,Please help me ,,Thanks in advance..
String[] categories = {"Select one","Automobile","Computers","Education"};
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
then onItemSelected Listerner Do :-
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
categories[0] = item;
ArrayAdapter<String> newdataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
spinner.setAdapter(newdataAdapter);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
ArrayAdapter arrayAdapter = null;
String[] list=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//you can replace the below list with your list
list = {"Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune","Pluto"};
final Spinner spinner = (Spinner) findViewById(R.id.spinner);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner.setAdapter(arrayAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
spinner.setPrompt("Your selected item is:"+list[position]);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
I am trying to get the spinner to display the item which i have selected. But it is only displaying the first word even if i choose the ones below. Here is the code i am using
ArrayAdapter<String> aa1 = new ArrayAdapter<String>(
getApplicationContext(), R.layout.spinner_item, R.id.textView1, al);
spFacilityType.setAdapter(aa1);
spFacilityType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
int index = arg0.getSelectedItemPosition();
position = index;
}
});
final String Strspinner = spFacility.getItemAtPosition(position).toString();
Use onItemSelected instead of onNothingSelected.
do the code in onItemSelected()..
String s= spFacilityType.getSelectedItem().toString();
now s will show the selected item
how to disable a spinner based on another spinner. For sample, when I choose first spinner value is "Yes" then the second spinner is enable also when I choose first spinner value is "No" then the second spinner is disable.
Thanks
Try this:
final Spinner sp1 = (Spinner) findViewById(R.id.spinner1);
final Spinner sp2 = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<String> data = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, first);
ArrayAdapter<String> data2 = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, second);
sp1.setAdapter(data);
sp2.setAdapter(data2);
sp1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String text = sp1.getSelectedItem().toString();
if (text.equals("No")) {
sp2.setEnabled(false);
} else {
sp2.setEnabled(true);
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
I have 2 spinners in my activity as category and sub-category.
When i choose some value from category spinner
I want to display its related values/data in sub-category spinner.
How can i perform this?
public class SellActivity extends Activity {
private Spinner spinner1, spinner2;
private String selectedOption;
List<String> addItemList = new ArrayList<String>();
String[] elCat = new String[] {"Mobile", "Television", "Computers","Home Appliances","Games","Others"};
String[] veCat = new String[] {"Cars", "Buses", "Motorcycles","Others"};
String[] reCat = new String[] {"Apartment", "Plot", "Office","Paying Guest","Others"};
String[] hCat = new String[] {"Books", "Furniture", "Musical Instrument","Watches","Others"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sell);
spinner1 = (Spinner) findViewById(R.id.categorySellSpinner);
spinner2 = (Spinner) findViewById(R.id.subCategorySpinner);
selectedOption=String.valueOf(spinner1.getSelectedItem());
System.out.println(selectedOption);
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
if(selectedOption.equals("Electronics"))
{
Collections.addAll(addItemList, elCat);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(SellActivity.this,android.R.layout.simple_spinner_item, addItemList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter.notifyDataSetChanged();
spinner2.setAdapter(dataAdapter);
}
else if(selectedOption.equals("Home - Lifestyle"))
{
Collections.addAll(addItemList, hCat);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(SellActivity.this,android.R.layout.simple_spinner_item, addItemList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter.notifyDataSetChanged();
spinner2.setAdapter(dataAdapter);
}
else if(selectedOption.equals("Vehicles"))
{
Collections.addAll(addItemList, veCat);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(SellActivity.this,android.R.layout.simple_spinner_item, addItemList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter.notifyDataSetChanged();
spinner2.setAdapter(dataAdapter);
}
else if(selectedOption.equals("Real Estate"))
{
Collections.addAll(addItemList, reCat);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(SellActivity.this,android.R.layout.simple_spinner_item, addItemList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter.notifyDataSetChanged();
spinner2.setAdapter(dataAdapter);
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
Here is the whole code.
Data is not changing....plus old data adds onto it
Just do like this in every if condition :-
Collections.addAll(addItemList, elCat);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String(this,android.R.layout.simple_spinner_item, addItemList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter.notifyDataSetChanged();
spinner2.setAdapter(dataAdapter);
Old data is getting added because you are not clearing the collection you are passing into the adapter just clear the contents of the collection before adding new data into it.
Try this :-
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
addItemList.clear();
spinner2.setAdapter(null);
if(selectedOption.equals("Electronics"))
{
Collections.addAll(addItemList, elCat);
Your code has many problems:
-- Data for spinner1 is missing. From your code, it should be: {"Electronics", "Home - Lifestyle", "Vehicles","Real Estate"}
-- spinner1 does not have an adapter set on it
-- creation and usage of list addItemList is not required
-- usage of selectedOption for checking against the selected item is not required. onItemClick() gives you the selected item in int arg2
-- calling notifyDataSetChanged() to an adapter not set to any View does nothing.
-- arrays can be passed to ArrayAdapters directly, without the need for dumping them into an ArrayList
Try re-initializing the addItemList after coming inside onItemSelected before your if statement..