我正在使用此序列来设计应用程序:
(这些课程不会改变,我会用它们进行多项活动)
Activity
带有标签布局(包含两个片段),其中包含:
我要说出这个名字:(包#1)
现在我要复制 Package#1 并更改一些内容,然后将其命名为Package#2。但我这里有一个问题。
我正在使用Package#1,Package#2,Package#3的一个共享首选项......,对吗? 请查看我的共享偏好类:
public class SharedPreference_light {
private SharedPreferences settings;
private SharedPreferences.Editor editor;
private Gson gson = new Gson();
private static final String PREFS_NAME = "Light_Products";
private static final String FAVORITES = "Favorite_Tones_Light";
public SharedPreference_light(Context context) {
settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
editor = settings.edit();
}
private void saveFavorites(List<ProductLocal> favorites) {
String jsonFavorites = gson.toJson(favorites);
editor.putString(FAVORITES, jsonFavorites);
editor.apply();
}
public void addFavorite(ProductLocal product) {
List <ProductLocal> favorites = getFavorites();
if (favorites == null)
favorites = new ArrayList<>();
favorites.add(product);
saveFavorites(favorites);
}
public void removeFavorite(ProductLocal product) {
ArrayList <ProductLocal> favorites = getFavorites();
if (favorites != null) {
favorites.remove(product);
saveFavorites(favorites);
}
}
public ArrayList <ProductLocal> getFavorites() {
List<ProductLocal> favorites;
if (settings.contains(FAVORITES)) {
String jsonFavorites = settings.getString(FAVORITES, null);
ProductLocal[] favoriteItems = gson.fromJson(jsonFavorites, ProductLocal[].class);
favorites = Arrays.asList(favoriteItems);
favorites = new ArrayList <> (favorites);
} else
return null;
return (ArrayList <ProductLocal> ) favorites;
}
}
问题是如果我使用这两个 变量 :
private static final String PREFS_NAME = "Light_Products";
private static final String FAVORITES = "Favorite_Tones_Light";
这些包之间会有冲突。因为我要将一些列表项添加到共享首选项中并使用getSharedPreferences
。那么多个包中的所有项目都将被添加到一个共享首选项中,我不希望这样。
现在我的真正问题是:
如何动态设置共享偏好设置名称( 变量 )?
注意: 我在自定义适配器中使用了一个共享首选项:
private boolean checkFavoriteItem(ProductLocal checkProduct) {
boolean check = false;
List<ProductLocal> favorites = sharedPreference.getFavorites();
if (favorites != null) {
for (ProductLocal product : favorites) {
if (product.equals(checkProduct)) {
check = true;
break;
}
}
}
return check;
}
适配器:
public class LocalAdapter extends RecyclerView.Adapter<LocalAdapter.MyViewHolder>{
private SharedPreference_light sharedPreference;
public LocalAdapter(Activity activity, List<ProductLocal> dataList, RelativeLayout snackLayout) {
this.snackLayout=snackLayout;
this.activity = activity;
this.dataList = dataList ;
this.dataListFilter = dataList ;
sharedPreference = new SharedPreference_light(activity);
methods = new Methods(activity);
}
答案 0 :(得分:3)
首先,您要使用提供包名称的接口:
public interface LightPrefs {
String getPackageName();
}
其次,您可以重用您的类并使其实现以前的接口,但将其变为抽象:
public abstract class SharedPreference_light implements LightPrefs {
private SharedPreferences settings;
private SharedPreferences.Editor editor;
protected final String PREFS_NAME = "Light_Products_" + getPackageName();
protected final String FAVORITES = "Favorite_Tones_Light_" + getPackageName();
public SharedPreference_light(Context context) {
settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
editor = settings.edit();
}
private void saveFavorites(List<ProductLocal> favorites) {
String jsonFavorites = gson.toJson(favorites);
editor.putString(FAVORITES, jsonFavorites);
editor.apply();
}
public void addFavorite(ProductLocal product) {
List <ProductLocal> favorites = getFavorites();
if (favorites == null)
favorites = new ArrayList<>();
favorites.add(product);
saveFavorites(favorites);
}
public void removeFavorite(ProductLocal product) {
ArrayList <ProductLocal> favorites = getFavorites();
if (favorites != null) {
favorites.remove(product);
saveFavorites(favorites);
}
}
}
特别注意一些已更改的可见性修饰符。 最后在你的包中扩展这个抽象类:
public class SharedPreference_package1 extends SharedPreference_light {
private static final String TAG = "SharedPref_package1";
public SharedPreference_package1(Context context) {
super(context);
Log.d(TAG, PREFS_NAME);
}
@Override
public String getPackageName() {
return "package#1";
}
}
和
public class SharedPreference_package2 extends SharedPreference_light {
private static final String TAG = "SharedPref_package2";
public SharedPreference_package2(Context context) {
super(context);
Log.d(TAG, PREFS_NAME);
}
@Override
public String getPackageName() {
return "package#2";
}
}
实例化这两个类可以为您提供此日志:
D/SharedPref_package1: Light_Products_package#1
D/SharedPref_package2: Light_Products_package#2
关于适配器,我认为你应该在构建时指定共享首选项对象:
public class LocalAdapter extends RecyclerView.Adapter<LocalAdapter.MyViewHolder>{
private SharedPreference_light sharedPrefs;
public LocalAdapter(Activity activity, List<ProductLocal> dataList, RelativeLayout snackLayout, SharedPreference_light sharedPrefs) {
this.snackLayout=snackLayout;
this.activity = activity;
this.dataList = dataList ;
this.dataListFilter = dataList ;
this.sharedPrefs = sharedPrefs;
methods = new Methods(activity);
}
因此,您可以在包#1中初始化您的适配器:
SharedPreference_package1 sharedPrefs = new SharedPreference_package1();
LocalAdapter adapter = new LocalAdapter(activity, dataList, snackLayout, sharedPrefs);
您可以在第二个包中使用SharedPreference_package2。
希望这会对你有所帮助。
答案 1 :(得分:0)
使所有方法将共享首选项键作为参数,如:
public SharedPreference_light(Context context, String name);
private void saveFavorites(List<ProductLocal> favorites, String name);
public void addFavorite(ProductLocal product, String name);
public void removeFavorite(ProductLocal product, String name);
public ArrayList <ProductLocal> getFavorites(String name);