Hi there, Thanks to all those who read and tried to help me. I have finally solved the problem.
All I had to do was to place setOnItemSelectedListener for the first two spinners within the onCreate then use functions to populate the second and third spinner based on the selection. Note: the first spinner is populate from the string xml file.
Below is the sample code
inside onCreate:
//set onItemSelected Listener
spinProvince.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView parent, View view, int position, long id) {
String province = (String)parent.getItemAtPosition(position);
if(province.matches("Eastern Cape")){
spinPro = 1;
populateDist();
}
else if(province.matches("Free State")){
spinPro = 2;
populateDist();
}
}
@Override
public void onNothingSelected(AdapterView parent) {
Toast.makeText(Reg_Prop.this, "Please select the Province where the Property is located", Toast.LENGTH_LONG).show();
}
});
spinDistrict.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView parent, View view, int position, long id) {
String spinDist = (String)parent.getItemAtPosition(position);
if(spinDist.matches("Buffalo City") && spinPro == 1){
dist = 1;
populateLocal();
}
else if(spinDist.matches("Nelson Mandela Bay") && spinPro == 1){
dist = 2;
populateLocal();
}
}
@Override
public void onNothingSelected(AdapterView parent) {
}
});
Functions to populate the spinners
void populateDist(){
//Eatern Cape
if(spinPro == 1){
String [] ec = {"Select District/Metro *", "Buffalo City", "Nelson Mandela Bay",
"Alfred Nzo District", "Amathole District", "Chris Hani District",
"Joe Gqabi District", "OR Tambo District", "Cacadu District"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ec);
spinDistrict.setAdapter(adapter);
}
//Free State
else if(spinPro == 2){
String [] fs = {"Select District/Metro *", "Mangaung Metropolitan", "Fezile Dabi District",
"Lejweleputswa District", "Thabo Mofutsanyana District", "Xhariep District"};
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, fs);
spinDistrict.setAdapter(adapter2);
}
}
void populateLocal(){
//Baffalo City
if(dist == 1){
String [] bc = {"Buffalo City"};
ArrayAdapter<String> adapterL1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, bc);
spinLocal.setAdapter(adapterL1);
}
//Nelson Mandela Bay
else if(dist == 2){
String [] nmb = {"Nelson Mandela Bay"};
ArrayAdapter<String> adapterL2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, nmb);
spinLocal.setAdapter(adapterL2);
}
}