Search code examples
androidgsonadapterpojo

Cannot access the getter within POJO class


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.


Solution

  • 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.