logo
down
shadow

How to save hashmap to shared preference?


How to save hashmap to shared preference?

By : user2956395
Date : November 22 2020, 10:56 AM
this one helps. Try this to save objects in SharedPreferences.
you need to add Gson library to your project.
code :
public void putMyObject(String key , Object obj) {

        //AnyVehicleModel mvehicle  =new AnyVehicleModel();
        SharedPreferences.Editor editor = preferences.edit();
        Gson gson = new Gson();
        String json = gson.toJson(obj);
        editor.putString(key,json);
        editor.apply();
    }




    public MyObject getMyObject(String key) {

        Gson gson = new Gson();
        String json = preferences.getString(key,"");
        MyObject obj = gson.fromJson(json, MyObject.class);
        if (obj== null){return new MyObject ();}
        return obj;

    }


Share : facebook icon twitter icon
how to check an items stored in a shared preference if it is equal to a new entry to this shared preference

how to check an items stored in a shared preference if it is equal to a new entry to this shared preference


By : Trashdres
Date : March 29 2020, 07:55 AM
it helps some times I have 2 problems with shared preference .. the first one each time when I try to store a new data to a shared preferences the old one is overwritten !! , U can get It firstly what u have already stored in SharedPreference.
code :
      String oldUser=spe.gettString("userName",name);

      String oldpass= spe.getString("password",pass);

       String oldId= spe.getString("userId",Id);
if(Id.equalsIgnoreCase(oldId))
{
//it contains
}
else
{}
How to save Image in shared preference in Android | Shared preference issue in Android with Image

How to save Image in shared preference in Android | Shared preference issue in Android with Image


By : 徐敬尧
Date : March 29 2020, 07:55 AM
seems to work fine In my application after login I have to save user name and image in shared preference for other pages. I am able to save name in preference but can't get any where how to save image. , I solved your problem do something like that:
code :
// method for bitmap to base64
public static String encodeTobase64(Bitmap image) {
    Bitmap immage = image;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    immage.compress(Bitmap.CompressFormat.PNG, 100, baos);
    byte[] b = baos.toByteArray();
    String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);

    Log.d("Image Log:", imageEncoded);
    return imageEncoded;
}
SharedPreferences.Editor editor = myPrefrence.edit();
editor.putString("namePreferance", itemNAme);
editor.putString("imagePreferance", encodeTobase64(yourbitmap));
editor.commit();
// method for base64 to bitmap
public static Bitmap decodeBase64(String input) {
    byte[] decodedByte = Base64.decode(input, 0);
    return BitmapFactory
            .decodeByteArray(decodedByte, 0, decodedByte.length);
}
Save value of Int (Shared Preference)

Save value of Int (Shared Preference)


By : Илина Недева
Date : March 29 2020, 07:55 AM
should help you out I'm new to java. I've made a counter which goes up as user holds on a button. I want the app to start with int value of where it left. I know SharedPreference is the way to go but I've no idea how to use it. I'm not sure where to put which part of SharedPreference. Thank you. , Add the following function to your activity
code :
 public int getValue(String key) {
        SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
        int value = sharedPref.getInt(key, 0);
        return value;
 }
 public void saveValue(String key, int value) {
        SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putInt(key, value);
        editor.commit();
 }
final String key = "somekey";
count  = getValue(key); //get value from sharedPreference
button = (Button) findViewById(R.id.button);
text = (TextView) findViewById(R.id.textView);
text.setText(String.valueOf(count)); // set it first
button.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View view) {
        count++;
        saveValue(key,count);
        text.setText(String.valueOf(count));
        return false;
    }
});
how to Save value in Shared Preference and set in spinner

how to Save value in Shared Preference and set in spinner


By : Rida Jahri
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I know this question has been asked many times but I don't understand how to set up because I am new in android. , you can save it like
code :
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putInt("Gender1", -1);
    editor.apply();
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    return sharedPreferences.getInt("Gender1", 0);
Save intent in shared preference

Save intent in shared preference


By : webstudiolund
Date : March 29 2020, 07:55 AM
wish of those help I have an application from which i can launch other apps installed on my phone, with a long click i get the app picker, in result i receive an intent data, how can i save it so the user when closes an comes back to my app has the same shortcuts setup? , Ok i found a way I save the intent like this
Related Posts Related Posts :
  • Can not scroll and click item on gridview inside scrollview
  • Android device keeps disconnecting from adb / eclipse
  • Volley cannot create cache directory. Could not clean up file
  • ShareActionProvider icon in actionbar
  • How to open a custom attachment using an Android app?
  • Unable to add column dynamically into the sqlite table
  • android persistence activity which never stop
  • The Google Play services resources were not found. Check your project configuration to ensure that the resources are inc
  • How to select files from google drive in android?
  • Run a AsyncTask every 10 seconds until cancelled
  • Blur on touch. Android application
  • NDK Android - Can't build ARMv5 and V7 at the same time
  • Android development with Eclipse clean and build
  • How to set Different Key Background for every Key in Android Custom Keyboard
  • Face recognition using OpenCV in android?
  • Tumblr API - Jumblr for Android [OAuthConnectionException]
  • Small Icon on Notification not working correctly
  • ConstraintLayout match parent height on larger screens but scroll on smaller
  • Kotlin version issue
  • How can i make my android app look the same on RTL and LTR languages?
  • Can not detect Android NDK toolchain
  • Falling back on PROMPT mode since _cordovaNative is missing. Expected for Android 3.2 and lower only.
  • Facebook Key Hash for Play Store Release
  • backbutton confirm exit app android + phonegap + jquery
  • How to Make Instagram Listview in android?
  • Up and Down Enemy Movement using translate corona sdk
  • Set selection method is not working in gallery in android
  • How to implement onOptionsItemSelected for Sub-Menu Items?
  • HttpHostConnectException connection refused ConnectException ErrnoException
  • Adding Buttons to expendableListView as last Child
  • Android. Block phone on startup
  • How to programmatically check If string is not to wide to fit onto the screen in Android
  • EditText box cursor appear after touching box?
  • using if statement when video is done playing
  • Add to Aspectpath not found MonkeyTalk
  • GooglePlusUtil cannot be resolved (Android)
  • Trouble understanding Relative Layout
  • How to title/name each fragment separately to appear on PagerTitleStrip?
  • Android webView blinks
  • Available service types in WifiP2pDnsSdServiceInfo.newInstance
  • Hide only bottom action bar Android
  • Android + MoPub + PhoneGap (Cordova) Integration (Half working)
  • Android OnTouchListener results in jerky drags
  • android notification disappears after reboot
  • Optimising Canny Edge Detector for Android
  • How to Play the online streaming radio in Android
  • Blurry map tiles at start of android app based on google Maps API v2
  • How to combine java native within the XML android and dart Flutter
  • Intercept the outgoing call data, alter it & then write it back out- Why it is not possible?- Android
  • Bundle extra sends wrong string
  • Display a view using windowmanager on lock screen
  • is it possible to load images from the directory on web server?
  • android SDK manager, only show installed package
  • How to handle time zone difference between server and native android application?
  • ViewPager PagerAdapter delete items
  • How to solve different Preview/Picture sizes returned by android.hardware.camera?
  • How to get songs from album/Artist in android,using MediametadataRetriever?
  • Android UIAutomator how to tilt device
  • DialogFragment issues with screen orientation and MediaController
  • blank screen after re-starting launcher activity (failed binder transaction)
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org