why this change signatures of java class - java

I have Fragments call CategoryFragments where I am adding a list name catList for that I define the variable in Class name CategoryMODEL and variable are
public class CategoryModel {
private String docID;
private String name;
private int noOfTests;
public CategoryModel() {
this.docID = docID;
this.name = name;
this.noOfTests = noOfTests;
}
public String getDocID() {
return docID;
}
public void setDocID(String docID) {
this.docID = docID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNoOfTests() {
return noOfTests;
}
public void setNoOfTests(int noOfTests) {
this.noOfTests = noOfTests;
}
}
but when I try to use add item in my catLIst I get error that Change Signature of CategoryModel

Because you have an empty constructor but inside your are init the values with themself, so create a constructor with params:
public CategoryModel(String docId, String name, int noOfTests) {
// Init your scope variables
}

Related

(Parse-SDK) How to access List<ParseObject> from another ParseObject?

I'm using ParseSDK for android project. But I have a problem to access List of ParseObjects from another ParseObject class. I tried a lot of things, but anything didn't help me. Below I put my code.
Team team;
List<User> members = team.getMembers();
for(User user : members) {
user.getName();
}
#ParseClassName("Team")
public class Team extends ParseObject {
public static class Constant {
private static final String CREATED_BY = "createdBy";
private static final String NAME = "name";
public static final String CODE = "code";
private static final String PARTICIPANTS = "participants";
public static final String IS_ACTIVE = "isActive";
}
private String name;
private String code;
private User createdBy;
private List<User> members;
private int isActive;
public String getName() {
return getString(Constant.NAME);
}
public void setName(String name) {
this.name = name;
put(Constant.NAME, name);
}
public String getCode() {
return getString(Constant.CODE);
}
public void setCode(String code) {
this.code = code;
put(Constant.CODE, code);
}
public User getCreatedBy() {
return (User) getParseUser(Constant.CREATED_BY);
}
public void setCreatedBy(User createdBy) {
this.createdBy = createdBy;
put(Constant.CREATED_BY, createdBy);
}
public List<User> getMembers() {
return getList(Constant.PARTICIPANTS);
}
public void setMembers(List<User> members) {
this.members = members;
put(Constant.PARTICIPANTS, members);
}
public int getIsActive() {
return getInt(Constant.IS_ACTIVE);
}
public void setIsActive(int isActive) {
this.isActive = isActive;
put(Constant.IS_ACTIVE, isActive);
}
#ParseClassName("_User")
public class User extends ParseUser {
private static class Constant {
private static final String NAME = "name";
private static final String GENDER = "gender";
private static final String BIRTHDATE = "birthdate";
private static final String FACEBOOK_ID = "facebookId";
private static final String AVATAR = "avatar";
private static final String WEIGHT = "weight";
private static final String WEIGHT_UNIT = "weightUnit";
private static final String EXPERIENCE_LEVEL = "experienceLevel";
private static final String GOALS = "goals";
private static final String SCORE = "score";
private static final String IS_PREMIUM = "isPremium";
private static final String IS_TRIAL_PERIOD = "isTrialPeriod";
private static final String TOTAL_WORKOUT_BUILDS = "totalWorkoutBuilds";
}
private String name;
private String gender;
private String facebookId;
private Date birthday;
private ParseFile avatar;
private int weight;
private String weighUnit;
private String experienceLevel;
private List<Goal> goals;
private int score;
private int isPremium;
private int isTrialPeriod;
private int totalWorkoutBuilds;
private String emailAddress;
public User() {
super();
}
public String getName() {
return validateStringResult(Constant.NAME);
}
public void setName(String name) {
put(Constant.NAME, name);
}
public String getGender() {
return validateStringResult(Constant.GENDER);
}
public void setGender(String gender) {
put(Constant.GENDER, gender);
}
public Date getBirthday() {
return getDate(Constant.BIRTHDATE);
}
public void setBirthday(Date birthday) {
put(Constant.BIRTHDATE, birthday);
}
public String getFacebookId() {
return validateStringResult(Constant.FACEBOOK_ID);
}
public void setFacebookId(String facebookId) {
put(Constant.FACEBOOK_ID, facebookId);
}
public ParseFile getAvatar() {
return getParseFile(Constant.AVATAR);
}
public void setAvatar(ParseFile avatar) {
put(Constant.AVATAR, avatar);
}
public int getWeight() {
return getInt(Constant.WEIGHT);
}
public void setWeight(int weight) {
put(Constant.WEIGHT, weight);
}
public String getWeightUnit() {
return validateStringResult(Constant.WEIGHT_UNIT);
}
public void setWeightUnit(String weightUnit) {
put(Constant.WEIGHT_UNIT, weightUnit);
}
public ExperienceLevel getExperienceLevel() {
final String result = validateStringResult(Constant.EXPERIENCE_LEVEL);
if (result.equalsIgnoreCase("intermediate")) {
return ExperienceLevel.INTERMEDIATE;
} else if (result.equalsIgnoreCase("advanced")) {
return ExperienceLevel.ADVANCED;
} else {
return ExperienceLevel.BEGINNER;
}
}
public void setExperienceLevel(String experienceLevel) {
put(Constant.EXPERIENCE_LEVEL, experienceLevel);
}
public List<Goal> getGoals() {
return getList(Constant.GOALS);
}
public void setGoals(List<Goal> goals) {
put(Constant.GOALS, goals);
}
public int getScore() {
return getInt(Constant.SCORE);
}
public void setScore(int score) {
put(Constant.SCORE, score);
}
public boolean isPremium() {
return getBoolean(Constant.IS_PREMIUM);
}
public void setPremium(boolean premium) {
put(Constant.IS_PREMIUM, premium);
}
public boolean isTrialPeriod() {
return getBoolean(Constant.IS_TRIAL_PERIOD);
}
public void setTrialPeriod(boolean trialPeriod) {
put(Constant.IS_TRIAL_PERIOD, trialPeriod);
}
public int getTotalWorkoutBuilds() {
return getInt(Constant.TOTAL_WORKOUT_BUILDS);
}
public void setTotalWorkoutBuilds(int totalWorkoutBuilds) {
put(Constant.TOTAL_WORKOUT_BUILDS, totalWorkoutBuilds);
}
private String validateStringResult(String key) {
final String result = getString(key);
return result == null ? "" : result;
}
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.kineticoach.traveltrainer, PID: 22022
java.lang.IllegalStateException: ParseObject has no data for 'name'. Call fetchIfNeeded() to get the data.
at com.parse.ParseObject.checkGetAccess(ParseObject.java:3607)
at com.parse.ParseObject.getString(ParseObject.java:3186)
at com.kineticoach.traveltrainer.models.objects.User.getName(User.java:25)
at com.kineticoach.traveltrainer.fragments.ProfileFragment.lambda$loadUserData$1$ProfileFragment(ProfileFragment.java:153)
at com.kineticoach.traveltrainer.fragments.-$$Lambda$ProfileFragment$Sdpefi97hyh_jTMOE2pWx3FVbo8.run(Unknown Source:2)
at android.os.Handler.handleCallback(Handler.java:874)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:198)
at android.app.ActivityThread.main(ActivityThread.java:6729)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Follow this reference
we do not send the nested ParseObject's full data to cloud code. In order to get the data of the nested ParseObject, you have to do a query or fetch. The result you get from cloud code is pretty similar to the result you get from ParseQuery. We represent nested ParseObject as pointer and you have to fetch or query it in order to use it.

How to use a constructor that already exists?

I am building a simple app for children with Firebase and I'm constantly getting this error:
Android Firebase Database exception: not define a no-argument constructor
I have a class Activities and two other helper classes HomeActivities and OutsideActivities. This is my code for my classes:
public class Activities {
private String type;
private int count;
public Activities() { }
public Activities(String type, int count) {
this.type = type;
this.count = count;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
class HomeActivities {
private String name;
private int noOfChildren;
HomeActivities() { }
public HomeActivities(String name, int noOfChildren) {
this.name = name;
this.noOfChildren = noOfChildren;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNoOfChildren() {
return noOfChildren;
}
public void setNoOfChildren(int noOfChildren) {
this.noOfChildren = noOfChildren;
}
}
class OutsideActivities {
private String name;
private int noOfChildren;
public OutsideActivities() { }
public OutsideActivities(String name, int noOfChildren) {
this.name = name;
this.noOfChildren = noOfChildren;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNoOfChildren() {
return noOfChildren;
}
public void setNoOfChildren(int noOfChildren) {
this.noOfChildren = noOfChildren;
}
}
}
Please see all the constructors. Even if I already have defined the correct constructor in each class, I get this error. How to solve this? Please help me.
There are two methods in which you can get rid of this error.
Make both inner classes static.
Make both inner classes independent (each class in it's own separte file).
That's it!
Constructor of HomeActivities() has default visibility. public keyword is missed. Database ORM provider can't instantiate objects when the constructor is not public.
Should be:
class HomeActivities {
private String name;
private int noOfChildren;
public HomeActivities() { }
All other suggestions are valid as well. You should externalize classes in separate files.

Java serialization, android

I need to save data into object
Here is my object class where I must store data:
public static class FilterEntity implements Serializable {
public int ageFrom;
public int ageTo;
public String sex;
public String status;
public void setAgeFrom()
{
this.ageFrom = ageFrom;
}
public void setAgeTo()
{
this.ageTo = ageTo;
}
public void setSex()
{
this.sex = sex;
}
public void setStatus()
{
this.status = status;
}
public Integer getAgeFrom()
{
return ageFrom;
}
public Integer getAgeTo()
{
return ageTo;
}
public String getSex()
{
return sex;
}
public String getStatus()
{
return status;
}
}
Is it correct implementation of serialization?
In the main activity I'm saving data to FilterEntity object
private FilterEntity filter = new FilterEntity();
filter.status = valueOf(spStatusForSearch.toString());
filter.sex = valueOf(rgSex);
filter.ageTo = sbAgeHigh.getProgress();
filter.ageFrom = sbAgeLow.getProgress();
Can I do it such way?
How I can get access to data, from the third class?
don't use static
public class FilterEntity implements Serializable {
public int ageFrom;
public int ageTo;
public String sex;
public String status;
public void setAgeFrom()
{
this.ageFrom = ageFrom;
}
public void setAgeTo()
{
this.ageTo = ageTo;
}
public void setSex()
{
this.sex = sex;
}
public void setStatus()
{
this.status = status;
}
public Integer getAgeFrom()
{
return ageFrom;
}
public Integer getAgeTo()
{
return ageTo;
}
public String getSex()
{
return sex;
}
public String getStatus()
{
return status;
}
}
To set Values to Model Class
FilterEntity filter = new FilterEntity();
filter.setStatus(spStatusForSearch.toString());
filter.setSex(rgSex);
filter.setAgeTo(sbAgeHigh.getProgress());
filter.setAgeFrom(sbAgeLow.getProgress());
And to get Values
String status = filter.getStatus();
String sex = filter.getSex();
String ageTo = filter.getAgeTo();
String ageFrom = filter.getAgeFrom();
public class FilterEntity
{
public int ageFrom;
public int ageTo;
public String sex;
public String status;
public FilterEntity(int ageFrom, int ageTo, String sex,String status)
{
this.ageFrom = ageFrom;
this.ageTo = ageTo;
this.sex = sex;
this.status = status;
}
public int getAgeFrom()
{
return ageFrom;
}
public int getAgeTo()
{
return ageTo;
}
public String getSex()
{
return sex;
}
public String getStatus()
{
return status;
}
}
// in your main activity file where your are saving the data
static ArrayList<FilterEntity> data=new ArrayList<FilterEntity>();
data.add(new FilterEntity(sbAgeLow.getProgress(),sbAgeHigh.getProgress(),valueOf(rgSex),valueOf(spStatusForSearch.toString())));
// you can add multiple like this
/*Now pass this arraylist to your third activity through intent or make this arraylist static so will be able to access this arraylist anywhere through the name of the class example: MainActivity.data*/
//Now in the third activity where you want the data of the object , get the arraylist
ArrayList data=MainActivity.data;
Iterator iterotor=data.iterator();
while (iterotor.hasNext())
{
FilterEntity filterEntity=iterotor.next();
// the multiple values you add
String status = filterEntity.getStatus();
String sex = filterEntity.getSex();
int ageTo = filterEntity.getAgeTo();
int ageFrom = filterEntity.getAgeFrom();
}

create unique id without using constructure and setters

class person {
private int id ;
private String name;
private boolean gender;
public person() {
}
public AtomicLong getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isGender() {
return gender;
}
public void setGender(boolean gender) {
this.gender = gender;
}
}
I want to create unique id in this class without using constructors and setters.
To construct a person instance, the field initializer will be copied into the constructor. Assuming that's okay, you could use an AtomicInteger and something like,
private static AtomicInteger ai = new AtomicInteger(0);
private int id = ai.incrementAndGet();
you could add:
private static int ID_GENERATOR = 0;
then, in the constructor, you will use:
public person() {
id = ID_GENERATOR++;
}

apache commons beanutils, how to set property value?

In the java, commons beanutils, try to set property 'address' and 'creditCardList' to object, but it gave me error :
java.lang.NoSuchMethodException: Property 'address' has no setter method in class 'class com.dao.Student'
but I have this method there. The code is here:
public class Main {
public static void main(String[] args) {
Object student = new Student("John");
Object address = new Address("NJ");
try {
PropertyUtils.setProperty(student, "address", address);
//----------
List list = new ArrayList();
Object creditCard = new CreditCard();
list.add(creditCard);
PropertyUtils.setProperty(student, "creditCardList", list);
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Student {
private String name;
private Address address;
private List<CreditCard> creditCardList;
public Student(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public List<CreditCard> getCreditCardList() {
return creditCardList;
}
public void setCreditCardList(List<CreditCard> creditCardList) {
this.creditCardList = creditCardList;
}
}
class Address {
private String name;
public Address(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class CreditCard{
private String cardName;
public String getCardName() {
return cardName;
}
public void setCardName(String cardName) {
this.cardName = cardName;
}
}
Your class Student should be a public class , try making it public and rerun your code.
I moved Student to a own file and made it public, that worked fine :)

Categories

Resources