2016-08-13 4 views
1

私はAndroidアプリを開発中です。アプリの目的は、広告タイトル、広告説明、広告画像パス(画像パス多くのことができます)。複数のイメージを1つのエントリとしてレルムデータベースに文字列として保存する方法

私はこの操作にレルムデータベースを使用しています。これらの情報をデータベースに保存する際に問題が生じています。 1つの広告エントリで複数の画像パスを保存するにはどうすればよいですか?

私のフィールドは次のとおりです。

Ad Title 
Ad Description 
Ad Image Path (Multi) 

これは私のRealmObjectクラス

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

答えて

0

単純な直感的な方法は、それらを連結して、それらを単一の文字列として格納することです。また、検索する場合は、Realmデータベースから文字列を取り出し、区切り文字で区切ります。例えば

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] 
+0

恐ろしいです!私はRepoのために投票することができませんが、あなたのためにくすぐります:-) –

+0

とあなたはタグについて何を言いますか、後でこれらのタグを検索目的に使用しますか? –

+0

@ YasirAmeenはい、同じ*エンコーディング*メカニズムをタグに適用することもできます。 –

0

ある よくお読みください。まず変換してください。文字列とバイト配列(バイト[])にすることはできません16 MBより大きい

レルム:ByteArrayの 私は与えているデモ

Bitmap bmp = intent.getExtras().get("data"); 
ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); 
byte[] byteArray = stream.toByteArray(); 

、その後、レルムデータベース

お知らせにバイト[]を保存への画像boolean、byte、short、int、long、float、double、String、Date、byte []のフィールド型をサポートしています。整数型byte、short、int、およびlongは、すべてRealm内で同じ型(実際はlong型)にマップされます。さらに、RealmObjectとRealmListのサブクラスは、リレーションシップをモデル化するためにサポートされています。

+0

は答えてくれてありがとう、しかし、私は文字列として複数の画像のパスを保存したいです。私は自分の質問を編集しました –

+0

この例を参考にしてください –

+0

http://www.androidhive.info/2016/05/android-working-with-realm-database-replacing-sqlite-core-data/ –

関連する問題