在android中使用微调器显示隐藏片段?

时间:2015-11-26 04:47:59

标签: android android-fragments android-spinner

我正在尝试使用fragment使用Spinner隐藏和显示,但我不知道如何执行此操作。但是当我使用buttons时,我没有任何问题

任何想法如何制作这个?

这是我的代码:

public void Spinners() {

        Connection connect = ConnectorClass.CONN();
        try {


            if (connect == null) {

                Toast.makeText(getApplicationContext(), "NULL", Toast.LENGTH_LONG).show();
            } else {
                String query = "select window_number from window_info";
                PreparedStatement stmt = connect.prepareStatement(query);
                rs = stmt.executeQuery();

                ArrayList<String> data = new ArrayList<String>();
                while (rs.next()) {
                    String id = rs.getString("window_number");
                    data.add(id);
                }
                ArrayAdapter NoCoreAdapter = new ArrayAdapter(this,
                        android.R.layout.simple_list_item_1, data);
                spinner1.setAdapter(NoCoreAdapter);
            }
        } catch
                (SQLException e) {
            e.printStackTrace();
        }

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

                windows = spinner1.getSelectedItem().toString();

                FragmentTransaction ft = getFragmentManager().beginTransaction();
                ft.setCustomAnimations(android.R.animator.fade_in,
                        android.R.animator.fade_out);
                if(windows == "Window One"){

                }

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

        });
    }

1 个答案:

答案 0 :(得分:0)

做这样的事情。

public void Spinners() {

        Connection connect = ConnectorClass.CONN();
        try {


            if (connect == null) {

                Toast.makeText(getApplicationContext(), "NULL", Toast.LENGTH_LONG).show();
            } else {
                String query = "select window_number from window_info";
                PreparedStatement stmt = connect.prepareStatement(query);
                rs = stmt.executeQuery();

                ArrayList<String> data = new ArrayList<String>();
                while (rs.next()) {
                    String id = rs.getString("window_number");
                    data.add(id);
                }
                ArrayAdapter NoCoreAdapter = new ArrayAdapter(this,
                        android.R.layout.simple_list_item_1, data);
                spinner1.setAdapter(NoCoreAdapter);
            }
        } catch
                (SQLException e) {
            e.printStackTrace();
        }

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

                windows = spinner1.getSelectedItem().toString();

                FragmentTransaction ft = getFragmentManager().beginTransaction();
                ft.setCustomAnimations(android.R.animator.fade_in,
                        android.R.animator.fade_out);
                if(windows == "Window One"){
                      ft.replace(R.id.container,new WindowOneFragment());
                      ft.commit();

                //Container is the Frame layout in which you want to add the fragment and WindowOneFragment is the fragment, you want to place there.
                }

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

        });
    }