I have this model -
public class ItemsModel extends RealmObject {
private long id;
private String itemName;
private int itemCode;
private int itemPrice;
private int itemImage;
private int itemStock;
private int itemAmount;
private int discount;
private int total;
private boolean isSelected = false;
private int seller;
public ItemsModel() {
}
and this model -
public class CartModel extends RealmObject {
#PrimaryKey
private int cartId;
private RealmList<ItemsModel> itemsModels;
public CartModel() {
}
I saves items in ItemsModel and want to save the items selected in realmlist inside the CartModel, but there is some problems with that -
even when I'm trying to change the id before - it inserted to the main model(ItemsModel) and not to the ItemsList in the CartModel -
ItemsModel i = new ItemsModel();
i.setId(itemsModelRealmList.size());
i.setSeller(registerModels.getRegisterMainSeller());
i.setItemCode(item.getItemCode());
i.setItemPrice(item.getItemPrice());
i.setItemImage(item.getItemImage());
i.setItemName(item.getItemName());
realm.beginTransaction();
realm.where(CartModel.class).findFirst().getItemsModels().add(i);
realm.commitTransaction();
if I'm trying to insert to the list inside the cart the same object from ItemsModel - the recyclerview act like they are one object.
thanks
While using android Room i'm having the following entity:
#Entity
public class Call implements Parcelable {
#PrimaryKey(autoGenerate = true)
private long id;
private String filePath;
private long durationInMillis;
private String phoneNumber;
private int isStarred;
private int isIncoming;
private long timestampCreated;
}
All works great. But now I want my pojo (Call.class) to extends an Abstract class as following:
#Entity
public class Call extends BaseViewTypeData implements Parcelable {
....
....
}
And i'm getting the following error:
Error:Cannot figure out how to save this field into database. You can
consider adding a type converter for it.
Error:Cannot find getter for field.
Error:Cannot find setter for field.
Error:Cannot figure out how to read this field from a cursor.
Error:Cannot find getter for field.
Error:Cannot find setter for field.
The parent (BaseViewTypeData.class) is a simple class to handle multiple view types in a recycler views.
public abstract class BaseViewTypeData extends BaseObservable {
public static final int VIEW_TYPE_CALL = 0;
public static final int VIEW_TYPE_SETTINGS_HEADER = 1;
public static final int VIEW_TYPE_SETTINGS_TITLE_SUBTITLE = 2;
public static final int VIEW_TYPE_SETTINGS_TITLE_SUBTITLE_SWITCH = 3;
public static final int VIEW_TYPE_SETTINGS_DIVIDER = 4;
public static final int VIEW_TYPE_SETTINGS_TITLE_SWITCH = 5;
public static final int VIEW_TYPE_CALL_LOG_DATA = 6;
public static final int VIEW_TYPE_CHECKBOX_TITLE_SUBTITLE = 7;
#Ignore
public abstract int getViewType();
}
The parent (BaseViewTypeData.class) is a simple class to handle multiple view types in a recycler views.
I suspect that your problem is not with BaseViewTypeData, but with BaseObservable, as Room will not know how to deal with the BaseObservable fields.
In general, having your entity inherit from classes that you do not control is unlikely to work.
I am designing an "advanced filter" functionality for my Android app where I can multi-select several query parameters, depending on the parameter type sometimes it will be a union, sometimes an intersection, but always on one object type (with some nested objects).
Would anyone know of an elegant and generic way to design such a query builder?
My object looks like this:
public class DBDocument extends RealmObject {
#PrimaryKey
private int _id;
private String updatedAt;
private String name;
private String subject;
private int type;
private int senderId;
private String dateSent;
private int paymentStatus;
private String paymentDue;
private double invoiceAmount;
private String currency;
private String acknowledged;
private String fetched;
private String exported;
private int recipient;
private int addressId;
private String printed;
private boolean important;
private boolean hidden;
private int printMode;
private int printRule;
private RealmList<DBTag> tag = new RealmList<>();
private RealmList<DBComment> comment = new RealmList<>();
private RealmList<DBEvent> event = new RealmList<>();
...
}
Problem
Why some constants are under the public modifier while some other private? Are those under public can be called from applications that use the library? If so, how to call the constant from an app, is it like this: CertainLibraryClass.ActivityResultCode.CODE_A?
Code
public class CertainLibraryClass {
public class ActivityResultCode {
public static final int CODE_A = 0X02;
public static final int CODE_B = 0X03;
public static final int CODE_C = 0X04;
}
public class VersionCode {
private static final int VERSION_MAJOR = 1;
private static final int VERSION_MINOR1 = 0;
private static final int VERSION_MINOR2 = 2;
}
// ....
}
Why some constants are under the public modifier?
Ans: So that all other classes can access it e.g.RESULT_OK,SUCCESS.
Why some constants are under the private modifier?
Ans:So that only that class can access it
e.g. consider you are calling getId() libarary function from your class
public class CertainLibraryClass {
private static int ID=0;
public static int getId()
{
return ID+1;
}
here you are not accessing ID field directly ,instead you are calling getId() function which ultimately returns the id, it means that ID variable is internally used by CertainLibraryClass class
i just started to android prograamming and found nice tutorial by using imdb api. instead of using xml in this tutorial i would like to use json and for the recevied json i have a problem.
this is the person.json:
[
{
"score":1,
"popularity":3,
"name":"Brad Pitt",
"id":287,
"biography":"test",
"url":"http://www.themoviedb.org/person/287",
"profile":[
{
"image":{
"type":"profile",
"size":"thumb",
"height":68,
"width":45,
"url":"http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w45/w8zJQuN7tzlm6FY9mfGKihxp3Cb.jpg",
"id":"4ea5cb8c2c0588394800006f"
}
},
{
"image":{
"type":"profile",
"size":"profile",
"height":281,
"width":185,
"url":"http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w185/w8zJQuN7tzlm6FY9mfGKihxp3Cb.jpg",
"id":"4ea5cb8c2c0588394800006f"
}
},
{
"image":{
"type":"profile",
"size":"h632",
"height":632,
"width":416,
"url":"http://d3gtl9l2a4fn1j.cloudfront.net/t/p/h632/w8zJQuN7tzlm6FY9mfGKihxp3Cb.jpg",
"id":"4ea5cb8c2c0588394800006f"
}
},
{
"image":{
"type":"profile",
"size":"original",
"height":1969,
"width":1295,
"url":"http://d3gtl9l2a4fn1j.cloudfront.net/t/p/original/w8zJQuN7tzlm6FY9mfGKihxp3Cb.jpg",
"id":"4ea5cb8c2c0588394800006f"
}
}
],
"version":685,
"last_modified_at":"2013-02-16 07:11:15 UTC"
}
]
my two object for them:
public class Person implements Serializable {
private static final long serialVersionUID = 6794898677027141412L;
public String score;
public String popularity;
public String name;
public String id;
public String biography;
public String url;
public String version;
public String lastModifiedAt;
public Profile profile;
}
public class Profile implements Serializable {
private static final long serialVersionUID = -8735669377345509929L;
public ArrayList<Image> imagesList;
}
public class Image implements Serializable {
private static final long serialVersionUID = -2428562977284114465L;
public String type;
public String url;
public String size;
public int width;
public int height;
}
ı couldnt figure out how to retrieve person list by using jackson object mapper.
when i use this one:
ObjectMapper mapper = new ObjectMapper();
Person person= mapper.readValue(jsonResponseString, Person.class);
i got:
02-16 18:34:48.010: W/System.err(376): com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.example.imdbsearcher.model.Person out of START_ARRAY token
02-16 18:34:48.180: W/System.err(376): at [Source: java.io.StringReader#40a81778; line: 1, column: 1]
02-16 18:34:48.554: W/System.err(376): at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:599)
02-16 18:34:48.830: W/System.err(376): at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:593)
i have changed the retrieve method with advice of Keith and crdnilfan.
but now i have a problem with the attribute of profile.
i realized that i am missing that one in person object and basicly i have created new profile object and moved imageList to this class.
i have updated POJO's as above.
but now i am getting the same error for the profile.
Can not deserialize instance of com.example.imdbsearcher.model.Profile out of START_ARRAY token
You need to deserialize the list, as your JSON is an array:
List<Person> people = mapper.readValue(
jsonResponseString, new TypeReference<List<Person >>(){});
However, after you do that you will have some additional deserialization errors because of the profile property in your JSON. Checkout: http://jackson.codehaus.org/1.5.7/javadoc/org/codehaus/jackson/annotate/JsonIgnoreProperties.html
Update:
public class Person implements Serializable
{
public List<Object> profile;
public String score;
public String popularity;
public String name;
public String id;
public String biography;
public String url;
public String version;
public String last_modified_at;
}
There are several ways to deal with this. This will give you a linked hash map for Profile.
Alternatively, if you control the JSON format, change the profile syntax to this:
"profile":[
image:...,
image:...
]
That's because you are trying to deserialize a list of Person.class, not one instance. Create another class like this
public class PersonList extends ArrayList<Person> {}
and then use
ArrayList<Person> people = mapper.readValue(jsonResponseString, PersonList.class);
The other two answers clearly explain the reason for the error, it would get rid off the error and it will parse Person object. But for me it failed to parse image objects. With below POJO we can parse the specified json completely, without any issues even in the absence of
#JsonIgnoreProperties
or
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
POJO definition:
public class PersonList extends ArrayList<Person> {}
public class Person implements Serializable {
private static final long serialVersionUID = 6794898677027141412L;
public String score;
public String popularity;
public String name;
public String id;
public String biography;
public String url;
public String version;
#JsonProperty(value="last_modified_at")
public String lastModifiedAt;
public List<Profile> profile;
}
public class Profile implements Serializable {
private static final long serialVersionUID = -8735669377345509929L;
#JsonProperty(value="image")
public Image imagesList;
}
public class Image implements Serializable {
private static final long serialVersionUID = -2428562977284114465L;
public String id;
public String type;
public String url;
public String size;
public int width;
public int height;
}
This should parse the json
String jsonResponseString = readJsonFile("person.json");
ObjectMapper mapper = new ObjectMapper();
PersonList person= mapper.readValue(jsonResponseString, PersonList.class);
or
List<Person> person= mapper.readValue(jsonResponseString,
new TypeReference<List< Person>>(){}); //if we use this we dont have to define PersonList class