如何使用setOnItemSelectedListener基于前两个微调器填充微调器

时间:2015-09-21 11:40:04

标签: android

我有三个微调器,第一个是从字符串xml文件中填充的,第二个应根据第一个和第三个的选择填充,基于第二个选择。

我在周末搜索并尝试了几种逻辑但没有成功。在第二个和第三个微调器上设置setOnItemSelectedListener后,它们都不会被填充。

请帮帮我。示例代码在下面提供

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

1 个答案:

答案 0 :(得分:0)

感谢所有阅读并试图帮助我的人。我终于解决了这个问题。

我所要做的就是为onCreate中的前两个微调器放置setOnItemSelectedListener,然后使用函数根据选择填充第二个和第三个微调器。注意:第一个微调器是从字符串xml文件中填充的。

以下是示例代码 在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) {

        }
    });

填充微调器的功能

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

}