I want to store three values in shared preference
Can I store integer as third value in hashmap? As i passed null for string, what can i passed for integer?
public HashMap<String, String> getUserDetails(){
HashMap<String, String> user = new HashMap<String, String>();
// user name
user.put(KEY_NAME, pref.getString(KEY_NAME, null));
// user email id
user.put(KEY_PASS, pref.getString(KEY_PASS, null));
user.put(KEY_ID,pref.getInt(KEY_ID,));//what should i pass here??
// return user
return user;
}
you've declared your HashMap() as a map that contains String objects, so, no, you can't really just store an integer in there. However, you can convert the integer to a string if you like. Since getInt() returns a primitive int, you should use the static method Integer.toString(valuetomakeintoastring)
The second value in your call to getInt() is the default value that should be returned if the KEY value is not found in the preferences. Use some value that can't be confused as a valid value for your application.
user.put(KEY_ID,Integer.toString(pref.getInt(KEY_ID,<somedefaultintegervalue>));
Perhaps a HashMap is not the right data structure for you to use here? Perhaps you really need to define a class that contains these values together, then create a HashMap of that class?
Related
I have HashMap<Shop, String> shopMap where I put two values:
shopMap(shopModel1, shopModel1.getName());
shopMap(shopModel2, shopModel2.getName());
In my method for search shop by name I passed object of shop identical like shopModel1 to get his name:
public String getNameForShop(Shop filter) {
return shopMap.get(filter);
}
but I get null. Objects have the same all values. There is any way to get shop name from hash map using object?
It is not ok to have the object as key value. Map it by name.
I guess mapping by object, in fact, it happens with the object's memory address. Then you search in map for an object it doesn't compare objects fields, just the addresses.
HashMap<String, Shop> shopMap;
if( shopMap.get("shopname").equals(anotherShop) ){
//do staff
}
How to give user defined key value in push(), instead of unique value created by push?
This is what am currently doing:
User user = new User(Editname.getText().toString(),
Editpid.getText().toString(),Editsem.getText().toString());
mRef.child("users").push().setValue(user);
.push() will create a new item with a unique reference.
You can use updateChildren() to update instead. For example,
User user=new User(Editname.getText().toString(),Editpid.getText().toString(),Editsem.getText().toString());
Map<String, Object> itemValues = user.toMap();
Map<String, Object> childUpdates = new HashMap<>();
// Define the key value here
String username = "yourKeyValueHere";
childUpdates.put("/users/" + username, itemValues);
mDatabase.updateChildren(childUpdates);
You might have to add something similar to the following to your User class.
#Exclude
public Map<String, Object> toMap() {
HashMap<String, Object> result = new HashMap<>();
result.put("name", name);
result.put("pid", pid);
result.put("sem", sem);
return result;
}
Simplest way is to specify the child key with the child method:
User user = new User(Editname.getText().toString(),
Editpid.getText().toString(),Editsem.getText().toString());
mRef.child("users").child(user.pid).setValue(user);
Where I specify user.pid, you can use whatever unique key you use to identify the user (typically when using Firebase Authentication this would be user.getUid()).
This is one way to do it assuming we want to use the User's name as unique key:
User user = new User(Editname.getText().toString(),
Editpid.getText().toString(),Editsem.getText().toString());
String uniqueKey = user.getName();
//You could use something else for quick reference since two users can have the same name
mDatabaseReference.child("users").child(uniqueKey).push().setValue(user);
I know there's an answer to this, but i'm not sure what to search and cannot remember the right phrasing.
Essentially what i mean, i'm being passed an integer. I want to search through an array using this integer to return a string value.
E.g.
I'm given the number 2. My lookup array looks like so:
array.add(1, "Auckland")
array.add(2, "Wellington")
array.add(3, "Bay of Plenty")
When i iterate through the array, i want to return "Wellington".
Can someone point me in the right direction please? :)
Hi you should declare a HashMap of Integer String:
HashMap<Integer, String> map= new HashMap<Integer, String>();
map.put(1, "Auckland");
map.put(2, "Wellington");
map.put(3, "Bay of Plenty");
And then to get the String you should call the HashMap using the key:
String yourString = map.get(1);
I want the JSON string to be in same order how I am putting it.This is my query.
object.put("name", name);
object.put("email", email);
object.put("query", query);
But in the resultant string its showing as
{"email""m#gmail.com","query":"k","name":"a"}
The order of keys in a JS object is not guaranteed. If you need a particular order, consider having a separate array of keys to preserve the ordering.
{
"order":["name", "email", "query"],
"data":{
"email":"m#gmail.com",
"query":"k",
"name":"a"
}
}
From JSON specification http://www.ietf.org/rfc/rfc4627.txt:
An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array.
(emphasis mine)
I have an Android application which retrieves from a external server, a name and a corresponding ID (this could be 1 name and ID combo or multiple name ID combinations), these are all stored in a HashMap<String, String> - The ID as the key and the Name as the value.
What I then would like to implement is a dynamic Spinner that populates itself with the 'Names' from this HashMap and when the user selects one of these names a setting somewhere I set to the ID number. This ID number will then be used in later server requests.
My question is what is the best way to implement this custom Spinner from the HashMap so that when the option is selected the ID number is set somewhere. This ID number has to be accessible from several activities - the spinner is present in several different activities... but should have the same effect on each screen.
Design patterns and pseudo code would be hugely appreciated. At the moment the Spinner is on 2 different screens, at the top below the ActionBar, but obviously the code is in both XML layout files.
Cheers.
EDIT
Code to set names and IDs in HashMap:
// Returns a Map of blog name to blog ID associated with the authenticated user
public Map<String, String> extractBlogInfo(XMLDataExtracter blogData)
{
Log.d(TAG, "BlogOperations::extractBlogInfo()");
ArrayList<String> blogIDs = new ArrayList<String>();
ArrayList<String> blogNames = new ArrayList<String>();
Map<String, String> blogIDNamePairs = new HashMap<String, String>();
// Get ID and Names and store them in blogIDs and blogNames variables
if (blogIDs.size() == blogNames.size())
{
for(int i = 0; i < blogIDs.size(); i++)
{
blogIDNamePairs.put(blogIDs.get(i), blogNames.get(i));
}
}
else
{
// An error occured
Log.d(TAG,
"BlogOperations::extractBlogInfo() > An error occured - ID and Name array sizes do not match");
return null;
}
return blogIDNamePairs;
}
For this you can use Shared preference.. You get the Id of the selected value from spinner and stored it in shared preference..
So that you can access this value any where inside your Apps..
If you want to display the selected spinner value from Say Activity1 in Activity2 get the value from Shared preference, now it will be Id so get the corresponding Name from the Id in hash Map..
For shared preference take a look at this... and this....