如何在itemSelected后动态创建新的Spinner?

时间:2016-05-16 10:39:34

标签: android

我在一个Android Activity中有一个Spinner元素。

现在我想要,如果我选择了这个Spinner的Item,我应该能够创建一个新的Spinner并将其放入我的活动中。如果我选择第二个Spinner的一个Item,我应该能够创建一个新的Spinner ....

所以这是代码:

public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        v = getActivity().getLayoutInflater().inflate(R.layout.alert_insert_dialog, null);
        builder.setView(v);
        builder.setTitle("Insert Alerts");
        builder.setCancelable(false);

        listValueSet_Agent = new ArrayList<AlertValueSet>();
        valueSet = new AlertValueSet("2.16.840.1.113883.1.11.20.4","106190000",
                "2.16.840.1.113883.6.96","Penicillin","SNOMED CT");
        listValueSet_Agent.add(valueSet);
        valueSet = new AlertValueSet("2.16.840.1.113883.1.11.20.4","281647001",
                "2.16.840.1.113883.6.96","Aspirin","SNOMED CT");
        listValueSet_Agent.add(valueSet);
        valueSet = new AlertValueSet("2.16.840.1.113883.1.11.20.4","282100009",
                "2.16.840.1.113883.6.96","Codeine","SNOMED CT");
        listValueSet_Agent.add(valueSet);
        valueSet = new AlertValueSet("2.16.840.1.113883.1.11.20.4","282100009",
                "2.16.840.1.113883.6.96","Select Agent","SNOMED CT");
        listValueSet_Agent.add(valueSet);

        //spinner status
        Spinner sAgent = (Spinner) v.findViewById(R.id.combo_agent);
        adapterAgent = new ArrayAdapter<AlertValueSet>(v.getContext(),
                android.R.layout.simple_spinner_item, listValueSet_Agent);
        sAgent.setAdapter(adapterAgent);
        sAgent.setSelection(0);
        sAgent.setOnItemSelectedListener(new SpinnerActivity());
        return builder.create();
}

这个类是OnItemSelectedListener类的实现:

public class SpinnerActivity implements AdapterView.OnItemSelectedListener {

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

            Spinner spinnerAgentN = new Spinner(v.getContext());
            spinnerAgentN.setAdapter(adapterAgent);
            spinnerAgentN.setSelection(listValueSet_Agent.size()-1);
            spinnerAgentN.setOnItemSelectedListener(new SpinnerActivity());
            LinearLayout linearLayout = (LinearLayout)v.findViewById(R.id.layoutText);
            linearLayout.addView(spinnerAgentN,2);
        }

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

        }

    }

现在的问题是,如果我尝试运行我的应用程序,每次调用此方法(onItemSelected),我可以在我的活动中看到N spinner,但这并不好。

如果我尝试删除此代码:

spinnerAgentN.setOnItemSelectedListener(new SpinnerActivity());

我没有任何问题。

我该如何解决?

3 个答案:

答案 0 :(得分:0)

将数据设置为适配器,然后自动调用onitem click listener。因此,在项目单击侦听器方法中,您必须检查位置值。如果位置值为0,那么您没有将数据设置为第二个微调器,否则您必须将数据设置为第二个微调器。

答案 1 :(得分:0)

在onItemSelected()

public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
        AlertValueSet set = listValueSet_Agent.get(pos);
        // Fetch new data based on the selected item and set it back to the spinner

        adapterAgent = new ArrayAdapter<AlertValueSet>(v.getContext(),
            android.R.layout.simple_spinner_item, listValueSet_Agent);
        sAgent.setAdapter(adapterAgent);
    }

答案 2 :(得分:0)

根据我对代码和要求的理解,有两种方法可以解决您的查询。

但我只会解释一个符合您要求的

只需声明您的数组如下。

   listValueSet_Agent = new ArrayList<AlertValueSet>();
    valueSet = new AlertValueSet("Select","2.16.840.1.113883.1.11.20.4","106190000",
            "2.16.840.1.113883.6.96","Penicillin","SNOMED CT");
    listValueSet_Agent.add(valueSet);
    valueSet = new AlertValueSet("Select","2.16.840.1.113883.1.11.20.4","281647001",
            "2.16.840.1.113883.6.96","Aspirin","SNOMED CT");
    listValueSet_Agent.add(valueSet);
    valueSet = new AlertValueSet("Select","2.16.840.1.113883.1.11.20.4","282100009",
            "2.16.840.1.113883.6.96","Codeine","SNOMED CT");
    listValueSet_Agent.add(valueSet);
    valueSet = new AlertValueSet("Select","2.16.840.1.113883.1.11.20.4","282100009",
            "2.16.840.1.113883.6.96","Select Agent","SNOMED CT");
    listValueSet_Agent.add(valueSet);

现在,更改你的onItemSelected方法,如下所示,

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

        if(pos > 0)
        {
            Spinner spinnerAgentN = new Spinner(v.getContext());
            spinnerAgentN.setAdapter(adapterAgent);
            spinnerAgentN.setSelection(listValueSet_Agent.size()-1);
            spinnerAgentN.setOnItemSelectedListener(new SpinnerActivity());
            LinearLayout linearLayout = (LinearLayout)v.findViewById(R.id.layoutText);
            linearLayout.addView(spinnerAgentN,2);
        }
    }

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

    }