Search code examples
androidrealm

How to save multiple image Path as String in realm database as one entry


I am working on an Android app, purpose of app is to publish Ads by taking some ads info like Ad Title, Ad Description and Ad Image Path (Image Path can be many).

I am using realm database for this operation. I am getting difficulty when saving these information into database. I want to know, how can I save multiple image path with one ad entry?

My Fields are:

Ad Title  
Ad Description  
Ad Image Path (Multi)

This is my RealmObject Class

public class FacebookAds extends RealmObject {

@PrimaryKey
@Index
private int id;
private String title;
private String tags;
private String description;
private String imageName; // How to use this for multiple image Path

public  FacebookAds() {

}

public String getImageName() {
    return imageName;
}

public void setImageName(String imageName) {
    this.imageName = imageName;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getTags() {
    return tags;
}

public void setTags(String tags) {
    this.tags = tags;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

}

This is my Ad new Title Dialog


Solution

  • A simple intuitive way would be concatenating them and then storing them as a single string. And when you want to retrieve them, simply fetch the string from your Realm database and split it by the separator character.

    For example:

    String imagePath1 = ...
    String imagePath2 = ...
    
    // Store this string
    String imagePath = imagePath1 + "|" + imagePath2;
    
    // Retrieve paths like this
    String[] paths = imagePath.split("|");
    
    // imagePath1
    paths[0]
    
    // imagePath2
    paths[1]