2015-09-07 43 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); 
        } 
       } 
      } 
相關問題