I'm building a Class to populate my layouts using a JSON Schema. Basically in JSON I have a definition { type: "checkbox", label: "Label text", value: true, id: "liquids" }. The problem is I don't just have to render the UI i also need to collect values later when the form is posted to submit it to my API service.
The ID of each element is a string when I create the View element on my layout I need to give it this id however View has setId() method however this can only be an integer also has to be unique.
Is there any way I can use the string as id ?
You can use Strings as a form of ids by passing them to your View's setTag() method.
Then, to find a particular View, use findViewWithTag()
Just make sure that your String ids are unique.
you could use the hash code of the string as the id.
view.setId(string.hashCode());
Related
I am receiving a json string, I need to separate data by class properties. The problem is that I can only know part of the key. For example, the key is 12345#666.777, I only know #666.777. Is it possible to somehow use regular expressions or look for values by part of the key?
I would iterate over all the keys in the JSON object as shown here: https://discuss.kotlinlang.org/t/iterating-over-json-properties/1940/2
and check for every key if it contains the partial key I have as a substring using this standard Kotlin method:
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/contains.html
You also need to treat cases where the same substring is present in 2 different keys (if you think that is possible).
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.
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.
I know you can set the int id of a View using the following:
View.setId(int id);
But is there a way to use a string instead? I know what I could do is declare an array of arrays and each array contains a string and an int associated with that string and if the string is called then it gets the int associated with that string, but is there a simpler way to do it to set the id a as a string?
Also, when a View is created during runtime on the app, does it automatically generate a unique int id for it? Thanks for the help.
but is there a simpler way to do it to set the id a as a string?
No, there is no. View has just the setId(int) method. If your string is a number, you can convert this to int and use as id for your view.
Also, when a View is created during runtime on the app, does it
automatically generate a unique int id for it?
no you have to take care of it.
I know how to assing ID's dynamically by invoking setID(). For the ID's to be unique, I used to utilize ids.xml and pass to setID() ID's from the pre-generated pool of ID's.
Question 1: Is there any way to assign ID's without utilizing the ids.xml since I cannot anticipate how many ID's I will need in runtime?
I tried to bypass the first issue presented in Question 1 by dynamically assigning each of which an id based on its label's hash (each label is unique), but there is no way to gaurantee that ID's won't be colliding with ID's auto generated in R.java.
Question 1.1: How the ID naming collision can be resolved?
Question 2: Assume I have the ID value of which I assign and generate dynamically. Since the aformentioned ID does not appear in R.id, findViewById() won't be applicable for retrieving the view. Hence, how can the view be retrieved when the ID is known?
Answer 2: You'd be able to retrieve the view by its corresponding ID only after onCreate() has returned control (terminated).
From API level 17 you can get a new id by calling
View.generateViewId()
Details here.
Is there any way to assign ID's without utilizing the ids.xml since I
cannot anticipate how many ID's I will need in runtime?
This guarantees that every view has a unique ID
for(int i =0 ; i < yourIDcount ; i++){
yourView.setId(i);
}
how can the view be retrieved when the ID is known?
View.findViewById(yourView.getId());
can be used to get your view's id, since every view has a unique Id you can get back the view you wanted..
The word dynamic means which is created at runtime, since you assign id in onCreate it is assigned as the views id, since onCreate is called only once an activity is created, you can make sure that the id you assigned stays intact...