Java 如何从另一个类获取Android交换机状态?
我正在使用一个RecyclerView,它有一个开关,当开关打开时,用户可以将列表从a组织到Z,当开关关闭时,用户可以将列表从Z组织到a,为了实现OnClick方法,我现在需要确定每个元素的位置,但元素可以位于两个不同的位置(取决于用户选择的排序)因此,为了知道元素在哪里,我需要从实现OnClick方法的类中了解开关的状态 这是我的交换机排序实现:Java 如何从另一个类获取Android交换机状态?,java,android,get,android-widget,Java,Android,Get,Android Widget,我正在使用一个RecyclerView,它有一个开关,当开关打开时,用户可以将列表从a组织到Z,当开关关闭时,用户可以将列表从Z组织到a,为了实现OnClick方法,我现在需要确定每个元素的位置,但元素可以位于两个不同的位置(取决于用户选择的排序)因此,为了知道元素在哪里,我需要从实现OnClick方法的类中了解开关的状态 这是我的交换机排序实现: @Override public void onCheckedChanged(CompoundButton buttonVi
@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;
}
在resume中,我需要找到一种方法来获取开关的状态,这样我就可以知道元素的位置
非常感谢。多亏了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();
}
}
});
多亏了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();
}
}
});
使用SharedReference。开关打开时保存/创建数据(共享),开关关闭时删除数据。如果启用,请检查SharedReference(您可以为此使用标记),如果匹配,则根据需要执行操作。使用SharedReference。开关打开时保存/创建数据(共享),开关关闭时删除数据。如果启用,请检查SharedReference(您可以为此使用标记),如果匹配,则执行任意操作。
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);
}
}
}