Home > Mobile >  How to get SharedPreferences float value in RecyclerView Adapter?
How to get SharedPreferences float value in RecyclerView Adapter?

Time:04-25

I am using this SharedPref Class for saving and retrieving value from it. Here I created setTextSize & getTextSize methods.

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

Now I want to call getTextSize method in RecyclerView Adapter for set text size.

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);

        }
    }

}

Please help. Thanks in advance.

CodePudding user response:

  • You can pass values in constructor like this

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

CodePudding user response:

I'd suggest you create a separate class for the SharedPrefs communication that you can call statically, like:

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

}

Then from your preferences activity do a

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

and since you keep the Context in your RecyclerView, get the TextSize from anywhere like

    holder.name.setTextSize( Preferences.getFloat( context, "TextSize", defaultValue );

CodePudding user response:

To get the value from the SharedPreferences, you need context. As @Manjeet suggested, you can use the constructor. But, in the onBindViewHolder, you get context from itemView.getContext(). Then it will work

  • Related