簡體   English   中英

Android Room TypeConverters無法識別

[英]Android Room TypeConverters not recognized

我正在嘗試實現DateConverter來保持日期。 它非常通用,但我一直得到:

無法弄清楚如何將此字段保存到數據庫中。 您可以考慮為其添加類型轉換器。

我仔細檢查並在數據庫級別和字段級別定義了注釋,它仍然無法解決為字段定義的類型轉換器。

我的成績文件依賴項:

compile('android.arch.persistence.room:runtime:1.0.0-alpha3') {
    exclude group: 'com.google.code.gson'
}
compile('android.arch.persistence.room:rxjava2:1.0.0-alpha3') {
    exclude group: 'com.google.code.gson'
}
testCompile('android.arch.persistence.room:testing:1.0.0-alpha3') {
    exclude group: 'com.google.code.gson'
}
annotationProcessor('android.arch.persistence.room:compiler:1.0.0-alpha3') {
    exclude group: 'com.google.code.gson'
}

在我的AppDatabase中:

@Database(entities = {Location.class, Room.class, Amenity.class, UserModel.class, EventModel.class}, version =1, exportSchema = false)
@TypeConverters({DateConverter.class})
public abstract class AppDatabase extends RoomDatabase {

    public abstract RoomDao getRoomDao();
    public abstract LocationDao getLocationDao();
    public abstract AmenityDao getAmenityDao();
    public abstract UserDao getUserDao();
    public abstract EventDao getEventDao();

}

然后我的DateConverter類看起來像這樣:

public class DateConverter {

    @TypeConverter
    public static Date toDate(Long timestamp) {
        return timestamp == null ? null : new Date(timestamp);
    }

    @TypeConverter
    public static long toTimestamp(Date date) {
        return date == null ? null : date.getTime();
    }
}

我的實體看起來像這樣:

@Entity(tableName = "event")
@TypeConverters({DateConverter.class})
public class EventModel {

    @PrimaryKey
    private String uuid;

    @ColumnInfo(name = "subject")
    private String subject;

    @TypeConverters({DateConverter.class})
    private Date start;

    @TypeConverters({DateConverter.class})
    private Date end;

    @ColumnInfo(name = "confirmed")
    private Boolean confirmed;

    public String getUuid() {return uuid;}
    public void setUuid(String uuid) {this.uuid = uuid;}

    public String getSubject() {return subject;}
    public void setSubject(String subject) {
        this.subject = subject;
    }

    public Date getStart() {return start;}
    public void setStart(Date date) {this.start = date;}

    public Date getEnd() {return end;}
    public void setEnd (Date end) {
        this.end = end;

    }

    public Boolean getConfirmed() {return confirmed;}

    public void setConfirmed(Boolean confirmed) {
        this.confirmed = confirmed;
    }

}

我還遺失了什么嗎? 謝謝!

Kotlin中的等價物是:(至少我正在使用的那個)

class DateConverter {
    @TypeConverter
    fun toDate(timestamp: Long?): Date? {
        return when (timestamp) {
            null -> null
            else -> Date(timestamp)
        }
    }

    @TypeConverter
    fun toTimestamp(date: Date?): Long? {
        return date?.time
    }
}

然后你就擁有了你的數據庫:

@Database(entities = arrayOf(MyObject::class), version = 1)
@TypeConverters(DateConverter::class)
abstract class MyObjectDb : RoomDatabase() {
    abstract fun myObjectDao(): MyObjectDao
}

嘗試改變:

public static long toTimestamp(Date date)

至:

public static Long toTimestamp(Date date)

或者,將另一個參數更改為long IOW,讓他們使用相同的類型。

例如,在此示例項目中 ,我使用以下類,它工作正常:

/***
 Copyright (c) 2017 CommonsWare, LLC
 Licensed under the Apache License, Version 2.0 (the "License"); you may not
 use this file except in compliance with the License. You may obtain    a copy
 of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
 by applicable law or agreed to in writing, software distributed under the
 License is distributed on an "AS IS" BASIS,    WITHOUT WARRANTIES OR CONDITIONS
 OF ANY KIND, either express or implied. See the License for the specific
 language governing permissions and limitations under the License.

 Covered in detail in the book _Android's Architecture Components_
 https://commonsware.com/AndroidArch
 */

package com.commonsware.android.room;

import android.arch.persistence.room.TypeConverter;
import java.util.Date;

public class TypeTransmogrifier {
  @TypeConverter
  public static Long fromDate(Date date) {
    if (date==null) {
      return(null);
    }

    return(date.getTime());
  }

  @TypeConverter
  public static Date toDate(Long millisSinceEpoch) {
    if (millisSinceEpoch==null) {
      return(null);
    }

    return(new Date(millisSinceEpoch));
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM