如何从另一个 class 获取 Android 切换状态?

How to get an Android Switch status from another class?

我正在使用 RecyclerView,它有一个开关,当开关打开时,用户可以将列表从 A 组织到 Z,当开关关闭时,从 Z 到 A,以实现 OnClick 方法我现在需要每个元素的位置,但元素可能位于两个不同的位置(取决于用户选择的排序)所以为了知道元素在哪里,我需要从class 实现了 OnClick 方法。

这是我的开关排序实现:

@Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if(isChecked){
                    switchStatus.setText("Sorting alphabetically");


                    Collections.sort(categories, new Comparator<Categories>() {
                        @Override
                        public int compare(Categories lhs, Categories rhs) {
                            return lhs.title.compareTo(rhs.title);
                        }
                    });


                    ca.notifyDataSetChanged();

                }else{
                    switchStatus.setText("Sorting by popularity");

                    Collections.sort(categories, new Comparator<Categories>() {
                        @Override
                        public int compare(Categories rhs, Categories lhs) {
                            return lhs.title.compareTo(rhs.title);
                        }
                    });

                ca.notifyDataSetChanged();
                }

            }
        });


        //check the current state before we display the screen
        if(categoriesSortingSwitch.isChecked()){
            switchStatus.setText("Sorting alphabetically");


            Collections.sort(categories, new Comparator<Categories>() {
                @Override
                public int compare(Categories lhs, Categories rhs) {
                    return lhs.title.compareTo(rhs.title);
                }
            });


        }
        else {
            switchStatus.setText("Sorting by popularity");

            Collections.sort(categories, new Comparator<Categories>() {
                @Override
                public int compare(Categories rhs, Categories lhs) {
                    return lhs.title.compareTo(rhs.title);
                }
            });

        }

    }

//I CREATED THIS METHOD THINKING IN USING IT IN THE OTHER CLASS TO GET THE STATUS
   public boolean getSwitchstatus(){

        if(categoriesSortingSwitch.isChecked()){

            return true;
        }
        else return false;
    }

这是另一个 class 的方法,我需要在其中获取状态:

@Override
        public void onClick(View v) {
            final Intent intent;

            int position = getAdapterPosition();
            if (position==0){

                if(/** find a way to see if it is on or off and if it is on do this**/){

                intent =  new Intent(context, nose.class);
                context.startActivity(intent);
                }
                else/** if it is off**/{

                }

            }
        }

在简历中,我需要找到一种方法来获取开关的状态,这样我才能知道元素的位置。

非常感谢。

感谢 MkiiSoft 的建议,这就是我所做的并且工作得很好:

    @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    if(isChecked){
                        switchStatus.setText("Sorting alphabetically");


                        Collections.sort(categories, new Comparator<Categories>() {
                            @Override
                            public int compare(Categories lhs, Categories rhs) {
                                return lhs.title.compareTo(rhs.title);
                            }
                        });


                        ca.notifyDataSetChanged();


//JUST ADDED THIS SHARED PREFERENCE WITH THE STRINGS ("it's","on") WHEN THE SWITCH IS ON

                        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                        SharedPreferences.Editor editor = preferences.edit();
                        editor.putString("it's","on");
                        editor.apply();

                    }else{
                        switchStatus.setText("Sorting by popularity");

                        Collections.sort(categories, new Comparator<Categories>() {
                            @Override
                            public int compare(Categories rhs, Categories lhs) {
                                return lhs.title.compareTo(rhs.title);
                            }
                        });

                    ca.notifyDataSetChanged();

    //JUST ADDED THIS SHARED PREFERENCE WITH THE STRINGS ("it's","off") WHEN THE SWITCH IS OFF

                        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                        SharedPreferences.Editor editor = preferences.edit();
                        editor.putString("it's", "off");
                        editor.apply();
                    }

                }
            });

然后在 Class 中我现在需要开关的状态,我简单地比较了字符串并根据字符串的匹配或不匹配打开了一个不同的 Activity,这样 :

    public void onClick(View v) {
                final Intent intent;

                int position = getAdapterPosition();

                SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
                String onoff = preferences.getString("it's", "");

//IF IT IS ON DO THIS
                if(onoff.equalsIgnoreCase("on"))
                {
                    if (position==0){

                        intent =  new Intent(context, nose.class);
                        context.startActivity(intent);
                    }

    //IF IT IS OFF DO THIS
                } else if (onoff.equalsIgnoreCase("off")){

                    if (position==0){

                        intent =  new Intent(context, nose2.class);
                        context.startActivity(intent);
                    }
                }
            }