Click here to Skip to main content
15,902,862 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have three spinners, the first is populated from the string xml file, the second should be populated based on the selection of the first and the third one based on the selection of the second.

I have search and tried several logic through the weekend with no success. After setting the setOnItemSelectedListener on the second and third spinners, none of them get populated.

Please help me. The sample code is provided below

Java
public class Reg_Prop extends Activity implements AdapterView.OnItemSelectedListener {

    //create reference
    Button btnSave;
    Spinner spinProvince, spinDistrict, spinLocal;
    String spinPro, spinDist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg_prop);

        Toast.makeText(this, "Please select the Province, District and Local Municipality where the " +
                "Property is located", Toast.LENGTH_LONG).show();

        //Initialise reference
        btnSave = (Button)findViewById(R.id.btnSave);
        spinProvince = (Spinner)findViewById(R.id.spinProvince);
        spinDistrict=(Spinner)findViewById(R.id.spinDistrict);
        spinLocal=(Spinner)findViewById(R.id.spinLocal);

        //set onClick Listener
        btnSave.setOnClickListener(new View.OnClickListener() {
            //@Overridde
            public void onClick(View v) {
                try {
                    Intent ro = new Intent(getApplicationContext(), MainActivity.class);
                    startActivity(ro);
                    //Toast.makeText(getApplicationContext(), "Login button pressed", Toast.LENGTH_LONG).show();

                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(), "Login window could not display",
                            Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                }
            }
        });

        //set onItemSelected Listener
        spinProvince.setOnItemSelectedListener(this);
        spinDistrict.setOnItemSelectedListener(this);
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        switch (view.getId()){

            case R.id.spinProvince:
                spinPro = String.valueOf(spinProvince.getSelectedItem());
                Toast.makeText(this, spinPro, Toast.LENGTH_SHORT).show();

                if(spinPro.contentEquals("Eastern Cape")){
                    List<String> list = new ArrayList<String>();

                    list.add("Select District/Metro *");
                    list.add("Buffalo City");
                    list.add("Nelson Mandela Bay");
                    list.add("Alfred Nzo District");
                    list.add("Amathole District");
                    list.add("Chris Hani District");
                    list.add("Joe Gqabi District");
                    list.add("OR Tambo District");
                    list.add("Cacadu District");

                    final ArrayAdapter<String> districtAdapter = new ArrayAdapter<>(this,
                            android.R.layout.simple_spinner_item, list);
                    districtAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    districtAdapter.notifyDataSetChanged();
                    spinDistrict.setAdapter(districtAdapter);
                }
}
            else{
                Toast.makeText(this, "Please select the Province where the Property is located", Toast.LENGTH_LONG).show();
            }

            break;

        case R.id.spinDistrict:
            spinDist = spinDistrict.getSelectedItem().toString();

            if(spinDist != "" && spinDist != "Select District/Metro *" || spinDist != "Select District *"){

                switch (spinDist){
                    case "Buffalo City":
                        List<String> list = new ArrayList<String>();

                        list.add("Select District/Metro *");
                        list.add("...");
                        list.add("N....");
                        list.add("Local");
                        list.add("Amathole District");
                        list.add("Chris Hani District");
                        list.add("Joe Gqabi District");
                        list.add("OR Tambo District");
                        list.add("Cacadu District");

                        final ArrayAdapter<String> localAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
                        localAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        localAdapter.notifyDataSetChanged();
                        spinLocal.setAdapter(localAdapter);

                        break;
                }
            }
            else{
                Toast.makeText(this, "Please select the District Municipality", Toast.LENGTH_LONG).show();
            }
            break;
    }
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

    Toast.makeText(this, "Please select the Province, District and Local Municipality where the Property is located", Toast.LENGTH_LONG).show();
}
Posted
Comments
Richard MacCutchan 21-Sep-15 11:33am    
Have you checked that if(spinPro.contentEquals("Eastern Cape")) returns TRUE?
Member 11999965 21-Sep-15 13:42pm    
Yes I did
It works without the third spinner but once i include the third spinner, even the second one does not populate.
Richard MacCutchan 21-Sep-15 14:51pm    
It looks to me like you are making the wrong tests on your spinner content. you are testing for values that do not exist at the time of the test, i.e. before you populate the spinners. I suggest you recheck your logic.

1 solution

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

    }
 
Share this answer
 
Comments
Member 13005021 22-Mar-17 2:14am    
Please send me the source code to my email bsuneetha437@gmail.com

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900