I have a pretty simple Enum that I am using throughout my application for Gender and I need to have a settings page where the user can change it, but I can't find any way to use an Enum in a ListPreference. I am using a PreferenceFragment to build the Preferences Screen, but I can't find a way to use the Enum I have as the entries and entryValues for the ListPreference.
The enum is very simple, just...
public enum Gender implements EnumSpinner {
MALE("Male", 0),
FEMALE("Female", 1),
NONE("Prefer not to say", -1);
private static SparceArray<Gender> genderMap = new SparceArray<>();
static {
for(Gender gender : Gender.values()) {
genderMap.put(gender.getValue(), gender);
}
}
private String name;
private int value;
Gender(String name, int value) {
this.name = name;
this.value = value;
}
public static Gender getGender(int val) { return genderMap.get(val); }
#Override
public String toString() { return name; }
public String getName() { return name; }
public int getValue() { return value; }
}
Is it possible to use in a ListPreference or am I going to have to create 2 string arrays in the strings.xml file and just use it that way?
SideNote, EnumSpinner is just an interface that allows me to use the enum in a spinner.
Please check in the google developers youtube channel, Android Performance Matters there is a video explaining why you would prefer not using enums in Android, there are other ways to get the same functionality.
Android has a Framework for Settings/Preferences, you can find an excellent tutorial here.
Buuilding Settings Screen Android
Related
I created an object to send some data to firebase. As an example, I use firebase user example:
public class User {
public String username;
public String email;
public User() {
// Default constructor required for calls to DataSnapshot.getValue(User.class)
}
public User(String username, String email) {
this.username = username;
this.email = email;
}
}
I want to encode property names that are sent to firebase. Currently keys are sent using variable names. I want to encode keys something like Useraname and Email, like Gson is doing. I don't want to change variable names.
#SerializateName("Username")
public String username;
#SerializateName("Username")
public String email;
I used #SerializateName(), but is not working. Same with #PropertyName that is used by Firebse, is not working. What I can use in order to serializare custom keys?
Update 1
public class Pojo {
#PropertyName("Guid")
public String guid;
#PropertyName("Name")
public String name;
public String getPojoGuid() {
return guid;
}
public void setPojoGuid(String guid) {
this.guid = guid;
}
}
As you can see in the image, it saves keys based on variable names. I changed property name from annotation for one field and when i save it, it ignores it, but when i change variable name, it save as new entry with key for that new varialbe name.
In this documentation is a method toMap(). If i do like that, is working (is not convenient for me), but is not working with #PropertyName.
Update 2
If i mark getters and setters with #Exclude and class with #IgnoreExtraProperties is working. I don't have to use toMap() method example from documetation. Is using specified name from #PropertyName. Not a good thing in my opinion, create confuses.
The Firebase SDK uses the annotation it finds for the property whenever it gets or sets its value. That means you need to consider how Firebase gets/sets the value, and annotate each place it looks.
Since you're declaring a getter method, Firebase will use that to get the value of the property. It will use the field for setting the value. So the annotation needs to be on both:
public class Pojo {
#PropertyName("Guid")
public String guid;
#PropertyName("Name")
public String name;
#PropertyName("Guid")
public String getPojoGuid() {
return guid;
}
#PropertyName("Guid")
public void setPojoGuid(String guid) {
this.guid = guid;
}
}
If you'd have getters and setters, the annotation would need to be on those, but not on the fields anymore:
public class Pojo {
private String guid;
private String name;
#PropertyName("Guid")
public String getPojoGuid() {
return guid;
}
#PropertyName("Guid")
public void setPojoGuid(String value) {
guid = value;
}
#PropertyName("Name")
public void setPojoGuid(String guid) {
this.guid = guid;
}
#PropertyName("Name")
public void setPojoGuid(String value) {
name = value;
}
}
What you are looking for is the feature of SDK Version 9.2 in which you can now use a new #PropertyName attribute to specify the name to use when serializing a field from a Java model class to the database. This replaces the #JsonProperty attribute.
#PropertyName("Username")
public String username;
#PropertyName("Email")
public String email;
See also this post in which Frank van Puffelen explains very clearly this concept.
#PropertyName :
Marks a field to be renamed when serialized. link
you have to use #PropertyName with public fields and no need for getters/setters
I saw the video(https://www.youtube.com/watch?v=Hzs6OBcvNQE) posted from google about price of enum and I'm convinced that enum cost more and has performance issue.
However, what about when I need to contain multiple information in an enum? Do I have to create intdef and stringdef to map the message?
Ie.
public enum Error{
NETWORK(1, "Network error!"),
STACK_OVER_FLOW(2, "Stack over flow error!");
final int mValue;
final String mMessage;
Error(int value, String message){
mValue = value;
mMessage = message;
}
public void getMessage(){
return mMessage;
}
public void getValue(){
return mValue
}
}
It is true that enums have that memory footprint, but just if are heavily using them, or sub using them, like comparisions or else, but if you're using such functionality like the one in your code, it's more than justified to work with them, in case you want to totally avoid enums, you can change this to a class.
I am using eclipse to create a app engine based application where I am also using app engine datastore(using JPA) using endpoints.
I wrote a test application with help from
https://developers.google.com/eclipse/docs/endpoints-addentities
and it went fine.
Now I want to create an entity where I define my own Key rather than it being automatically assigned by the system. Can someone help me as to what do I need to do that in my Notes.java entity class. By default the Notes.java like this.
package com.bfp.mypackage;
import javax.persistence.Entity;
import javax.persistence.Id;
#Entity
public class Note {
#Id
private String id;
private String emailAddress;
private String description;
public Note() {
}
public String getId() {
return id;
}
public String getDescription() {
return description;
}
public String getEmailAddress() {
return emailAddress;
}
public void setId(String idIn) {
this.id = idIn;
}
public void setDescription(String description) {
this.description = description;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
}
When I test this code I see the Note entity created in my dev app server admin console with the values(an emailAddress, a dsescription and an ID(I pass timestamp there)) I had supplied. I see two other fields created when I view the entity in admin console. One "Key" and other "Write ops". Now the "Key" field as I understand is the one generated automatically by java. Is there anyway I can set my emailAddress field as the Key? If so then what changes I need to do in above code. That way I can use emailAddress as the unique key for each entity.
You must set your email address as the "Key" name : a key is defined by either an auto-incremented long ID, or a user assigned key name that is unique for an entity kind.
I'm not the most experimented using JPA since I've used mostly JDO and the low level approach, but you might have to use directly the com.google.appengine.api.datastore.Key object instead of a java.lang.String attribute...
The low level API method is :
KeyFactory.createKey("EntityName", "whatever#domain.com");
I have a project that I have to design an array of coins and work with it. My GUI looks like this - http://i.imgur.com/eRzN3Sb.png
I want to be able to load the appropriate image from the coinArray for each coin. basically i want to be able to say coinView.setBackgroundResource(coinArray[x].image) i assume i need to somehow use a drawable object and i was hoping its possible to include it in my enum class. the enum class looks like
public enum Currency {
Penny(1), Nickel(5), Dime(10), Quarter(25);
private int value;
private Currency(int value) {
this.value = value;
}
}
Each coin in the array has a currency value so i can compute them. I'd like to add a drawable or some other object that will allow me to refernce the correct image for each coin.
Thank you
public enum Currency {
Penny(1,R.drawable.xxx), Nickel(5,R.drawable.yyy),...;
private int value;
private int image
private Currency(int value,int drawableId) {
this.value = value;
this.image=drawableId;
}
public int getImage(){
return image;
}
}
There are many ways you can do this. This is one of them. to use it:
coinView.setImageResource(coinArray[x].getImage());
I have getFilmsBrowse function and it should return the following field:
name (String)
description (String)
year (Int)
etc.
Results of this function to be used in another function displayMovies.
How can I pass them and be able to use names of fields in displayMovies?
Wanted to use Map for the same, but it is not clear for me how to initialize that.
You could use a collection, but why not create a class called Film, with private member variables called name, description, year etc, and then accessor methods like getYear(). Then you can do this:
Film film = getFilmsBrowse(...);
int year = film.getYear();
You can use Bundle class for this. It's like a Map, but it can contain values of different types.
Create some model classes which will hold data:
public class Page implements Serializable {
private String name;
private String description;
//and so on...
public Page(String name, String description) {
this.name = name;
this.description = description;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
}
Now you can create a Page object and fill it with data(name, description) via the constructor. Optionally make some setters.
Page p = new Page("James", "Hello World");
startActivity(new Intent(context, MyActivity.class).putExtra("Page", p));
Retrieve your Page in MyActivity in its onCreate method:
Page p = (Page)getIntent().getExtras().getSerializable("Page");
Toast.makeText(this, "Name: " + p.getName() + ", Description:" + p.getDescription(), Toast.LENGTH_LONG).show();
If you're moving from Activity to Activity you will use Intents and Bundle to pass parameters. If you're calling a function and reutrning to your original activity, then you will use conventional java