I am trying to access a getter method which is inside a POJO class within my response modal.
public class Event implements Parcelable{
class Name {
#SerializedName("text")
#Expose
public String eventName;
public String getEventName() {
return eventName;
}
}
.
. some parcelable stuff here
.
}
I am trying to access the getEventName method from within my adapter class. my piece of code there goes like this ( cant access the method, geteventname):
holder.cardTextView.setText(eventsList.get(position).getEventName());
If i define another variable outside of an inner pojo class, i can reach its getter, i can only not reach the one within the pojo class.
Edit
I am trying to read a json response like this, the text under name is the one im trying to build the modal for.
"events": [
{
"name": {
"text": "textextextext",
"html": "textextextext"
},
"description": {}
.
.
.
Thank you in advance.
JSON nesting is fun.
name is an inner class. Looks like description is also.
events":[ { "name":{ "text":" text ", "html":"something " }, "description":{ }, ... }]
In the above, there is an array of events.
Every event has a name, and every name has a "text" and an "html".
Every event also has a description, which has it's own fields.
You are on the right track:
public class Event implements Parcelable{
#SerializedName("name")
#Expose
public Name name;
#SerializedName("description")
#Expose
public Description description;
public class Name {
#SerializedName("text")
#Expose
public String eventName;
public String getEventName() {
return eventName;
}
}
public class Description {
//whatever fields are in the description object in the json
}
.
. some parcelable stuff here
.
}
You would access it like: holder.cardTextView.setText(eventsList.get(position).name.getEventName());
If this works you can clean it up by adding custom getters to the event class.
holder.cardTextView.setText(eventsList.get(position).getEventName());
try this. use parentheses for function call
Try this getEventName() with parentheses :
holder.cardTextView.setText(eventsList.get(position).getEventName());
Related
My firestore document I'm trying to serialize contains a map of maps of strings like this:
Some example document in my collection:
id: "someId1" (String)
vouchers (Map)
voucher_with_some_random_id_1 (Map)
name: "name 1"
description: "description 1"
voucher_with_some_random_id_2 (Map)
name: "name 2"
description: "description 2"
The problem I have with creating a matching POJO class is that the field names of the inner maps are not constant since the amount of vouchers in my vouchers Map changes from time to time.
For documents with unknown custom IDs there is a solution (Annotation #DocumentId)
But I couldn't find an annotation that works for maps.
My latest failed attempt:
public class MyPOJO {
public MyPOJO() {
}
public String id;
public VouchersPOJO vouchers;
}
public class VouchersPOJO {
public VouchersPOJO() {
}
public List<ActualVoucherPOJO> vouchers;
}
public class ActualVoucherPOJO {
public ActualVoucherPOJO() {
}
public String name, description;
}
MyPOJO x = documentSnapshot.toObject(MyPOJO.class);
As you already say voucher is a Map, so that's what you should use in your Java class too:
public class MyPOJO {
public String id;
public Map<String,Voucher> vouchers;
}
public class Voucher {
public String name, description;
}
In this case, I don't think toObject() is going to work on the entire data structure. It's not meant for handling dynamic/variable data. Instead, you will have to manually:
Iterate the entries of the Map of Maps that come back from the snapshot
Convert each map to the individual objects
Assemble the final MyPOJO object using what you found in the maps
I am trying to structure my databases, and I was wondering if it's possible to deserialize an object from Firebase RealDatabase with the following structure.
public class Profile{
private String userID;
private TenantProfile tenant;
}
public class TenantProfile{
private String name;
private Room room;
}
public class Room{
private String town;
private int size;
}
Can the entire object Profile from Firebase be deserialized from a single query?
I just want to make sure that this is possible before refactoring my code and changing the references to the database.
Thanks in advance.
The Firebase database serializes/deserializes any public fields, and public properties that follow JavaBean naming conventions for getters and setters. Since the classes you show contain neither of those, they will not read or write any data.
If you mark the fields as public or add public getters/setters, then writing an instance of the Profile class will generate this JSON:
"userID": {
"tenant": {
"name": "the name",
"room": {
"town": "the town",
"size": 42
}
}
}
And that's also the format you'll need to have in the database to read the value back.
Hi all I can't think of a better example to illustrate my point so do let me know If my example has some errors. But hopefully this example will get my point through.
class A {
String CATEGORY = "A";
public String getCATEGORY() {
return CATEGORY;
}
}
class B extends A {
String CATEGORY = "B";
#Override
public String getCATEGORY() {
return CATEGORY;
}
}
class C extends A {
String CATEGORY = "C";
#Override
public String getCATEGORY() {
return CATEGORY;
}
}
public class MyClass {
private List<A> array = Arrays.asList(new A(), new B(), new C());
public MyClass() {}
}
Now if I upload MyClass onto firebase using setValue for example, firebase will show me the properties of class A, B and C. However, when I read the data from firebase and call sth like getValue(MyClass.class) the List it returns me are all of type A and the subclasses are not preserved. Is there a workaround to allow firebase to preserve the class types uploaded?
If you use Firebase's default serializer, it simply writes all public properties and fields to the database. Say that you store a single instance of each class, it'd be:
-L1234567890: {
cATEGORY: "A"
},
-L1234567891: {
cATEGORY: "B"
},
-L1234567892: {
cATEGORY: "C"
},
There won't be enough knowledge in the database for the SDK to reinflate the correct sub-class. While you and I can see that the cATEGORY value matches the class name, the Firebase SDK has no such knowledge.
It won't be too hard to write your own custom deserializer for this data though, taking a DataSnapshot with the values above and reinflating the correct class and values.
You could also do a hybrid: detect the class type directly, and then tell Firebase what class to read:
String cat = snapshot.child("cATEGORY").getValue(String.class)
Class clazz = "C".equals(cat) ? C.class : "B".equals(cat) ? B.class : A.clas;
A object = snapshot.getValue(clazz);
I have a problem pretty much the same as this: retrofit returning valid json but pojo is empty
But my variables are not declared as static. The are all declared like:
#SerializedName("name")
#Expose
private String name;
I have tried removing the annotations, but that doesn't work.
what could be the problem?
EDIT:
Interface:
#GET("/MyController/MyAction/{name}")
void getSomeData(#Path("name") String name, Callback<List<DataItem>> cb);
Can you show me the actual received data(JSON or XML)? It seems that your callback structure is not matching with your data. For example, it would be possible that your data may have array that have a name, and you ignored it.
In my case, I declared like this,
void getList(#Path("data") String data,//
Callback<OrderList> callback);
OrderList is:
public class OrderList {
List<Order> order_list;
}
And my data is:
{
"order_list":
[
{ "id": "1001", "data": "a" },
{ "id": "1002", "data": "b" }
]
}
I mean, it seems that your data may have nested structure and your class may not matching with that.
I have a JSON string that contains a nested json like
{
"name": "name",
...
...
"profile": {
"id": 987,
"first_name": "first name"
...
...
}
}
I'm trying to map this JSON into Realm by using the method realm.createObjectFromJson(Class clazz, String string) and the problem is that the nested JSON is not mapped, the resulting RealmObject instance that corresponds to the "profile" has 0's and null's for all the fields. I used realm.beginTransaction() before the create operation, and realm.commitTransaction() after.
I'm using 'io.realm:realm-android:0.80.1' for my Android project.
Can you please tell me what am I doing wrong?
Thanks.
EDIT
These are my model classes. Simple RealmObjects linked together
public class SomeClass extends RealmObject {
private String name;
private Profile profile;
public Profile getProfile() {
return profile;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name= name;
}
}
public class Profile extends RealmObject {
private String firstName;
private String lastName;
private String birthdate;
private boolean newsLetter;
private boolean push;
private int userId;
private Date lastUpdate;
private RealmList<RealmAddress> addresses;
private RealmList<RealmGender> genders;
}
the profile class contains only getters and setters, and it contains other Strings and ints, which I deleted for the sake of simplicity.
Your JSON names doesn't match your child object field names which is why you don't see any data. Your profile name matches the field in SomeClass, which means the object gets created (with default values), but as none of the fields match in Profile, none of them are set.
firstName != first_name
userId != id
If you want to have separate names in your JSON and the Java models you should use something like GSON (http://realm.io/docs/java/#gson) as that is not yet supported by Realm directly.
use this :
public class Profile extends RealmObject {
private String first_name;
private int id;
...
}
check that you have the same names in JSON and your class model