Alireza Nezami
Alireza Nezami

Reputation: 715

Find if String[] contains String in Room database Android

I want to search in my table that have a String array and it should be contain a String.

This is my Model

@ColumnInfo(name = DbConstants.ColumnNames.ConversationData.AGENTS_ID)
@SerializedName("agentsId")
public String[] agents;

And this is my TypeConverter

 @TypeConverter
    public String fromStringsToJson(String[] strings) {
        if (strings == null)
            return null;
        return new Gson().toJson(strings);
    }

@TypeConverter
public String[] fromJsonToStrings(String string) {
    if (string == null)
        return null;

    Gson gson = new Gson();
    Type type = new TypeToken<String[]>() {
    }.getType();
    return gson.fromJson(string, type);
}

@TypeConverter
public String fromStringMapToJson(Map<String, String> map) {
    if (map == null)
        return null;

    Gson gson = new Gson();
    Type type = new TypeToken<Map<String, String>>() {
    }.getType();
    return gson.toJson(map, type);
}

@TypeConverter
public Map<String, String> fromJsonToMap(String json) {
    if (json == null)
        return null;

    Gson gson = new Gson();
    Type type = new TypeToken<Map<String, String>>() {
    }.getType();
    return gson.fromJson(json, type);
}

And this is DAO Query

@Query("select * from conversations where channelId = :channelId AND " +
        " agentsId LIKE :agentId ORDER BY updateTimeStamp DESC")
List<ConversationData> getMyConversations(String channelId, String agentId);

Everything has been set perfectly. I suppose the problem is one of Converter or the Query

But it can't find agentId in string array.

Upvotes: 3

Views: 2539

Answers (1)

Alireza Nezami
Alireza Nezami

Reputation: 715

For those who seek for the answer.

The problem was with my Query. I just updated it with this.

@Query("select * from conversations where channelId = :channelId AND status = :status AND" +
            " agentsId LIKE '%' || :agentId || '%' ")

Hope it helps.

Upvotes: 5

Related Questions