Removing data in ObjectBox based on Id - android

I would like to remove the data on my ObjectBox database in Android based on its Id. Is this correct?
Box<Cart> box = ObjectBox.get().boxFor(Cart.class);
Cart order = box.get(id);
box.remove(order);
Thank you

The remove method is overloaded and there are variants accepting the following arguments:
entity object
ID (long)
java.util.Collection objects
long... ids
T... objects
Thus, you can directly remove by ID like this:
box.remove(id);
For more details, please check the API docs of the Box class.

Related

Is there a way to map my document id with toObject?

I'm deserializing an entire document, to keep things in handy and prevent me to check for each value and construct the object I use
myList.add(documentSnapshot.toObject(House::class.java))
Now, lets say House is this
data class House(val name:String,val:address:String)
Now, if I want to also get the House document Id and put it inside my document I do this
data class House(val houseId:String,val name:String,val:address:String)
But after doing that , the first line of code transforms into this
val houseId = documentSnapshot.id
val houseName = docuementSnapshot.getString("name")
val houseAddress = documentSnapshot.getString("address")
myList.add(House(houseId,houseName,houseAddress))
What I want to do is use .toObject() to also map that extra field that is the document id inside of it because if the House object expands in size, I will need to hand write again each property, and now think that house has 100 properties and I just need the id of it inside the object. I will need to write off 99 get fields to just place the document Id inside that house object.
Is there a way to map that id to the object without doing the above and just placing .toObject ?
Thanks
You need just add annotation #DocumentId
data class House(#DocumentId val houseId:String,val name:String,val:address:String)
What I want to do is use .toObject() to also map that extra field that is the document id
This will be possible only if the document already contains the id property that holds the document id as a value. If you only have the name and the address, then you cannot map the id, because it doesn't exist in the document. To be able to map all those three properties you should update each and every document in that collection so it contains the document id. If you have lots of documents in the collection, I recommend you use batch writes.
Right after that, you'll be able to use:
val house = documentSnapshot.toObject(House::class.java)
And now this house object will contain the id too.

Connecting DB request to API - Android

I would like to do something, I don't know how to do it. But before, let me explain the previous steps:
1st: I have defined a content manager that, when I introduce a specific "id" from a resource, it finds such reosurce and shows it to me. In addition, I have created a POJO for the resource and an interface in which I specifiy my request.
2nd: Now I want to be able to incorporate such resources into a local data base. So, I have defined my entity for such data base (and that entity has the same attributes that the resource (consulted) from the API). Then I have defined the contract for such entity and the repository for that entity (where I have defined my query as "WHERE field "name" = (...)" .
3rd: After that, I have defined a ContentProvider for my DB.
4th: Finally, there is my MainActivity, which acts as a Client to consult my DB through my content provider.
Now, my question is, how and where I have to define the next:
a) When I make a search -based on the attribute name of the resource-, if the content provider doesn't find it in my local DB, then a request is made to the API (and the name specified on my search should fit with the attribute "name"
of the resource in the API).
b)Once the response with the resource is given, my local DB should add that resource as a new entity.
Thanks.
I hope I have explained myself well.

How to add a specific key with a specific value in a firebase database with Android?

I have a realtive simple Firebase database, in which i have 2 models. A ListModel and a UserModel. In my Lists, i'm using push() method to generate unique ids. Each unique id i want to be added as a key and "true" as it's value under Users/gmail#gmail,com/Lists.
When i add the first list, the database looks like this:
And everything works fine, but when i try to add another one, the database looks like this:
In Users/gmail#gmail,com/Lists, the first one is overwritten by the second insert. How can i add the specific id and the specific value, as a new item as shown below?
And this is my code:
final UserModel um = new UserModel();
um.setUserEmail("gmail#gmail,com");
userDatabaseReference.setValue(um);
ListModel lm = new ListModel();
lm.setListName(listName);
listKeyDatabaseReference = listDatabaseReference.push();
listKey = listKeyDatabaseReference.getKey();
listKeyDatabaseReference.setValue(lm);
listDatabaseReference.child(listKey).child("Users").child("gmail#gmail,com").setValue("true");
userDatabaseReference.child("gmail#gmail,com").child("Lists").child(listKey).setValue("true");
Thanks in advance!
Check the official doc:
For basic write operations, you can use setValue() to save data to a specified reference, replacing any existing data at that path.
Your problem is here:
userDatabaseReference.setValue(um);
In this way you are overriding all children in the userDatabaseReference path.
It means that the first record in Users/gmail#gmail,com/Lists is just deleted when you are adding the second one.
Before using the
userDatabaseReference.setValue(um);
you can check if the record exists.
If doesn't exist use the setValue to add the user-model with all its data.
If it exists, just skip this step and add the data in the lists path inside the same user.

How to link many objects to a singular object in Parse.com

I am making an android application that uses Parse.com as its server backend.I want to enable users to post comments to poems uploaded but am stuck on how to link many comments to a particular poem Object.Does anyone have an idea of how to do that?Any help will be appreciated.
You create the comment as an instance of class=comment that is separate from the Poem instance, saving the object ID that parse returns on 'insert comment'.
Then, as a child of Poem object, you have 'comments' attribute under which you insert ArrayType pointer to the ID returned by the previous insert to the comment class.
'{"comments":{"__op":"Add","objects":[{"__type":"Pointer",
"className":"Comment","objectId":"$returnValueOfCommentInsert"}]}}'
The pointer type entity is added (ptrToComment) is added to an array of pointers sitting in 'comments' entity inside Poem object.
This assumes use of Rest API.
Reading the documentation is a good start. How about the section on Andriod Relational Data?

Using text field with AdapterView

Is it possible to use AdapterView with text fields in android?
My query returns a set of values and for each I want to place that within a textfield, so that the user may edit the value.
Also, I want to click a button to create a new empty field, so that I may insert a new entry.
If you know of good example, then please let me know!
EDIT 1
I would prefer to use XML to define ui and I found this informations:
"In this case we create a new id called text1. The + after the # in the id string indicates that the id should be automatically created as a resource if it does not already exist, so we are defining text1 on the fly and then using it." Source http://developer.android.com/resources/tutorials/notepad/notepad-ex1.html
Will this + allow me to autocreate as many fields as needed? Is there a way I can generically specify the layout in XML and then create fields adhoc based on db response?
Many thanks,
Mith
Is it possible to use AdapterView with
text fields in android?
Yes. It will be complex for you (and possibly also for the user), so I would not recommend it unless you have a few months' Android programming experience, but it should work.
Also, I want to click a button to
create a new empty field, so that I
may insert a new entry.
That will get a little complicated.
Will this + allow me to autocreate as
many fields as needed?
No, that is not how you use ListAdapters for ListView rows.
Is there a way I can generically
specify the layout in XML and then
create fields adhoc based on db
response?
Use a CursorAdapter.
Here is a free excerpt from one of my books that describes creating custom adapter classes. Here is a sample project that shows creating a custom CursorAdapter to display the results of a database query.

Categories

Resources