Mapping to custom objects
You can map your custom objects starting from MBSection automatically using MBurgerMapper class.
Using the commodity class MBFieldsMapping which fields of your custom class should be mapped with the fields of the MBSection you named on your Project dashboard. Your destination object should at least override getters and setters and if you wish to obtain simple values or MBurger object values for:
    Images -> First MBImage (only an object, not an array)
    Media & Documents -> First MBFile (only an object, not an array)
    Addresses -> Latitude, longitude or textual address
You will find all possible simple data inside the MBMappingArgs class.
Make sure your custom class is public and that provides getters and setters for all fields you wish to map.
For example, for a simple News class with title and MBurger image
1
class News: Serializable {
2
var title: String?
3
var img: MBImage?
4
}
Copied!
Can be mapped this way:
1
val fieldsMapping = MBFieldsMapping()
2
3
//”ttl” is my custom object element, “Title” is the name of the Element we want to map from the section
4
fieldsMapping.putMap(“ttl”, “Title”)
5
6
//”img” is my custom object element, “Images” is the name of the Element we want to map from the section, we add “imageArguments” which tells to take only the first image from the array.
7
val imageArguments = arrayOf(MBMappingArgs.mapping_first_image_media)
8
fieldsMapping.putMap(“img”, “Images”, imageArguments)
9
10
//If getSimpleValues was “true” we would not have a single MBImage but the basic value, so the URL of the image and News class “img” should be a String
11
val getSimpleValues = false;
12
val nws = MBurgerMapper.mapToCustomObject(nkSection, fieldsMapping, News(), getSimpleValues) as News
Copied!
If you need to map images, the SDK will return a MBImages object, which contains an array of MBImage.
If you want an array of URLs you will have to set getSimpleValues to true.
Due to the new nature of Android 9 this functionaity could give you light greylist logcat messages because it uses reflections.
For now on, there should be no problem, but this functionality may change with new Android releases.
Last modified 1yr ago
Copy link