2015-09-07 19 views
1

我正在使用一个RecyclerView,它具有一个开关,可以让用户在开关打开时组织从A到Z的列表,并且从Z到A时关闭,以实现我需要的OnClick方法现在是每个元素的位置,但元素可能位于两个不同的位置(取决于用户选择的排序),所以为了知道元素的位置,我需要从实现的类中知道Switch的状态OnClick方法。如何从另一个类获得Android开关状态?

这是我的开关排序实现:

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

而这是其他类的,我需要获得状态的方法:

@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**/{ 

       } 

      } 
     } 

在恢复,我需要找到一种获取开关状态的方法,以便我可以知道元素的位置。

非常感谢。

+2

使用SharedPreference。在开关打开时保存/创建数据(共享),并在关闭时将其删除。如果打开,请检查SharedPreference(您可以使用标签),如果匹配,则无论您想要的位置如何。 –

回答

0

由于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,以现在的开关,我只是比较字符串和基于匹配或missmatching的状态该字符串使用不同的活动,如下所示:

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); 
        } 
       } 
      } 
相关问题