如何在 RecyclerView Adapter 中获取 SharedPreferences 浮点值?

How to get SharedPreferences float value in RecyclerView Adapter?

我正在使用这个 SharedPref Class 来保存和检索它的值。这里我创建了 setTextSize 和 getTextSize 方法。

SharedPref.java


public class SharedPref {
            SharedPreferences mySharedPref;
            float aFloat = 18.0f;

    // this method will save the text size
    public void setTextSize(float tSize) {
        SharedPreferences.Editor editor = mySharedPref.edit();
        editor.putFloat("TextSize",tSize);
        editor.apply();
    }
    
    // this method will get the text size
    public float getTextSize (){
        return mySharedPref.getFloat("TextSize",aFloat);
    }
}

现在我想在 RecyclerView Adapter 中调用 getTextSize 方法来设置文本大小。

NameAdapter.java

public class NameAdapter extends RecyclerView.Adapter<NameAdapter.NameViewHolder> implements Filterable {

    Context context;
    List<NameModel> nameItemList;

    public NameAdapter(@NonNull Context context, List<NameModel> nameItemList) {
        this.context = context;
        this.nameItemList = nameItemList;
    }

    @NonNull
    @Override
    public NameViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.name_item,parent, false );
        return new NameViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull NameViewHolder holder, int position) {

        holder.name.setText(nameItemList.get(position).getName());
        holder.meaning.setText(nameItemList.get(position).getMeaning());

        // here I want to use setTextSize method from SharedPref
        holder.name.setTextSize(getTextSize);
    }

    @Override
    public int getItemCount() {
        return nameItemList.size();
    }

      public class NameViewHolder extends RecyclerView.ViewHolder{

        TextView name,meaning;

        public NameViewHolder(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.tvName);
            meaning = (TextView) itemView.findViewById(R.id.tvMeaning);

        }
    }

}

请帮忙。提前致谢。

  • 你可以像这样在构造函数中传递值

      Context context;
      List<NameModel> nameItemList;
      Float prepsValue;
    
      public NameAdapter(@NonNull Context context, List<NameModel> nameItemList,Float prepsValue) {
          this.context = context;
          this.nameItemList = nameItemList;
          this.prepsValue =prepsValue;
      }
    

我建议您为您可以静态调用的 SharedPrefs 通信创建一个单独的 class,例如:

public class Preferences {

    public static void put( Context context, String key, boolean value ) {
        PreferenceManager.getDefaultSharedPreferences( context ).edit().putBoolean( key, value ).apply();
    }

    public static void put( Context context, String key, int value ) {
        PreferenceManager.getDefaultSharedPreferences( context ).edit().putInt( key, value ).apply();
    }

    public static void put( Context context, String key, long value ) {
        PreferenceManager.getDefaultSharedPreferences( context ).edit().putLong( key, value ).apply();
    }

    public static void put( Context context, String key, float value ) {
        PreferenceManager.getDefaultSharedPreferences( context ).edit().putFloat( key, value ).apply();
    }

    public static void put( Context context, String key, String value ) {
        PreferenceManager.getDefaultSharedPreferences( context ).edit().putString( key, value ).apply();
    }

    public static String getString( Context context, String key ) {
        return getString( context, key, "" );
    }

    public static String getString( Context context, String key, String fallback ) {
        try {
            return PreferenceManager.getDefaultSharedPreferences( context ).getString( key, fallback );
        } catch( Exception ignored ) {
            return fallback;
        }
    }

    public static boolean getBoolean( Context context, String key, boolean fallback ) {
        try {
            return PreferenceManager.getDefaultSharedPreferences( context ).getBoolean( key, fallback );
        } catch( Exception ignored ) {
            return fallback;
        }
    }

    public static int getInt( Context context, String key, int fallback ) {
        try {
            return PreferenceManager.getDefaultSharedPreferences( context ).getInt( key, fallback );
        } catch( Exception ignored ) {
            return fallback;
        }
    }

    public static long getLong( Context context, String key, long fallback ) {
        try {
            return PreferenceManager.getDefaultSharedPreferences( context ).getLong( key, fallback );
        } catch( Exception ignored ) {
            return fallback;
        }
    }

    public static float getFloat( Context context, String key, float fallback ) {
        try {
            return PreferenceManager.getDefaultSharedPreferences( context ).getFloat( key, fallback );
        } catch( Exception ignored ) {
            return fallback;
        }
    }

}

然后根据您的喜好activity做一个

    Preferences.put( getApplicationContext(), "TextSize", aFloat );

并且由于您将 Context 保留在 RecyclerView 中,因此可以从

之类的任何地方获取 TextSize
    holder.name.setTextSize( Preferences.getFloat( context, "TextSize", defaultValue );

要从 SharedPreferences 中获取值,您需要上下文。正如@Manjeet 建议的那样,您可以使用构造函数。但是,在 onBindViewHolder 中,您从 itemView.getContext() 获取上下文。然后就可以了