如何用独特的值填充多个微调器?和处理其项目选定的侦听器

时间:2016-10-04 08:51:07

标签: android android-layout android-studio

我想在多个微调器中填充数据而没有重复的数据

我已经尝试并填充了三个微调器,但仍然无法正确选择数据所以请帮我提供一些示例

我的情景: 当我从第一个微调器中选择任何一个项目时,那个项目不应该在第二个和第三个微调器中重复,反之亦然,对于其他两个微调器

我的代码:

private Spinner sp1, sp2, sp3;
    ArrayList<String> MAinArrayList;
    ArrayList<String> TempList;

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

        sp1 = (Spinner) findViewById(R.id.spinner1);
        sp2 = (Spinner) findViewById(R.id.spinner2);
        sp3 = (Spinner) findViewById(R.id.spinner3);

        TempList = new ArrayList<String>();
        MAinArrayList = new ArrayList<String>();
        MAinArrayList.add("a");
        MAinArrayList.add("b");
        MAinArrayList.add("c");
        MAinArrayList.add("d");
        MAinArrayList.add("e");
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, MAinArrayList);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //  dataAdapter.notifyDataSetChanged();
        sp1.setAdapter(dataAdapter);

        final String strA = sp1.getSelectedItem().toString();

        TempList.addAll(MAinArrayList);
        for (int i = 0; i < TempList.size(); i++) {
            if (TempList.get(i).equals(strA)) {
                TempList.remove(strA);
                break;
            }
        }
        for (int i = 0; i < TempList.size(); i++) {
            System.out.println("Templist " + (i) + "==>" + TempList.get(i));
        }
        ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, TempList);
        dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //  dataAdapter.notifyDataSetChanged();
        sp2.setAdapter(dataAdapter2);
//        ArrayList<String> TempList1 = new ArrayList<>();
//        TempList1.addAll(MAinArrayList);

//        if (TempList.size() > 0) {
//            TempList.clear();
//        }
        //TempList.addAll(MAinArrayList);

        final String strB = sp2.getSelectedItem().toString();
        final ArrayList<String> TempList1 = new ArrayList<>();
        TempList1.addAll(TempList);
        System.out.println("Templist1 ADDDED" + TempList1);

        for (int i = 0; i < TempList1.size(); i++) {
            if (TempList1.get(i).equals(strB)) {
                TempList1.remove(strB);
                break;
            }

            System.out.println("removed templist " + TempList1);

        }

        for (int i = 0; i < TempList1.size(); i++) {
            System.out.println("TempList3 " + (i) + "==>" + TempList1.get(i));
        }
        ArrayAdapter<String> dataAdapter3 = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, TempList1);
        dataAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //  dataAdapter.notifyDataSetChanged();
        sp3.setAdapter(dataAdapter3);

        sp2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                ArrayList<String> TempSP2 = new ArrayList<String>();
                TempSP2.addAll(MAinArrayList);
                System.out.println("TempSP2 ==>" + TempSP2);
                String strC = sp3.getSelectedItem().toString();
                String strA = sp1.getSelectedItem().toString();

                for (int i = 0; i < TempSP2.size(); i++) {
                    if (TempSP2.get(i).equals(strC)) {
                        TempSP2.remove(strC);
                        break ;
                    }
                }

                for (int i = 0; i < TempSP2.size(); i++) {
                    if (TempSP2.get(i).equals(strA)) {
                        TempSP2.remove(strA);
                        break;
                    }
                }
                System.out.println("removed TempSP2 strAC" + TempSP2);

                ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(SpinnerDemo.this,
                        android.R.layout.simple_spinner_item, TempSP2);
                dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                //  dataAdapter.notifyDataSetChanged();
                sp2.setAdapter(dataAdapter);
            }

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

            }
        });
        sp3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                ArrayList<String> TempSP3 = new ArrayList<String>();
                TempSP3.addAll(MAinArrayList);
                System.out.println("TempSP3 ==>" + TempSP3);
                String strA = sp1.getSelectedItem().toString();
                String strB = sp2.getSelectedItem().toString();

                for (int i = 0; i < TempSP3.size(); i++) {
                    if (TempSP3.get(i).equals(strA)) {
                        TempSP3.remove(strA);
                        break;
                    }
                }

                for (int i = 0; i < TempSP3.size(); i++) {
                    if (TempSP3.get(i).equals(strB)) {
                        TempSP3.remove(strB);
                        break;
                    }
                }
                System.out.println("removed TempSP3 strAb" + TempSP3);

                ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(SpinnerDemo.this,
                        android.R.layout.simple_spinner_item, TempSP3);
                dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                //  dataAdapter.notifyDataSetChanged();
                sp3.setAdapter(dataAdapter);
            }


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

            }
        });

        sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                ArrayList<String> TempSP1 = new ArrayList<String>();
                TempSP1.addAll(MAinArrayList);
                System.out.println("TempSP1 ==>" + TempSP1);
                String strB = sp2.getSelectedItem().toString();
                String strC = sp3.getSelectedItem().toString();

                for (int i = 0; i < TempSP1.size(); i++) {
                    if (TempSP1.get(i).equals(strB)) {
                        TempSP1.remove(strB);
                        break;
                    }
                }

                for (int i = 0; i < TempSP1.size(); i++) {
                    if (TempSP1.get(i).equals(strC)) {
                        TempSP1.remove(strC);
                        break;
                    }
                }
                System.out.println("removed TempSP1 strBC" + TempSP1);

                ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(SpinnerDemo.this,
                        android.R.layout.simple_spinner_item, TempSP1);
                dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                //  dataAdapter.notifyDataSetChanged();
                sp1.setAdapter(dataAdapter);
            }

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

            }
        });

提前致谢

1 个答案:

答案 0 :(得分:0)

在项目string.xml文件中,写入要放在微调器中的数据:

 spinner1 = (Spinner) findViewById(R.id.spinner1); 
 String quintity= String.valueOf(spinner1.getSelectedItem());
     switch(quintity){

      case "Spoon":
    //what you want to do
    break;
      case "Cup":
    //what you want to do
    break;}

然后在spinner属性的项目布局中写入entries字段 @阵列/ cooking_arrays

然后在你的活动中

 SELECT * FROM tblInteractions WHERE date(ctime) = '2016-09-30' AND userID = 8