Android working with realm - android

I have this model -
public class ItemsModel extends RealmObject {
private long id;
private String itemName;
private int itemCode;
private int itemPrice;
private int itemImage;
private int itemStock;
private int itemAmount;
private int discount;
private int total;
private boolean isSelected = false;
private int seller;
public ItemsModel() {
}
and this model -
public class CartModel extends RealmObject {
#PrimaryKey
private int cartId;
private RealmList<ItemsModel> itemsModels;
public CartModel() {
}
I saves items in ItemsModel and want to save the items selected in realmlist inside the CartModel, but there is some problems with that -
even when I'm trying to change the id before - it inserted to the main model(ItemsModel) and not to the ItemsList in the CartModel -
ItemsModel i = new ItemsModel();
i.setId(itemsModelRealmList.size());
i.setSeller(registerModels.getRegisterMainSeller());
i.setItemCode(item.getItemCode());
i.setItemPrice(item.getItemPrice());
i.setItemImage(item.getItemImage());
i.setItemName(item.getItemName());
realm.beginTransaction();
realm.where(CartModel.class).findFirst().getItemsModels().add(i);
realm.commitTransaction();
if I'm trying to insert to the list inside the cart the same object from ItemsModel - the recyclerview act like they are one object.
thanks

Related

Android ExpandableListview with Room database

I need to populate an ExpandableListView whose data is fetched from Room database.
There are answers on how to do this with SQLiteDatabase:
1) Android ExpandableListView and SQLite Database
2) Android ExpandableListView From Database
Is it possible to achieve the same with Room Database?
I have two tables: a) GroupHeader b) GroupContent
#Entity
public class GroupHeader implements Serializable {
#PrimaryKey(autoGenerate = true)
private int groupId;
private String groupName;
private String otherProperty1;
private String otherProperty2;
/* getters and setters */
}
#Entity
public class GroupContent implements Serializable {
#PrimaryKey(autoGenerate = true)
private int contentId;
private int groupId;
private String contentName;
private String otherProperty3;
private String otherProperty4;
/* getters and setters */
}
Any suggestions please?
Found the solution using #Relation.
Reference: https://developer.android.com/reference/android/arch/persistence/room/Relation
#Entity
public class GroupHeader implements Serializable {
#PrimaryKey(autoGenerate = true)
private int groupId;
private String groupName;
private String otherProperty1;
private String otherProperty2;
/* getters and setters */
}
#Entity
public class GroupContent implements Serializable {
#PrimaryKey(autoGenerate = true)
private int contentId;
private int groupId;
private String contentName;
private String otherProperty3;
private String otherProperty4;
/* getters and setters */
}
public class Wrapper {
#Embedded
private GroupHeader header;
#Relation(parentColumn="groupId", entityColumn="groupId", entity=GroupContent.class)
private List<GroupContent> contents;
/*getters & setters*/
}

GreenDao list entity with a list of entities

I am making an Android application using GreenDao, and I have these two entities:
#Entity
public class Quiz {
#Id(autoincrement = true)
private Long id;
private Date date;
private String type;
#ToMany(referencedJoinProperty = "quizId")
private List<Answer> answers;
}
#Entity
public class Answer {
#Id(autoincrement = true)
private Long id;
private int answer;
private float value;
private int questionNumber;
private String type;
private Long quizId;
}
I'm trying to fetch the list of Quiz using the following code:
DaoSession daoSession = AndroidAdapter.getDaoSession();
QuizDao quizDao = daoSession.getQuizDao();
List<Quiz> quizs = quizDao.loadAll();
But the list of answers always comes empty, what am I doing wrong?
My AndroidAdapter class:
public class AndroidAdapter {
public static Context getContext() {
return AppApplication.getContext();
}
public static DaoSession getDaoSession() {
return AppApplication.getDaoSession();
}
}
And AppApplication class
#Override
public void onCreate() {
super.onCreate();
context = this;
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this,ENCRYPTED ? "exam-db-encrypted" : "exam-db");
Database db = ENCRYPTED ? helper.getEncryptedWritableDb("exam-secret") : helper.getWritableDb();
daoSession = new DaoMaster(db).newSession();
this.addAllEvents();
}
public static DaoSession getDaoSession() {
return daoSession;
}
For me, I've not entered some Answer entities in my db.
Once I've added a code to add those entities to the db and I've requested the list with a generated getter for the List<Answer> answers - it all worked as it should 😊

Sort List<Modal> with Custom Fild Object

I write a program that parse json and shows it in a listView.
Now i want to sort it but don't know how and fail. In program i have a List<PoolOfflineModal> alist = new ArrayList<>();
so modal are this;
public class PoolOfflineModal {
private int id;
private String name;
private int price;
private int priceWithoutDiscount;
private String tel;
private int discountPercent;
private String address;
private String photo;
}
and data simple is here http://hayat.cloudsite.ir/pools/pools.json
so i want to sort List ascending with price, so update ListView and i don't know...
another idea?! or solution?!
another way are exist to sort ListView from a column without sort list?!?
this is my last Comparator
public class MyComparator implements Comparator<PoolOfflineModal> {
#Override
public int compare(PoolOfflineModal lo, PoolOfflineModal ro) {
int result = 0;
if (way == "name") {
result = lo.getName().compareTo(ro.getName());
} else if (way == "price") {
result = lo.getPrice().compareTo(ro.getPrice());
} else if (way == "percent") {
result = lo.getDiscountPercent().compareTo(ro.getDiscountPercent());
}
return result;
}
}
List Object Like This:
List<PoolOfflineModal> alist = new ArrayList<>();
Comparator Class Like this:
public class MyComparator implements Comparator<PoolOfflineModal> {
#Override
public int compare(PoolOfflineModal o1, PoolOfflineModal o2) {
return o1.id.compareTo(o2.id);
}
}
Use Like this:
Collections.sort(alist, new MyComparator());
Hope it will helpful to you.

What would be the best approach to design a dynamic query builder for realm.io?

I am designing an "advanced filter" functionality for my Android app where I can multi-select several query parameters, depending on the parameter type sometimes it will be a union, sometimes an intersection, but always on one object type (with some nested objects).
Would anyone know of an elegant and generic way to design such a query builder?
My object looks like this:
public class DBDocument extends RealmObject {
#PrimaryKey
private int _id;
private String updatedAt;
private String name;
private String subject;
private int type;
private int senderId;
private String dateSent;
private int paymentStatus;
private String paymentDue;
private double invoiceAmount;
private String currency;
private String acknowledged;
private String fetched;
private String exported;
private int recipient;
private int addressId;
private String printed;
private boolean important;
private boolean hidden;
private int printMode;
private int printRule;
private RealmList<DBTag> tag = new RealmList<>();
private RealmList<DBComment> comment = new RealmList<>();
private RealmList<DBEvent> event = new RealmList<>();
...
}

Gson not able to parse Array. But it is json array

I have the following json string:
{"objKampfEntry":[{"deffSpielerId":"9","kampfId":"7","offSpielerId":"10","rundeCounter":"0","rundenList":[{"deffIsReady":"0","deffMove":"0","deffSpieler":{"ausdauer":"5","eiId":"0","gesichtId":"normal","helm":"0","id":"13","kraft":"6","leben":"12","lebenMax":"50","level":"1","name":"Test","niederlagen":"0","punkte":"1","rang":"1","rustung":"0","siege":"0","timestamp":"1397385595686","waffe":"0"},"done":"0","kampfId":"7","offIsReady":"1","offMove":"0","offSpieler":{"ausdauer":"5","eiId":"0","gesichtId":"normal","helm":"0","id":"14","kraft":"6","leben":"12","lebenMax":"50","level":"1","name":"Test2","niederlagen":"0","punkte":"1","rang":"1","rustung":"0","siege":"0","timestamp":"1397385841118","waffe":"0"},"rundeNr":"0","winner":"0"}]}]}
If I use
Gson gson = new Gson();
Type listOfKampfEntry = new TypeToken<List<ObjKampfEntry>>(){}.getType();
ArrayList<ObjKampfEntry> list = gson.fromJson(json, listOfKampfEntry);
I get this error:
04-13 12:45:48.706: W/System.err(4411): com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2
04-13 12:45:48.706: W/System.err(4411): at com.google.gson.Gson.fromJson(Gson.java:815)
04-13 12:45:48.706: W/System.err(4411): at com.google.gson.Gson.fromJson(Gson.java:768)
04-13 12:45:48.706: W/System.err(4411): at com.google.gson.Gson.fromJson(Gson.java:717)
So I have an array of objKampfEntry and inside an array of rundenList. What am I doing wrong?
My ObjKampfEntry class:
public class ObjKampfEntry {
private long kampfId;
private long offSpielerId;
private long deffSpielerId;
private int rundeCounter;
private ArrayList<Runde> rundenList = new ArrayList<Runde>();
//constructor and getter/setter
}
And the runde object
public class Runde {
private long kampfId;
private long winner;
private int rundeNr;
private int offIsReady;
private int deffIsReady;
private int done;
private Spieler offSpieler;
private Spieler deffSpieler;
private int offMove;
private int deffMove;
...
Here the Spieler object
public abstract class ACharakter {
protected long id;
protected String name;
protected int leben;
protected int lebenMax;
protected int kraft;
protected int ausdauer;
public class Spieler extends ACharakter {
private int rang;
private int level;
private int punkte;
private long timestamp;
private int waffe;
private int rustung;
private int helm;
private int siege;
private int niederlagen;
private int eiId;
private String gesichtId;
Your input is not a JSON array of ObjKampfEntry objects. Instead, it is a JSON object with one key/value pair whose value is a JSON array of ObjKampfEntry objects.
The "correct" input according to your scheme would be [{"deffSpielerId":"9","kampfId":"7",...}], as that will indeed map to a List<ObjKampfEntry>. If you can't change the input, then you'll need another class holding one field named objKampfEntry of type List<ObjKampfEntry>:
public class ObjKampfEntries {
private ArrayList<ObjKampfEntry> objKampfEntry = new ArrayList<ObjKampfEntry>();
}
ObjKampfEntries entries = gson.fromJson(json, ObjKampfEntries.class);
List<ObjKampfEntry> list = entries.objKampfEntry;

Categories

Resources