Thứ Tư, 11 tháng 9, 2013

Change values from one spinner to another in Android

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

Answer

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);
Source:  http://stackoverflow.com/questions/17922979/change-spinner-data-android

Không có nhận xét nào:

Đăng nhận xét