sharedpreferences

Flutter: shared preferences

馋奶兔 提交于 2020-01-01 04:43:09
问题 I have this function: Future<String> load(SharedPreferences prefs, String fileName) async { prefs = await SharedPreferences.getInstance(); String jsonString = prefs.getString(fileName) ?? ""; if (jsonString.isNotEmpty) { return jsonString; }else{ return ... } } What should I return in the else case? I tried with "" but it doesn't work. 回答1: The answer is "it depends". Namely, it depends on what exactly you are doing with the result of this function, and what a good empty default value means

How to resolve an error: getSharedPreferences(String, int) is undefined for the type new View.OnClickListener(){}

只愿长相守 提交于 2020-01-01 00:43:33
问题 I'm getting this error in my coding and not entirely sure how to resolve this. I've searched to try and resolve this issue but can't seem to find anything that works. I've done this before but never in a fragment so maybe it could be because of that? I'm getting following exception : The method getSharedPreferences(String, int) is undefined for the type new View.OnClickListener(){} Here is my code: public class TestingFragment extends Fragment { public TestingFragment(){} private CheckBox ch;

How to store class object in android sharedPreference?

拈花ヽ惹草 提交于 2019-12-31 10:47:14
问题 I would like to store class object in android sharedpreference. I did some basic search on that and I got some answers like make it serializable object and store it but my need is so simple. I would like to store some user info like name, address, age and boolean value is active. I made one user class for that. public class User { private String name; private String address; private int age; private boolean isActive; public String getName() { return name; } public void setName(String name) {

How to force two digit after decimal

佐手、 提交于 2019-12-31 07:42:24
问题 I have the following TextView which displays $0.00 all the time unless any calculation is done after a button click and saved to this TextView. <TextView android:id="@+id/tvTotalAmount" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="$0.00" android:textStyle="bold" android:paddingTop="10dp" android:textColor="#00A21E" android:textSize="25dp" android:paddingLeft="30dp" android:layout_below="@id/tvTotal" /> <EditText android:id="@+id/etToll" android:layout

Passing SharedPreferences to doInBackground()

旧街凉风 提交于 2019-12-31 07:38:33
问题 I am trying to pass the SharedPreferences prefs, as an argument to doInBackground function in AsyncTask. I am already passing a string(url) to it, so I would need to pass the prefs also as a string. Can I simply use, prefs.toString() to convert it into string? This is where I set the prefs: if (prefs.getBoolean("firstrun", true)) { prefString = prefs.toString(); prefs.edit().putBoolean("firstrun", false).commit(); } 回答1: You can't and you shouldn't. You can easily read the preferences just

How to save/load BigInteger array

早过忘川 提交于 2019-12-31 07:34:09
问题 I want to save/load a BigInteger array into/from the SharedPreferences. How can it be done? For example for the following array: private BigInteger[] dataCreatedTimes = new BigInteger[20]; 回答1: Using Gson you can convert to a json String and back, which then of course makes it trivial to save in preferences: import com.google.gson.Gson; import java.math.BigInteger; public final class BigIntegerArrayJson { private BigIntegerArrayJson(){} public static String toJson(BigInteger[] array) { return

save state and value of radio button

笑着哭i 提交于 2019-12-31 05:25:07
问题 in the main activity i have radio buttons , in the method public void onRadioButtonClicked(View view) i associate each radio button selected with a value, so i have two variables a and b, after that i click on the button bton i have according to the method ajouter, a and b values into database.Until now everything is okey, i decide to use OnResume and OnPause methods to save the state of activity, so when i return to the activity i see that the radio buttons are selected, the problem comes

SharedPreferences return only default value

瘦欲@ 提交于 2019-12-31 01:34:13
问题 So instead of creating a database, I'm storing the data using SharedPreference . My code is below: SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit(); editor.putInt("favid"+id, 1); editor.commit(); Toast.makeText(getApplicationContext(), "Select as favorite", Toast.LENGTH_SHORT).show(); Now I want to retrieve that data so I have used below code in other activity : strFav = new ArrayList<Integer>(); if(strFav.size()>0) strFav.clear(); SharedPreferences prefs = getPreferences

SharedPreferences return only default value

安稳与你 提交于 2019-12-31 01:33:59
问题 So instead of creating a database, I'm storing the data using SharedPreference . My code is below: SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit(); editor.putInt("favid"+id, 1); editor.commit(); Toast.makeText(getApplicationContext(), "Select as favorite", Toast.LENGTH_SHORT).show(); Now I want to retrieve that data so I have used below code in other activity : strFav = new ArrayList<Integer>(); if(strFav.size()>0) strFav.clear(); SharedPreferences prefs = getPreferences

Flutter: Shared Preferences null on Startup

坚强是说给别人听的谎言 提交于 2019-12-30 18:06:43
问题 Problem: Shared preference bool value is null on startup even though I have given it a value if prefs.getBool('myBool') returns null (though my shared preferences value should already be set and saved). It does, however, work by the time I press a button (I assume because it has finished running the async code). Question: How can I force shared preferences to load on startup (so my value is not null ) without having to press the print button? Example Code: import 'package:flutter/material