我从货币兑换API获取数据并将其保存到房间。我想提取2个值 从我保存的数据中,比如说,用户想把120美元兑换成欧元,我会从我的房间数据库中取出美元的兑换率和欧元的兑换率,然后进行相应的数学运算,但是我不知道如何得到这两个值 从我保存为散列表数据中
我写了一段这样的代码
刀
@Dao
interface ExchangeDao {
@Query("SELECT * FROM ExchangeValues WHERE conversion_rates=:currency")
suspend fun getConversionRateByCurrency(currency : String) : Double
}
实体
@Entity(tableName = "ExchangeValues")
data class ExchangeEntity(
@ColumnInfo(name = "base_code") val base_code: String,
@ColumnInfo(name = "conversion_rates") val conversion_rates: HashMap<String,Double>,
@ColumnInfo(name = "result") val result: String,
@PrimaryKey(autoGenerate = true) val uid:Int?=null
)
存储库实现
class ExchangeRepositoryImpl @Inject constructor(
private val dao:ExchangeDao,
private val api: ExchangeApi
) : ExchangeRepository{
override suspend fun getConversionRateByCurrency(currency: String): Double {
return dao.getConversionRateByCurrency(currency)
}
}
存储库
interface ExchangeRepository {
suspend fun getConversionRateByCurrency(currency : String) : Double
}
使用案例
class GetConversionRateByCurrencyUseCase @Inject constructor(
private val repository: ExchangeRepository
) {
suspend fun getConversionRateByCurrency(currency:String) : Double {
return repository.getConversionRateByCurrency(currency)
}
}
但它给出了这样的错误
error: Not sure how to convert a Cursor to this method's return type (java.lang.Double).
public abstract java.lang.Object getConversionRateByCurrency(@org.jetbrains.annotations.NotNull()
error: The columns returned by the query does not have the fields [value] in java.lang.Double even though they are annotated as non-null or primitive. Columns returned by the query: [base_code,conversion_rates,result,uid]
public abstract java.lang.Object getConversionRateByCurrency(@org.jetbrains.annotations.NotNull()
1条答案
按热度按时间nhjlsmyf1#
要从Room数据库中得HashMap获取多个值,可以执行以下操作:
定义一个实体类,该实体类具有HashMap的主键和列。例如:
定义一个Dao(数据访问对象),该对象具有通过实体的主键获取实体并从HashMap返回值的方法。例如:
在代码中,调用Dao方法,并为要从HashMap获取的值提供主键和键。例如:
这将返回一个字符串数组,其中包含HashMap中指定键的值。然后,您可以根据需要在代码中使用这些值。