I just started to implement parse in my app.
I want to save user data on a cloud so it can be accessed by other users.
I'm trying to subclass the ParseObject just as they guide in their tutorial.
I can see on the dashboard the new class i created, but no object is being uploaded there.
same thing about the exampleObject.
this is the Application Class onCreate():
Parse.enableLocalDatastore(this);
ParseObject.registerSubclass(MyClass.class);
Parse.initialize(this, "XXXX", "YYYY");
ParseObject testObject = new ParseObject("Object");
testObject.put("foo", "bar");
testObject.saveInBackground();
MyClass R = new MyClass();
R.set(...) //here i set all attributes
ArrayList<Ingredient> arrayList = new ArrayList<>();
R.saveInBackground();
MyClass:
#ParseClassName("MyClass")
public class MyClass extends ParseObject implements Serializable , Cloneable{
private attrs; //define Class's attributes
public MyClass()
{
super();
}
I added the uses-permissions and gradle code.
You are using R.set() to set the attributes but there is no set method for ParseObjects and I don't see one in MyClass().
Try using R.put(key, value) to add data to the object.
From Parse.com:
"You can add accessors and mutators for the fields of your ParseObject easily. Declare the getter and setter for the field as you normally would, but implement them in terms of get() and put(). The following example creates a displayName field in the Armor class:"
// Armor.java
#ParseClassName("Armor")
public class Armor extends ParseObject {
public String getDisplayName() {
return getString("displayName");
}
public void setDisplayName(String value) {
put("displayName", value);
}
}
Related
Items upon inserting
Items upon retrieving
So here is the thing:
I have an Object Product which extends RealmObject.
Inside the Product object, there is an attribute: RealmList. Bundle also extends RealmObject.
Inside the Bundle Object, there is an attribute: RealmList. ProductBundle extends RealmObject.
Now all those lists are not empty nor null by the time they should be inserted.
After insertion, if I query Realm for a list of Products, I will receive a list that has a list of RealmList. However, inside each of the Bundle items, the RealmList< ProductBundle> productBundles is always empty.
class Product extends RealmObject{
RealmList<Bundle> bundles= new RealmList<Bundle>();
boolean isLocallyAddedToCart;
}
class Bundle extends RealmObject{
RealmList<ProductBundle> productsBundles = new RealmList<ProductBundle>();
}
class ProductBundle extends RealmObject{
String title;
}
Any thoughts on that??
Query:
RealmResults<Product> cartItem = realm.where(Product.class).equalTo("isLocallyAddedToCart", true).findAll();
Inserting:
public void insertAddToCart(#NonNull ArrayList<Product> items) {
try {
Realm realm = getRealmInstance();
realm.beginTransaction();
realm.insertOrUpdate(items);
realm.commitTransaction();
realm.close();
} catch (Exception ex) {
ex.getStackTrace();
}
}
Handling the objects:
RealmList<BundleProductOption> bundleProductOptions = new RealmList<>();
private SparseArray<List<ProductBundle>> optionsFinalSelection = new SparseArray<>();
BundleProductOption bundleProduct = new BundleProductOption();
bundleProduct.setDefaultTitle(bundleProductOption.getDefaultTitle());
bundleProduct.setOptionId(bundleProductOption.getOptionId());
bundleProduct.setParentId(bundleProductOption.getParentId());
bundleProduct.setRequired(bundleProductOption.getRequired());
bundleProduct.setType(bundleProductOption.getType());
RealmList<ProductBundle> productBundles = new RealmList<>();
productBundles.addAll(optionsFinalSelection.get(i));
bundleProduct.setProductsBundle(productBundles);
product.setSelectedOptions(bundleProductOptions);
you must manage your insertion with ID as a primary key in each model.
when u insert or update u must use a primary key for saving all states of your data, and then u can do not use them, but realm need them for arrange insert or update method.
keep me updated with your issue .
Hi all I can't think of a better example to illustrate my point so do let me know If my example has some errors. But hopefully this example will get my point through.
class A {
String CATEGORY = "A";
public String getCATEGORY() {
return CATEGORY;
}
}
class B extends A {
String CATEGORY = "B";
#Override
public String getCATEGORY() {
return CATEGORY;
}
}
class C extends A {
String CATEGORY = "C";
#Override
public String getCATEGORY() {
return CATEGORY;
}
}
public class MyClass {
private List<A> array = Arrays.asList(new A(), new B(), new C());
public MyClass() {}
}
Now if I upload MyClass onto firebase using setValue for example, firebase will show me the properties of class A, B and C. However, when I read the data from firebase and call sth like getValue(MyClass.class) the List it returns me are all of type A and the subclasses are not preserved. Is there a workaround to allow firebase to preserve the class types uploaded?
If you use Firebase's default serializer, it simply writes all public properties and fields to the database. Say that you store a single instance of each class, it'd be:
-L1234567890: {
cATEGORY: "A"
},
-L1234567891: {
cATEGORY: "B"
},
-L1234567892: {
cATEGORY: "C"
},
There won't be enough knowledge in the database for the SDK to reinflate the correct sub-class. While you and I can see that the cATEGORY value matches the class name, the Firebase SDK has no such knowledge.
It won't be too hard to write your own custom deserializer for this data though, taking a DataSnapshot with the values above and reinflating the correct class and values.
You could also do a hybrid: detect the class type directly, and then tell Firebase what class to read:
String cat = snapshot.child("cATEGORY").getValue(String.class)
Class clazz = "C".equals(cat) ? C.class : "B".equals(cat) ? B.class : A.clas;
A object = snapshot.getValue(clazz);
I have an existing array that I created locally and import to Firebase and my array looks like this.
These both elements are objects created that have some many information related to appointments.
Now i am trying to create a new element with the same form, for example:
2--
|__ And the object I have created in my app
I have only managed or eliminate the rest of the elements (with setValue(object))
Appointment newAppointment = new Appointment.Builder()
.fechacita(dateSelected)
.horacita(hourSelected)
.usertoken(mAuthManager.getCurrentUserId())
.oficina(centerSelected)
.build();
mDatabaseRef.child("LISTACITAS").setValue(newAppointment);
or create it with an ID that when recovering the data causes a crash in the application due to the deserialization of the objects that are not equal.
The Appointment object that I want to insert is
public class Appointment implements Parcelable {
private String fechacita;
private String horacita;
private Office oficina;
private String userID;
.....
}
The class is a normal Parcelable class that generates an object with her builder.
Please some help...
try this code
mDatabaseRef.push().setValue(incidentReportUser)
Write it this way (push() adds values instead of overriding).
Ans from here
UPDATE 1
if you want a series in key, not some random value, try this:
get the last key in the list using
Query dbQry = mDatabaseRef.orderByKey().limitToLast(1);
dbQry.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
int key = Integer.parseInt(dataSnapshot.getKey());
//Increment the key and add the object here using the earlier method
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
}
I have not checked this as of now, but you could get the idea
i) Class Place (has 3 string variables) ii) Class Coupon (has 4 string variables-3 same as Class Place and one additional String Variable). Now I have ArrayList of object of class Place which need to be copied to ArrayList of object of Class Coupon. SO, basically 3 variables of Arraylist of object Class Coupon will be same as ArrayList of object of Class Place and fourth variables will then be initialized with one another string. How i can achieve that.
And how I can do that vice-versa like copying 3 variables of ArrayList of object of class Coupon into ArrayList of object of Class Place. Please Help.
1. Class Place:
public class Place implements Serializable{
public String mPlace;
public String mOffer;
public String mImage;
public Place(String place, String offer, String image) {
this.mPlace = place;
this.mOffer = offer;
this.mImage = image;
}
}
2. Class Coupon:
public class Coupon implements Serializable {
public String mPlace;
public String mOffer;
public String mImage;
public String mItemClicked;
public Coupon(String place, String offer, String image, String ItemClicked) {
this.mPlace = place;
this.mOffer = offer;
this.mImage = image;
this.mItemClicked = ItemClicked;
}
}
3. I have now two variables List<Place>mPlaces and List<Coupon>mCoupons.
If one of variable has complete data then how I can copy it into another.
Since your members of Place.class is same in Coupon.class, you can remove those 3 members from Coupon.class and just extent Place.class like this :
public class Coupon extends Place implements Serializable {
public String mItemClicked;
public Coupon(String place, String offer, String image, String ItemClicked) {
this.mPlace = place;
this.mOffer = offer;
this.mImage = image;
this.mItemClicked = ItemClicked;
}
}
Then suppose if you have an ArrayList<Coupon> list = new ArrayList<>(); then you can easily create a list of Place.class like :
ArrayList<Place> placeList = new ArrayList<>();
for(Coupon coupon : couponList) {
placeList.add(coupon);
}
So your can easily cast coupon class to place class.
EDIT:
You have to create new Coupon Object for all Place object, the reason is When you create Coupon object it have Place Instance since it extends Place so you can directly cast Coupon to Place, but you can't cast directly the Vice-Versa. So you need to create new Coupon object for each Place Object and set the value. Like :
ArrayList<Coupon> couponList = new ArrayList<>();
for(Place place : placeList) {
Coupon coupon = new Coupon(place.mPlace, place.mOffer, place.mImage, null);
couponList.add(coupon);
}
Note: If the ArrayList<Place> you have is created by casting Coupon Object then you can directly cast the Place object to Coupon object so in this case no need to create new Coupon Object for all Place object.
You can use theese methods to convert them.
public static ArrayList<Coupon> toCoupons(ArrayList<Place> places){
ArrayList<Coupon> coupons = new ArrayList<>();
for (Place place: places) {
Coupon coupon = new Coupon;
coupon.mOffer = place.mOffer;
....
coupons.add(coupon);
}
return coupons;
}
public static ArrayList<Places> toPlaces(ArrayList<Coupons> coupons){
ArrayList<Coupon> places = new ArrayList<>();
for (Coupon coupon: coupons) {
Place place = new Place();
place.mOffer = coupon.mOffer;
....
places.add(place);
}
return places;
}
I strongly advice to create a base class that has fields which is shared between these classes. And other classes extends it. Then you can use polymorphism. It will make your world to run easier.
Good luck.
I am planning to store some data in json in that way I can read and write this json data.
I want to use it as a simple database instead of using sqlite.
Where should I create this json data in file hierarchy and how to write and read data from json.
just declare a string variable and then set that to your json like :
String myjson = "obj1: {}..." //it is your json
JsonObject obj = new JsonObject(myJson);
then you can do
String s = obj.getString("key");
This is a very nice question for a beginner in Android so I will tell you about singleton class which we use to store data in any language.
"Singleton class" this is a class which have only one reference (object). Code how to make this class is below:
public class DataController {
private static DataController ref;
public static DataController getInstance()
{
if(ref==null)
ref = new DataController();
return ref;
}
public void deleteInstance()
{
ref=null;
}
public ArrayList<MediaWrapper>videoWrapper = new ArrayList<MediaWrapper>();
public ArrayList<MediaWrapper>audioWrapper = new ArrayList<MediaWrapper>();
public ArrayList<MediaWrapper>documentWrapper = new ArrayList<MediaWrapper>();
}// you can add any data type under this class
Now anywhere you can use this class like this put this code inside your activity on-create method where you want to use this.
private DataController controller;
controller = DataController.getInstance();
Now just call that datatype in which you want to add your data like this.
controller.videoWrapper = //put your data inside this. This data will be save until or unless your activity in the stack
Also you can access that data like this controller.videoWrapper it will give you all data which you save in this.