How to use realm relationships using 2 RealmList, here the example.
Class Menu
public class Menu extends RealmObject {
#SerializedName("name")
private String name;
#SerializedName("module")
private String module;
#SerializedName("controller")
private String controller;
#SerializedName("parent_module")
private String parentModule;
#SerializedName("status")
private Boolean status;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getModule() {
return module;
}
public void setModule(String module) {
this.module = module;
}
public String getController() {
return controller;
}
public void setController(String controller) {
this.controller = controller;
}
public String getParentModule() {
return parentModule;
}
public void setParentModule(String parentModule) {
this.parentModule = parentModule;
}
public Boolean getStatus() {
return status;
}
public void setStatus(Boolean status) {
this.status = status;
}
}
Class Privilege
public class Privilege extends RealmObject {
private String module;
public String getModule() {
return module;
}
public void setModule(String module) {
this.module = module;
}
}
and i'm using this method to save them.
#Override
public void saveMenuPrivilege(RealmList<Menu> menu, RealmList<Privilege> privileges) {
}
now what makes me confuse is, there is a condition where if the module in class Menu has the same module in class Privilege, then set the active field for that module in class Menu to be "true". How to do that? or i'm doing it wrong using the code above?
Thanks in advance
All ready have the answer, just testing all by myself. :D
Related
I'm try to use Realm Database in My project
Here are my code snippets
In Application java code
public class CoreApplication extends Application {
private static CoreApplication sInstance;
public static final String F_PREFERENCE = "K_PREFERENCES";
public static Realm realm;
#Override
public void onCreate() {
super.onCreate();
sInstance = this;
LocaleManager.setLocale(this);
realm.init(this);
realm = Realm.getDefaultInstance();
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder()
.name("mydb.realm")
.schemaVersion(2)
.build();
Realm.setDefaultConfiguration(realmConfiguration);
}
#Override
public void onTerminate() {
Realm.getDefaultInstance().close();
super.onTerminate();
}
}
RealmObject java class
public class CashierTable extends RealmObject implements Serializable {
#Index
#PrimaryKey
private long id;
private String name = "";
private String password = "";
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
When I try to call this function when App has started.I have a exception
public static CashierTable getSingleCashierTable() {
CoreApplication.realm = Realm.getDefaultInstance();
AtomicReference<CashierTable> cashierTable=new AtomicReference<>();
CoreApplication.realm .executeTransaction(realm -> {
cashierTable.set(realm.where(CashierTable.class).findFirst());
});
return cashierTable.get();
}
Here is a my log result
java.lang.RuntimeException: Unable to create application com.unipay.posApp.CoreApplication: java.lang.IllegalStateException: Schema validation failed due to the following errors:
- Type 'CashierTable' appears more than once in the schema.
I cleared cash,but still have same issue.
Can anyone explain me what's wrong in my code?
You cannot have two RealmObject classes with the same class name twice unless you use Realm 5.0+ and use #RealmClass(name="... to specify a different table name for at least one of your classes.
package io.package.first;
public class Dog extends RealmObject {
}
package io.package.second;
#RealmClass(name="SecondDog")
public class Dog extends RealmObject {
}
I want to retrieve data from a web service to textview. Web service works well. I'm using retrofit for network. But I'm getting null in prmoDetails here response.body. I have checked and tried all past solutions as well. But still it's not working. Please help me to solve this.
POJO class
public class PromoDetails {
private String PromoId;
private String PromoName;
private String Category;
private String PromoImg;
private String promoDetails;
private String promoValidty;
public PromoDetails(String PromoId, String PromoName, String Category , String PromoImg , String promoDetails , String promoValidity) {
this.PromoId = PromoId;
this.PromoName = PromoName;
this.Category = Category;
this.PromoImg = PromoImg;
this.promoDetails = promoDetails;
this.promoValidty = promoValidity;
}
public String getPromoId() {
return PromoId;
}
public void setPromoId(String promoId) {
PromoId = promoId;
}
public String getPromoName() {
return PromoName;
}
public void setPromoName(String promoName) {
PromoName = promoName;
}
public String getCategory() {
return Category;
}
public void setCategory(String category) {
Category = category;
}
public String getPromoImg() {
return PromoImg;
}
public void setPromoImg(String promoImg) {
PromoImg = promoImg;
}
public String getPromoDetails() {
return promoDetails;
}
public void setPromoDetails(String promoDetails) {
this.promoDetails = promoDetails;
}
public String getPromoValidty() {
return promoValidty;
}
public void setPromoValidty(String promoValidty) {
this.promoValidty = promoValidty;
}}
Api Interface
public interface ApiInterface {
#POST("ap/promotions.php")
Call<List<PromoDetails>> getPromotions();}
MainActivity
public class MainActivity extends AppCompatActivity {
private ApiInterface apiInterface;
private List<PromoDetails> promoDetails;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getPromotionUpdate();
}
private void getPromotionUpdate() {
apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
Call<List<PromoDetails>> call = apiInterface.getPromotions();
call.enqueue(new Callback<List<PromoDetails>>() {
#Override
public void onResponse(Call<List<PromoDetails>> call, Response<List<PromoDetails>> response) {
promoDetails = response.body();
runOnUiThread(new Runnable() {
#Override
public void run() {
TextView prDescription = (TextView)findViewById(R.id.TextView1) ;
prDescription.setText(promoDetails.get(0).getPromoId());
}
});
}
#Override
public void onFailure(Call<List<PromoDetails>> call, Throwable t) {
}
});
}}
My web service like this
[{"promoId":"7","companyName":"Pizza Hut","pName":"Connecting Dots TEDXCOLOMBO 2017","category":"EVENTS","pImg":"https:\/\/androidsra.000webhostapp.com\/img_ap\/tedx.png"},{"promoId":"6","companyName":"Subway","pName":"BUY any SUB & get another SUB FREE!","category":"FOODS & DRINKS","pImg":"https:\/\/androidsra.000webhostapp.com\/img_ap\/subway.png"},{"promoId":"5","companyName":"KFC ","pName":"40% off at Queens Hotel - Kandy for HSBC Credit cards.","category":"BANKS & CARDS","pImg":"https:\/\/androidsra.000webhostapp.com\/img_ap\/queens.png"},{"promoId":"4","companyName":"Pizza Hut","pName":"New sets of Furniture with special discounts.","category":"HOME & KITCHEN","pImg":"https:\/\/androidsra.000webhostapp.com\/img_ap\/singerfur_promo.png"},{"promoId":"3","companyName":"Browns Tours","pName":"Exclusive Offer !! Fly to Melbourne with Srilankan Airlines from Browns Tours","category":"TRAVEL","pImg":"https:\/\/androidsra.000webhostapp.com\/img_ap\/melbourne_promo.png"},{"promoId":"2","companyName":"KFC ","pName":"Hot Drumlets with 2L Pepsi for just Rs.1100\/- only","category":"FOODS & DRINKS","pImg":"https:\/\/androidsra.000webhostapp.com\/img_ap\/kfc_promo.png"},{"promoId":"1","companyName":"Pizza Hut","pName":"50% Off for Medium Pizzas.","category":"FOODS & DRINKS","pImg":"https:\/\/androidsra.000webhostapp.com\/img_ap\/pizza_promo.png"}]
I think it's because your pojo class variables doesn't match with your response variables. Either use #SerializedName annotation or basically change your pojo definitions as exactly as in your response. For example:
#SerializedName("promoId")
private String PromoId;
#SerializedName("pName")
private String PromoName;
or
private String promoId;
private String pName;
As you are not passing any data to getPromotions i presume its a get Method. make following changes in code
public interface ApiInterface {
change --------> #GET("ap/promotions.php")
Call<List<PromoDetails>> getPromotions();}
and change your pojo class to following
public class Example {
#SerializedName("promoId")
#Expose
private String promoId;
#SerializedName("companyName")
#Expose
private String companyName;
#SerializedName("pName")
#Expose
private String pName;
#SerializedName("category")
#Expose
private String category;
#SerializedName("pImg")
#Expose
private String pImg;
public String getPromoId() {
return promoId;
}
public void setPromoId(String promoId) {
this.promoId = promoId;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public String getPName() {
return pName;
}
public void setPName(String pName) {
this.pName = pName;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getPImg() {
return pImg;
}
public void setPImg(String pImg) {
this.pImg = pImg;
}
}
why your post method don't have any passing parameters???
Check method, is it GET or POST?
If it is GET,
then change it to #GET("ap/promotions.php") , other code will be same.
check response.isSuccessful() before getting the body, if it is false get response.errorBody() and that will show you the cause of error
I have 2 class one is login_class second is student_class which is showing details of student.
What i have to do is when i login. In response i get the students details like
student_name,student_srno,student_father_name,student_mother_name etc.
This is my bean(Getter Setter) class
package com.smartschoolapp;
public class Bean {
String username, password, srno, studentname, classname, sec, contact,
father_name, mother_name, dob;
public Bean(String username,String password,String srno,String studentname,String classname,String sec,String contact,String father_name,String mother_name,String dob) {
this.username=username;
this.password= password;
this.srno=srno;
this.studentname=studentname;
this.classname=classname;
this.sec=sec;
this.contact=contact;
this.father_name=father_name;
this.mother_name=mother_name;
this.dob =dob;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSrno() {
return srno;
}
public void setSrno(String srno) {
this.srno = srno;
}
public String getStudentname() {
return studentname;
}
public void setStudentname(String studentname) {
this.studentname = studentname;
}
public String getClassname() {
return classname;
}
public void setClassname(String classname) {
this.classname = classname;
}
public String getSec() {
return sec;
}
public void setSec(String sec) {
this.sec = sec;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
public String getFather_name() {
return father_name;
}
public void setFather_name(String father_name) {
this.father_name = father_name;
}
public String getMother_name() {
return mother_name;
}
public void setMother_name(String mother_name) {
this.mother_name = mother_name;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public Bean() {
}
}
And this is my login_class where i add data of the student in list.
class login_class extends activity{
Bean beanobj;
List student_detail_list;
.....
{
String name = getting data from response;
......... so on
beanobj = new Bean();
beanobj.studentname(name);
beanobj.student_parent_name(parentname);
.............so on
}
order_list.add(beanobj); // the details to list
}
Now the next activity student_class where i want to show the student in listview
public class student_class extends Activity {
ListView student_listview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.student_list);
student_listview = (ListView) findViewById(R.id.student_listview);
}
}
*==>> in login_class i have created List student_detail_list how can i get it in my student_class class and retrieve data .
Thanks
There are a lot a ways to do that. 1.Recommended Solution : How to pass an object from one activity to another on Android you can serialize your objects and put it into the intent. This is the recommended solution but there are some problems here. If your data contains a lot of memory, it might crash your application in runtime.
2. You can save your array into the file, send file path to other activity as with URL. In the second activity read the file and create the array in second activity as well.
3. My favourite solution is create a singleton class for student array, reach the class in any activity you want. But you should be careful because singleton pattern has some issues by design also it will stay in memory all the time
Realize the "class Bean implements Parcelable". When go to "student_class" from "login_class", just put the "student bean data" inside the Intent. After going to student_class, you can get the "student bean data" from the intent, here the sample code:
Intent intent = new Intent(login_class.this, student_class.class)
intent.putParcelableArrayListExtra("content", student_detail_list);
startActivity(intent);
In "student_class":
student_detail_list = getIntent().getParcelableArrayListExtra("content");
I am getting this error:
Caused by: com.google.firebase.database.DatabaseException: Found conflicting getters for name: isAccessibilityFocusable
while trying to update my User.
The User class is like this
public class User {
#PrimaryKey
String userName;
String groupName;
int totalMoney;
boolean turn;
boolean hapyo;
boolean blind;
#Exclude
TextView textView;
#Exclude
Button bHapdiye,bChale,bShow,bHeram;
public Button getbHapdiye() {
return bHapdiye;
}
public void setbHapdiye(Button bHapdiye) {
this.bHapdiye = bHapdiye;
}
public Button getbChale() {
return bChale;
}
public void setbChale(Button bChale) {
this.bChale = bChale;
}
public Button getbShow() {
return bShow;
}
public void setbShow(Button bShow) {
this.bShow = bShow;
}
public Button getbHeram() {
return bHeram;
}
public void setbHeram(Button bHeram) {
this.bHeram = bHeram;
}
public TextView getTextView() {
return textView;
}
public void setTextView(TextView textView) {
this.textView = textView;
}
public boolean isBlind() {
return blind;
}
public void setBlind(boolean blind) {
this.blind = blind;
}
public boolean isHapyo() {
return hapyo;
}
public void setHapyo(boolean hapyo) {
this.hapyo = hapyo;
}
public boolean isTurn() {
return turn;
}
public void setTurn(boolean turn) {
this.turn = turn;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
public int getTotalMoney() {
return totalMoney;
}
public void setTotalMoney(int totalMoney) {
this.totalMoney = totalMoney;
}
}
I am trying to update the User like this
final User user1 = userList.get(0);
user1.setTextView(tvUser1);
user1.setbChale(bChaleP1);
user1.setbHapdiye(bHapdiyeP1);
user1.setbHeram(bHeramP1);
user1.setbShow(bShowP1);
user1.setTurn(true);
setUsersTurn(user1.isTurn(),bHapdiyeP1,bChaleP1,bShowP1,bHeramP1);
setTurnInFirebase(user1);
The setTurnInFirebase method is like this
public void setTurnInFirebase(User user){
myRef.child("users").setValue(user);
}
So when I run the project, I get the above mentioned error. What could be the reason for this. Thankyou
Your user contains a TextView, which is a class that Firebase won't be able to serialize/deserialize.
To allow writing a class to the Firebase Database, make sure that it only contains properties of simple types or objects that you created: so called POJOs - Plain Old Java Objects.
Alternatively you can exclude the non-supported properties (such as the TextView) by annotating them:
#Exclude
public TextView getTextView() {
return textView;
}
#Exclude
public void setTextView(TextView textView) {
this.textView = textView;
}
i have the following class which i intent to pass from one activity to another:
public class Ad extends ListItem implements parcelable{
private String _type;
private String _recordID;
private String _line1;
private String _line2;
private String _line3;
private String _line4;
private String _url;
private IOnUiNeedToUpdateListener _listener;
private boolean _notActive = false;
private String _alertText;
private Double _longitude;
private Double _latitude;
}
i want to pass an array of such objects from one activity to another. however, i do not need to pass all fields.
is it possible to create a parcel only from the desired fields and send it?
It's your code that writes to Parcel and your code that reads from Parcel. So basically yes. You can write whatever you want. Content of all members, content of some, no members, but other values you use to restore state of the object etc, etc.
Try design your class like this..
public class Form implements Parcelable {
private String formdata1;
private String formdata2;
private String formdata3;
private String formdata4;
public Form() {
}
public Form(Parcel in) {
setFormdata1(in.readString());
setFormdata2(in.readString());
setFormdata3(in.readString());
setFormdata4(in.readString());
}
public String getFormdata1() {
return formdata1;
}
public void setFormdata1(String formdata1) {
this.formdata1 = formdata1;
}
public String getFormdata2() {
return formdata2;
}
public void setFormdata2(String formdata2) {
this.formdata2 = formdata2;
}
public String getFormdata3() {
return formdata3;
}
public void setFormdata3(String formdata3) {
this.formdata3 = formdata3;
}
public String getFormdata4() {
return formdata4;
}
public void setFormdata4(String formdata4) {
this.formdata4 = formdata4;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel in, int arg1) {
in.writeString(getFormdata1());
in.writeString(getFormdata2());
in.writeString(getFormdata3());
in.writeString(getFormdata4());
}
public static final Parcelable.Creator<Form> CREATOR = new Parcelable.Creator<Form>() {
#Override
public Form createFromParcel(Parcel in) {
return new Form(in);
}
#Override
public Form[] newArray(int size) {
return new Form[size];
}
};
}