How to iterate through all keys of shared preferences?
What you can do is use getAll()
method of SharedPreferences
and get all the values in Map<String,?>
and then you can easily iterate through.
Map<String,?> keys = prefs.getAll();
for(Map.Entry<String,?> entry : keys.entrySet()){
Log.d("map values",entry.getKey() + ": " +
entry.getValue().toString());
}
For more you can check PrefUtil.java's
dump()
implementation.
How to iterate through all keys of shared preferences?
What you can do is use getAll()
method of SharedPreferences
and get all the values in Map<String,?>
and then you can easily iterate through.
Map<String,?> keys = prefs.getAll();
for(Map.Entry<String,?> entry : keys.entrySet()){
Log.d("map values",entry.getKey() + ": " +
entry.getValue().toString());
}
For more you can check PrefUtil.java's
dump()
implementation.
How can I Loop through data that is stored in my sharedpreference with a key in - Flutter
Here is an example for you to achieve your scenario..
var user;
var userData;
List anchors = [];
_getUserAnchor() async{
SharedPreferences localStorage = await SharedPreferences.getInstance();
user = userJson;
setState(() {
anchors = user['Anchors'];
});
print(anchors);
setState(() {
userData = anchors;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('test'),),
body: Column(
children: <Widget>[
ListView.builder(
shrinkWrap: true,
itemCount: anchors.length,
itemBuilder: (BuildContext context, int i){
return Padding(
padding: const EdgeInsets.all(8.0),
child: Card(
child: Column(
children: <Widget>[
Text(anchors[i]['Name']),
Text(anchors[i]['Oid'].toString()),
],
),
),
);
})
],
),
);
}
Here I didn't get data from shared preferences i have equal your json to userJson, U can use your same steps get the data from shared preferences and follow up, So you can build list of card's.. hope this will help you...
Looping through SharedPreferences using Map
Thanks to crgarridos I managed to see my mistake. It was actually quite a silly one. I had just forgotten to store a value into SharedPreferences. With this code in MySecondActivity, it is working:
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("First key", var1);
editor.putInt("Second key", var2);
editor.putInt("Third key", var3); //<- Hehe...
// Commit the edits!
editor.commit();
Get all SharedPreferences names and all their keys?
Get all the _preferences.xml from
/data/data/(package)/shared_prefs/(package)_preferences.xml
Parse the XML, in the XML there is name for every key. I might misunderstood your question
Related Topics
Eclipse, Android, Scala Made Easy But Still Does Not Work
Android - Play Sound on Button Click - Null Pointer Exception
Limit Height of Listview on Android
How to Update Notification with Remoteviews
How to Populate Android Room Database Table on First Run
How to Check How Much Free Space an Sd Card Mounted on an Android Device Has
Calling Startintentsenderforresult from Fragment (Android Billing V3)
Start a Service in a Separate Process Android
How to Implement Placeautocompletefragment and Placeautocompleteactivity to Get Place Details
How to Know Which Intent Is Selected in Intent.Action_Send
How to Avoid Delay in Android Gcm Messages/Change Heartbeat
Adjust Layout When Soft Keyboard Is On
Error:Unexpected Lock Protocol Found in Lock File. Expected 3, Found 0
Cordova Plugin Development - Adding Aar
Android How to Create Triangle and Rectangle Shape Programmatically
Android Google+ Integration - Repeated Userrecoverableauthexception