I have this FacebookUser object i've created:
private String id,name, status;
private double distance = -1, longitude, latitude;
private transient Drawable profilePicture,gender;
private boolean isFacebookFriend = false, isApplicationFriend = false, isClicked = false, isOnline = false, isLoaderChecked=false;
public FacebookUser()
{
}
public FacebookUser(String id, String name) {
this.setId(id);
this.setName(name);
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
if (isApplicationFriend())
return name;
else
return "";
}
public void setName(String name) {
this.name = name;
}
public boolean isApplicationFriend() {
return isApplicationFriend;
}
public void setApplicationFriend(boolean isApplicationFriend) {
this.isApplicationFriend = isApplicationFriend;
}
}
My problem is with the getName() method.
When showing an ArrayList of FacebookUser in a ListView, i'm using FacebookUser.getName() to display the name in a TextView.
Now, i'm 100% sure that isApplicationFriend() return true - and i've even logged it near the TextView text insert.
So why does getName() keep returning "" ??
You're right you're right... i'm so silly...
The method was called before and emptied the name property...
Sorry...
Related
hey guys im using android's gson library to parse my JSON. everything is working fine in that I have that selected goal array sometimes it is full of values and sometimes it is an empty array. when array is empty I want to push an object init when I try to add its index
selected_goal.add(INDEX,VALUE)
I want to push an object at the place of value please help me how can I do it…
this is my pojo:
public class SelectedGoal {
#SerializedName("id")
#Expose
private String id;
#SerializedName("goal_name")
#Expose
private String goalName;
#SerializedName("image")
#Expose
private String image;
#SerializedName("position")
#Expose
private String position;
#SerializedName("athlete_id")
#Expose
private String athleteId;
#SerializedName("coach_id")
#Expose
private String coachId;
#SerializedName("current_goal_id")
#Expose
private String currentGoalId;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getGoalName() {
return goalName;
}
public void setGoalName(String goalName) {
this.goalName = goalName;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public String getAthleteId() {
return athleteId;
}
public void setAthleteId(String athleteId) {
this.athleteId = athleteId;
}
public String getCoachId() {
return coachId;
}
public void setCoachId(String coachId) {
this.coachId = coachId;
}
public String getCurrentGoalId() {
return currentGoalId;
}
public void setCurrentGoalId(String currentGoalId) {
this.currentGoalId = currentGoalId;
}
}
selected_goal.add(INDEX,new SelectedGoal());
EDIT- To set values, make constuctor in your SelectedGoal class like.
public SelectedGoal(String id, String goalName, String image, String
position, String athleteId, String coachId, String currentGoalId) {
this.id = id;
this.goalName = goalName;
this.image = image;
this.position = position;
this.athleteId = athleteId;
this.coachId = coachId;
this.currentGoalId = currentGoalId;
}
finally add your object into list like..
selected_goal.add(INDEX,new SelectedGoal(id,goalName,image,position,athleteId,coachId,currentGoalId));
This question already has answers here:
Why does Gson fromJson throw a JsonSyntaxException: Expected BEGIN_OBJECT but was BEGIN_ARRAY?
(2 answers)
GSON throwing "Expected BEGIN_OBJECT but was BEGIN_ARRAY"?
(11 answers)
Closed 4 years ago.
I have the following JSON structure:
[
{
"id":1,
"name":"car",
"elements":[
{
"id":1,
"name":"price",
"type":"textField",
"constraints":"blablabla"
},
{
"id":2,
"name":"color",
"type":"textField",
"constraints":"blablabla"
},
{
"id":3,
"name":"images",
"type":"image",
"constraints":"blablabla"
}
]
}
]
And I have the following models:
public class Product {
private Long id;
private String name;
#Expose
private Element elements;
public Product(Long id, String name, Element elements) {
this.id = id;
this.name = name;
this.elements = elements;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public Element getElements() {
return elements;
}
}
public class Element {
private Long id;
private String name;
private String type;
private String constraints;
public Element(Long id, String name, String constraints, String type) {
this.id = id;
this.type=type;
this.name = name;
this.constraints = constraints;
}
public String getType() {
return type;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public String getConstraints() {
return constraints;
}
}
The Elements model is which I have problems, I am getting the following error:
ERROR: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 35 path $[0].elements
How can I change the model to make it work? I tried to change the elements in Product class to JSONObject array but when I wanted to parse, it was empty.
You need to modify your POJOs like this:
Element.java
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Element {
#SerializedName("id")
#Expose
private Integer id;
#SerializedName("name")
#Expose
private String name;
#SerializedName("type")
#Expose
private String type;
#SerializedName("constraints")
#Expose
private String constraints;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getConstraints() {
return constraints;
}
public void setConstraints(String constraints) {
this.constraints = constraints;
}
}
Product.java
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Product {
#SerializedName("id")
#Expose
private Integer id;
#SerializedName("name")
#Expose
private String name;
#SerializedName("elements")
#Expose
private List<Element> elements = null;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Element> getElements() {
return elements;
}
public void setElements(List<Element> elements) {
this.elements = elements;
}
}
P.S: You should use 'http://www.jsonschema2pojo.org/' website for generating POJO automatically from the JSON string you provide to it.
I hope this helps.
I think you try to put an array of Elements into "private Element elements" which is a single object
Change your pojo with this. Also you can add your respective constructor which you require.
public class Element {
#SerializedName("id")
#Expose
private Integer id;
#SerializedName("name")
#Expose
private String name;
#SerializedName("type")
#Expose
private String type;
#SerializedName("constraints")
#Expose
private String constraints;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getConstraints() {
return constraints;
}
public void setConstraints(String constraints) {
this.constraints = constraints;
}
}
public class Product {
#SerializedName("id")
#Expose
private Integer id;
#SerializedName("name")
#Expose
private String name;
#SerializedName("elements")
#Expose
private List<Element> elements = null;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Element> getElements() {
return elements;
}
public void setElements(List<Element> elements) {
this.elements = elements;
}
}
in json you have list of Element and in the model you declared Element as an Object
change this private Element elements; to List of Element
Try this pojo or you can make from this link
private String id;
private String name;
private ArrayList<Elements> elements;
public String getId ()
{
return id;
}
public void setId (String id)
{
this.id = id;
}
public String getName ()
{
return name;
}
public void setName (String name)
{
this.name = name;
}
public ArrayList<Elements> getElements ()
{
return elements;
}
public void setElements (ArrayList<Elements> elements)
{
this.elements = elements;
}
#Override
public String toString()
{
return "ClassPojo [id = "+id+", name = "+name+", elements = "+elements+"]";
}
public class Elements
{
private String id;
private String name;
private String constraints;
private String type;
public String getId ()
{
return id;
}
public void setId (String id)
{
this.id = id;
}
public String getName ()
{
return name;
}
public void setName (String name)
{
this.name = name;
}
public String getConstraints ()
{
return constraints;
}
public void setConstraints (String constraints)
{
this.constraints = constraints;
}
public String getType ()
{
return type;
}
public void setType (String type)
{
this.type = type;
}
#Override
public String toString()
{
return "ClassPojo [id = "+id+", name = "+name+", constraints = "+constraints+",
type = "+type+"]";
}
}
I have a list specialties having 75 items that has two values id and name.
private List specialties;
I would like to get the name without using a loop something like below
specialties.get(0).name;
I get an error saying can't resolve name. Is there any way to retrieve name from the values list.
Thanks.
Create a class (Model) to get and set the ID and Name property:-
public class ClassName {
private String id;
private String name;
public ClassName(String mId, String mName){
this.id=mId;
this.name=mName;
}
public String getName() {
return name;
}
public void setName(String sName) {
this.name = sName;
}
public String getId() {
return id;
}
public void setId(String sId) {
this.id = sId;
}
}
In your Activity:-
Define a List having the ClassName type of objects.
List<ClassName> mList = new ArrayList<>();
Now access the property name like this:-
mList.get(0).getName();
mList.get(0).getId();
try the following:
public class Clone {
private int Id;
private String name;
public int getId() {
return Id;
}
public String getName() {
return name;
}
public void setId(int id) {
Id = id;
}
public void setName(String name) {
this.name = name;
}
}
and use
private List<Clone> arrayList = new ArrayList();
for (int i = 0; i < 10; i++) {
Clone helloItem = new Clone();
helloItem.setId(i);
helloItem.setName("I'm Clone no - " + i);
arrayList.add(helloItem);
}
Log.d("check", "get item - " + arrayList.get(0).getName());
hope it help.
I want to suggest you. You should declare name with public access. If it is not public, use public getter and setter method.Call getName() method.
public class YourClass{
private int id;
private String name;
public YourClass(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
private List<YourClass> specialties = new ArrayList<YourClass>;
...//add data into list...
specialties.add(new YourClass( 1 , "John"));
...// retrieve data
specialties.get(position).getName();
You have declared your list as private List specialties; you can not access it like this specialties.get(0).name;
You need declare your list like this private List<YourModelClass> specialties;
SAMPLE DEMO
If you want add model class in your list than than check this example
create model class like this
public class User {
String name, email;
public User(String name, String email) {
this.name = name;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Now use like this in your list
List<User> userArrayList= new ArrayList<>();
To add data inside list like this
userArrayList.add(new User("Nilesh","abc#gmail.com"));
To get data from your list use like this
userArrayList.get(0).getEmail();
userArrayList.get(0).getEmail();
or
for (int i=0;i<userArrayList.size();i++){
userArrayList.get(i).getName();
userArrayList.get(i).getEmail();
}
I have the following object:
#RealmClass
public class TripStop extends RealmObject implements Serializable {
#Nullable
private int arrival_time;
private String address;
#PrimaryKey
private int id;
#Nullable
private int departure_time;
private Destination place;
private CoordLocation location;
public int getArrival_time() {
return arrival_time;
}
public void setArrival_time(int arrival_time) {
this.arrival_time = arrival_time;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getDeparture_time() {
return departure_time;
}
public void setDeparture_time(int departure_time) {
this.departure_time = departure_time;
}
public Destination getPlace() {
return place;
}
public void setPlace(Destination place) {
this.place = place;
}
public CoordLocation getLocation() {
return location;
}
public void setLocation(CoordLocation location) {
this.location = location;
}
}
But everytime I get a json response back, with an int (or primitive) as null, I get an error in Realm that:
08-29 16:45:59.220: I/WSCalls(6370): error :Trying to set non-nullable field 'departure_time' to null.
I read that from Realm 3.5 it is possible to set null values, or set a default version, but I did not manage yet.
How is that possible? I see that the #Nullable does not help
First of all:
int is a primitive data type and cannot be assigend with null.
Solution:
You can change the type to Integer. An Integer can contain null, but it adds a little overhead because of boxing/unboxing.
(see: https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html)
I fecht a Json-String from my server:
{"erfolgreich":"true","id":"14"}
When I call
//result is the string above
msgServer = gson.fromJson(result, MsgSpielerErstellenSA.class);
The boolean is always false...
What am I doing wrong?
Here is my MsgSpielerErstellenSA
public class MsgSpielerErstellenSA {
private long id;
private boolean isErfolgreich;
public MsgSpielerErstellenSA(long id, boolean isErfolgreich) {
super();
this.id = id;
this.isErfolgreich = isErfolgreich;
}
public boolean isErfolgreich() {
return isErfolgreich;
}
public void setErfolgreich(boolean isErfolgreich) {
this.isErfolgreich = isErfolgreich;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
Because the correct name for the boolean field is erfolgreich, not isErfolgreich. Please use the following class:
public class MsgSpielerErstellenSA {
private long id;
private boolean erfolgreich;
public MsgSpielerErstellenSA(long id, boolean isErfolgreich) {
this.id = id;
this.erfolgreich = isErfolgreich;
}
public boolean isErfolgreich() {
return erfolgreich;
}
public void setErfolgreich(boolean isErfolgreich) {
this.erfolgreich = isErfolgreich;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
But if you don't want to rename this field, you can use #SerializedName("erfolgreich") annotation on it
The name of your key "erfolgreich" in json string should be same as your class data member "isErfolgreich" or you should use #SerializedName notation before define your member. if gson can not match between a class member and json keys, then use the default value for that member type. so you can use the nikis solution or you can use notation like this:
#SerializedName("erfolgreich")
private boolean isErfolgreich;