Populate first spinner on the basis of second spinner - android

I want my page to have two spinners and want to populate the second spinner on the basis of selection of the first spinner . I took two arrays . The first array is or the 1st spinner and the three arrays are for the second spinner . On the value selected from the first spinner I want the second spinner to select the corresponding values .
I have also referenced
get selected value from second spinner on the basis of selected value of first spinner
in this post , the author has used three adapters for three arrays to populate the second spinner but I was thinking of using only two . Is it possible ?
Simple Android two spinner and submit example
This code isn't showing any errors but the app isn't opening .
Spinner spin,spin1;
String state[] = new String[]{"A","B","C"};
String A[]= new String[]{"Pink","Blue","Red","white"};
String B[]= new String[]{"Apple","Grapes","Banana","Orange"};
String C[]= new String[]{"River","Mountains","Tree","Flower"};
ArrayAdapter<String> adap;
ArrayAdapter<String> adap1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spin = findViewById(R.id.spin);
spin1 = findViewById(R.id.spin1);
adap = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item,state);
spin.setAdapter(adap);
spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(MainActivity.this, ""+ state[i], Toast.LENGTH_SHORT).show();
if (state[i].equalsIgnoreCase("A")) {
adap1.clear();
adap1 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, A);
spin1.setAdapter(adap1);
}
if (state[i].equalsIgnoreCase("B")) {
adap1.clear();
adap1 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item,B);
spin1.setAdapter(adap1);
}
if (state[i].equalsIgnoreCase("C")) {
adap1.clear();
adap1 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, C);
spin1.setAdapter(adap1);
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
}

Check of null pointer exp -
spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(MainActivity.this, "" + state[i], Toast.LENGTH_SHORT).show();
if (adap1 != null) {
adap1.clear();
}
if (state[i].equalsIgnoreCase("A")) {
adap1 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, A);
spin1.setAdapter(adap1);
}
if (state[i].equalsIgnoreCase("B")) {
adap1 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, B);
spin1.setAdapter(adap1);
}
if (state[i].equalsIgnoreCase("C")) {
adap1 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, C);
spin1.setAdapter(adap1);
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});

Remove adap1.clear(); from all if conditions

Related

Problem with using depended spinner (Multiple spinner) in android

I'm trying to populate value of the next spinner from the previous spinner. In this case, what I did yet is generating all values inside the spinner means, no matter which value I select in the previous spinner it is always showing all values in the next spinner. I'm using five spinners that needs to populate values according to the previous one. Here I'm using a library for the spinner. The library is as follows.
'com.toptoche.searchablespinner:searchablespinnerlibrary:1.3.1'
Here I'm sharing my whole code/class for your consideration. There is no error and I get all values in respective spinner but I want to get values according to the previous one. here is my code.
import androidx.appcompat.app.AppCompatActivity;
import com.toptoche.searchablespinnerlibrary.SearchableSpinner;
public class Catchment extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
public ArrayAdapter<String> regionAdapter;
public ArrayAdapter<String> districtAdapter;
public ArrayAdapter<String> upzAdapter;
public ArrayAdapter<String> unionAdapter;
public ArrayAdapter<String> villageAdapter;
public String[] Region = {"1", "2",
};
public String[] District = {"1","2","3","4","5","6","7","8",};
public String[] Upz = {"1","2"...."23";
public String[] Union = {"1","2"...."25"..."50"...."115"};
public String[] Village = { "1","2"...."25"..."50"...."150"...."500"...."947" };
Button catchment_code;
SearchableSpinner spinner_region, spinner_district, spinner_upz, spinner_union, spinner_village;
String[] region_code;
String[] district_code;
String[] upz_code;
String[] union_code;
String[] vill_code;
int positions;
String test;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_catchment);
spinner_region = (SearchableSpinner) findViewById(R.id.region);
spinner_district = (SearchableSpinner) findViewById(R.id.district);
spinner_upz = (SearchableSpinner) findViewById(R.id.upz);
spinner_union = (SearchableSpinner) findViewById(R.id.union);
spinner_village = (SearchableSpinner) findViewById(R.id.village);
catchment_code = findViewById(R.id.catchment_code);
// Set spninner adapter
regionAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, Region);
districtAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, District);
upzAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, Upz);
unionAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, Union);
villageAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, Village);
//Set dropdown to the spinner
regionAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
districtAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
upzAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
unionAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
villageAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//assign to spinner
spinner_region.setAdapter(regionAdapter);
spinner_district.setAdapter(districtAdapter);
spinner_upz.setAdapter(upzAdapter);
spinner_union.setAdapter(unionAdapter);
spinner_village.setAdapter(villageAdapter);
spinner_region.setOnItemSelectedListener(this);
spinner_district.setOnItemSelectedListener(this);
spinner_upz.setOnItemSelectedListener(this);
spinner_union.setOnItemSelectedListener(this);
spinner_village.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (parent.getId() == R.id.region) {
positions = spinner_region.getSelectedItemPosition();
region_code = this.getResources().getStringArray(R.array.region_code);
//Toast.makeText(this, region_code[positions], Toast.LENGTH_SHORT).show();
}
if (parent.getId() == R.id.district) {
positions = spinner_district.getSelectedItemPosition();
district_code = this.getResources().getStringArray(R.array.district_code);
//Toast.makeText(this, district_code[positions], Toast.LENGTH_SHORT).show();
}
if (parent.getId() == R.id.upz) {
positions = spinner_upz.getSelectedItemPosition();
upz_code = this.getResources().getStringArray(R.array.upz_code);
//Toast.makeText(this, upz_code[positions], Toast.LENGTH_SHORT).show();
}
if (parent.getId() == R.id.village) {
positions = spinner_village.getSelectedItemPosition();
vill_code = this.getResources().getStringArray(R.array.village_code);
//Toast.makeText(this, vill_code[positions], Toast.LENGTH_LONG).show();
}
if (parent.getId() == R.id.union) {
positions = spinner_union.getSelectedItemPosition();
union_code = this.getResources().getStringArray(R.array.union_code);
//Toast.makeText(this, union_code[positions], Toast.LENGTH_SHORT).show();
}
if (!(vill_code == null || region_code == null || district_code == null || upz_code == null ||
union_code == null)) {
test = region_code[positions] +
district_code[positions] +
upz_code[positions] +
union_code[positions] + vill_code[positions];
Toast.makeText(this, test, Toast.LENGTH_SHORT).show();
}
catchment_code.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Catchment.this, Participant_Details.class);
intent.putExtra("catment_code", test);
startActivity(intent);
}
});
//Toast.makeText(this, union_code[positions]+upz_code[positions], Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
#Override
public void onBackPressed() {
Intent intent = new Intent(Catchment.this, SplashScreen.class);
Catchment.this.startActivity(intent);
Catchment.this.finish();}
}
So how can I get rid of this problem? I need help.
You are declaring all adapter at once in onCreate().
Instead declare just first adapter with values in onCreate() and declare other adapters with empty values.
#Override
protected void onCreate(Bundle savedInstanceState) {
--------;
// Set spninner adapter
regionAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, Region);
districtAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, new String[]());
upzAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, new String[]());
unionAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, new String[]());
villageAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, new String[]());
------
spinner_region.setOnItemSelectedListener(this);
}
Now on itemSelectedListener, filter the desired values if needed and add the values to the next required adapter and notify the values along with adding listener to next spinner(adding listener is up to you, how you want to handle the things)
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (parent.getId() == R.id.region) {
positions = spinner_region.getSelectedItemPosition();
String[] filteredDistrict = District.filter(Region[position]) // District.filter is not any predefined function, assuming some filter needed for selected Districts
districtAdapter.addValues(filteredDistrict);
districtAdapter.notifyDataSetChanged()
spinner_district.setOnItemSelectedListener(this);
}
}
Other than that filtering logic depends on your usecase.

Android - Two spinners in one Activity - Saving Data is not working

my problem is the following. I have 2 Spinners in one activity. I want that both spinner items should be saved in the DB. But there is a slight difference between both spinners:
Spinner 1: Is populated by data from other db-table - when it is the only spinner in the mentioned activity, it works.
Spinner 2: Has default values. I am not sure if added it correctly to my activity. Anyway, after i added it to my saving the data is not working anymore.
Please help me what am I doing wrong??
My Activity Code:
public class activity_transport extends AppCompatActivity {
private BottomNavigationViewEx bottomNavigationViewEx;
DatabaseHelper myDb;
Button btn_save;
Spinner ChooseProject,ChooseTransport;
EditText Entfernung,Price,MWST;
String selectedspinner, selectedTransport;
ImageView imageView2;
private static int PICK_IMAGE = 100;
Uri imageUri;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_transport);
myDb = new DatabaseHelper(this);
ChooseProject = (Spinner) findViewById(R.id.ChooseProject);
ChooseTransport = (Spinner) findViewById(R.id.ChooseTransport);
Entfernung = (EditText) findViewById(R.id.Entfernung);
Price = (EditText) findViewById(R.id.Preis);
MWST = (EditText) findViewById(R.id.MwSt);
btn_save=(Button) findViewById(R.id.btn_save);
//bildanzeigebereich im layout
imageView2=(ImageView) findViewById(R.id.imageView2);
loadProjectSpinnerData();
//loadTransportSpinnerData();
SaveData();
//+++++++++++++++Function to load the spinner data from SQLite database++++++++++//
private void loadProjectSpinnerData() {
// database handler
DatabaseHelper db = new DatabaseHelper (getApplicationContext());
// Spinner Drop down elements
List<String> projects = db.getAllProjects();
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, projects);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ChooseProject.setPrompt("Projekt auswählen");
// attaching data adapter to spinner
ChooseProject.setAdapter(dataAdapter);
//Choose Transport Spinner
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, new String[] {
new String("Auto"),
new String("Flugzeug"),
new String("Taxi"),
new String("Bahn")});
ChooseTransport.setAdapter(spinnerArrayAdapter);
// Drop down layout style - list view with radio button
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ChooseTransport.setPrompt("Transport auswählen");
// attaching data adapter to spinner
ChooseTransport.setAdapter(spinnerArrayAdapter);
//Listener für den Projekt Spinner damit ich den Wert abspeichern kann
ChooseProject.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Spinner spinner = (Spinner) parent;
if(spinner.getId() == R.id.ChooseProject)
{
selectedspinner = (String) ChooseProject.getSelectedItem();
}
else if(spinner.getId() == R.id.ChooseTransport)
{
selectedTransport = (String) ChooseTransport.getSelectedItem();
}
//selectedspinner =String.ValueOf(parent.getItemAtPosition(pos));
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
/*private void loadTransportSpinnerData() {
// Spinner method to read the on selected value
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, new String[] {
new String("Auto"),
new String("Flugzeug"),
new String("Taxi"),
new String("Bahn")});
ChooseTransport.setAdapter(spinnerArrayAdapter);
// Creating adapter for spinner
//ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, projects);
// Drop down layout style - list view with radio button
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ChooseTransport.setPrompt("Transport auswählen");
// attaching data adapter to spinner
ChooseTransport.setAdapter(spinnerArrayAdapter);
//Listener für den Projekt Spinner damit ich den Wert abspeichern kann
ChooseTransport.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
//selectedspinner =String.ValueOf(parent.getItemAtPosition(pos));
selectedTransport = (String) ChooseTransport.getSelectedItem();
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}*/
//++++++++++++Save Data++++++//
public void SaveData(){
btn_save.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean isInserted = myDb.createTransport(
selectedspinner,
selectedTransport,
Integer.valueOf(Price.getText().toString()),
Integer.valueOf(MWST.getText().toString()),
Integer.valueOf(Entfernung.getText().toString()),
imageViewToByte(imageView2)
);
if(isInserted=true)
Toast.makeText(activity_transport.this, "Daten gespeichert", Toast.LENGTH_LONG).show();
else
Toast.makeText(activity_transport.this, "Daten nicht gespeichert", Toast.LENGTH_LONG).show();
}
}
);
}
}
EDIT:
I tried to run the app by setting a few breakpoints.
When trying to run this line selectedTransport = (String) ChooseTransport.getSelectedItem(); the debugger posts Cannot find local variable 'ChooseTransport'
I solved the problem:
I implemented an own method for ChooseTransport- Spinner and now it works. I also figured out that the order how I add the data to the DB was not right. I changed this too.
See here my method for my second spinner:
private void loadTransportSpinnerData(){
ArrayAdapter<CharSequence> spinnerArrayAdapter = ArrayAdapter.createFromResource(this, R.array.transport, android.R.layout.simple_spinner_dropdown_item);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ChooseTransport.setPrompt("Transport auswählen");
ChooseTransport.setAdapter(spinnerArrayAdapter);
ChooseTransport.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
selectedTransport = (String) ChooseTransport.getSelectedItem();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}

Open Text plain when spinner option get selected

i got a spinner with some options, and i want to change visibility of text box when the specific "other" option get selected , how should i do this ?
this.arraySpinner = new String[] {"Mobile", "Gaming", "Media Features", "Medical", "Sports And Nutrition","Security","Space","Other"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, arraySpinner);
categories.setAdapter(adapter);
Try this:
.................
........................
this.arraySpinner = new String[] {"Mobile", "Gaming", "Media Features", "Medical", "Sports And Nutrition","Security","Space","Other"};
// Views
categories= (Spinner) findViewById(R.id.spinner);
textView = (TextView) findViewById(R.id.textView);
// Adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, arraySpinner);
categories.setAdapter(adapter);
// Set onItemSelected listener to spinner
categories.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
TextView spinnerText = (TextView) view;
// Checking weather selected item is "Other" or not
if (spinnerText.getText().equals("Other"))
{
// Hide TextView
textView.setVisibility(View.INVISIBLE); // or View.GONE
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
.............
...................
Hope this will help
you can Implement OnItemSelected in the spinner and do the things what you want.
categories.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
if(arraySpinner!=null && arraySpinner[position].equalIgnoreCase("Other")){
// your code here
}
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});

Android Spinner setSelection calls onItemSelected immidiately or not?

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

Item selection in spinner doesn't detect in MyOnItemSelectedListener

I have activity with 3 spinners and selection of 1st spinner is related to 2nd and 2nd selection is related to the 3rd, so I have the spinner code one inside one.
CrustSp, SizeSp and extraDescriptionOneSP are the 3 spinners I have. I have loaded the items to the Spinners and I can select a value as well. But my problem is whenever i made a selection in CrustSp and SizeSp doesn't show in MyOnItemSelectedListener(). But if I made a selection in extraDescriptionOneSP it shows inside MyOnItemSelectedListener().
I can't figure where I have gone wrong, anyone can point out where I have gone wrong it will be really helpful.
This the code
crust = Utils.removeDuplicatesFromList(crust);
ArrayAdapter<String> dataAdapterCru = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, crust);
dataAdapterCru
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
crustSP.setAdapter(dataAdapterCru);
crustSP.setOnItemSelectedListener(new MyOnItemSelectedListener(
ActivityPizzaCustomize.this) {
public void onNothingSelected(AdapterView<?> arg0) {
}
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
String crustSelectedItem = crustSP.getSelectedItem()
.toString();
List<String> resultSizes = getFilteredSizes(crustSelectedItem);
ArrayAdapter<String> dataAdapterDes = new ArrayAdapter<String>(
ActivityPizzaCustomize.this,
android.R.layout.simple_spinner_item,
resultSizes);
dataAdapterDes
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sizeSP.setAdapter(dataAdapterDes);
sizeSP.setOnItemSelectedListener(new MyOnItemSelectedListener(
ActivityPizzaCustomize.this) {
public void onNothingSelected(AdapterView<?> arg0) {
}
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
String sizeSelectedItem = sizeSP
.getSelectedItem().toString();
List<String> resultTopping = getFilteredToppings(sizeSelectedItem);
extraDescriptionOneSP
.setOnItemSelectedListener(new MyOnItemSelectedListener(
ActivityPizzaCustomize.this));
ArrayAdapter<String> dataAdapterExtraDesOne = new ArrayAdapter<String>(
ActivityPizzaCustomize.this,
android.R.layout.simple_spinner_item,
resultTopping);
dataAdapterExtraDesOne
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
extraDescriptionOneSP
.setAdapter(dataAdapterExtraDesOne);
extraDescriptionOneSP
.setAdapter(new NothingSelectedSpinnerAdapter(
dataAdapterExtraDesOne,
R.layout.contact_spinner_row_nothing_selected,
ActivityPizzaCustomize.this));
}
});
}
});
MyOnItemSelectedListener method,
public class MyOnItemSelectedListener implements OnItemSelectedListener {
Activity activity;
String StrOneSPPosition;
public MyOnItemSelectedListener(Activity p_activity) {
activity = p_activity;
}
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// check which spinner triggered the listener
switch (parent.getId()) {
case R.id.sp_crust: {
String StrCrustSPPosition = String.valueOf(crustSP
.getSelectedItemPosition());
System.out.println("crust = " + StrCrustSPPosition);
System.out.println("crust = " + position);
CrustSPPosition = Integer.parseInt(StrCrustSPPosition);
}
I don't see you using the selected position to determine the data for next spinner. BTW you should not make new adapter for each selection, instead reuse the once made adapter.

Categories

Resources