如何使用setOnItemSelectedListener基于前两个微调器填充微调器 [英] How to populate a spinner based on the first two spinners using setOnItemSelectedListener

查看:72
本文介绍了如何使用setOnItemSelectedListener基于前两个微调器填充微调器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




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



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



请帮助我。示例代码如下所示



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

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

推荐答案

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

    }


这篇关于如何使用setOnItemSelectedListener基于前两个微调器填充微调器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆