Map your objects from MBurger objects
We are going to create an array of Home objects from the sections returned by the api.
The home objects will be like this:
iOS
Android
1
class Home: NSObject {
2
@objc var title: URL?
3
@objc var homeDescription: String?
4
@objc var images: [MBImage]?
5
}
Copied!
Let’s add a mapping dictionary in Home like this, so that the section can map its elements to the Home object properties.
1
static func mappingDictionary() -> [String: String] {
2
return [“title”: “title”,
3
“description”: “homeDescription”,
4
“images”:“images”]
5
}
Copied!
Now we have to create the array of homes from the MBSections. To do so, let’s add the following in the success block:
1
self.homes = sections.map({ section -> Home in
2
let home = Home()
3
section.mapElements(to: home, withMapping: Home.mappingDictionary())
4
return home
5
}
Copied!
Now in the homes array we will have Homes objects created from the sections.
We should do the same thing in the News and Gallery viewcontrollers, using different object models and a different mapping dictionary.
1
public class Home implements Serializable {
2
private String title, description;
3
private MBImages images;
4
5
public String getTitle() {
6
return title;
7
}
8
9
public void setTitle(String title) {
10
this.title = title;
11
}
12
13
public MBImages getImages() {
14
return images;
15
}
16
17
public void setImages(MBImages images) {
18
this.images = images;
19
}
20
21
public String getDescription() {
22
return description;
23
}
24
25
public void setDescription(String description) {
26
this.description = description;
27
}
28
}
Copied!
We use the MBImages class, which is a representation of images inside a section for convenience.
Now we need to map our MBSection objects to Home objects:
1
public Home mapHome(MBSection section) {
2
MBFieldsMapping fieldsMapping = new MBFieldsMapping();
3
fieldsMapping.putMap(“title”, “title”);
4
fieldsMapping.putMap(“images”, “image”);
5
fieldsMapping.putMap(“description”, “description”);
6
Home h = (Home) MBurgerMapper.mapToCustomObject(section, fieldsMapping, new Home(), false);
7
return h;
8
}
Copied!
Once we have an array of homes, we can show them creating our own custom UI using the Android SDK. We can then do the same procedure for the gallery and news screens.
Last modified 1yr ago
Copy link