kotlin 如何从房间数据库中的Hashmap中获取两个数据?

rryofs0p  于 2022-12-04  发布在  Kotlin
关注(0)|答案(1)|浏览(181)

我从货币兑换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()
nhjlsmyf

nhjlsmyf1#

要从Room数据库中得HashMap获取多个值,可以执行以下操作:
定义一个实体类,该实体类具有HashMap的主键和列。例如:

@Entity(primaryKeys = {"id"})
public class MyEntity {
    private int id;
    @ColumnInfo(name = "data_map")
    private HashMap<String, String> dataMap;

    public MyEntity(int id, HashMap<String, String> dataMap) {
        this.id = id;
        this.dataMap = dataMap;
    }

    public int getId() {
        return id;
    }

    public HashMap<String, String> getDataMap() {
        return dataMap;
    }
}

定义一个Dao(数据访问对象),该对象具有通过实体的主键获取实体并从HashMap返回值的方法。例如:

@Dao
public interface MyDao {
    @Query("SELECT * FROM my_entity WHERE id = :id")
    MyEntity getEntityById(int id);

    // Method to get two values from the HashMap in MyEntity
    @Transaction
    default String[] getData(int id, String key1, String key2) {
        MyEntity entity = getEntityById(id);
        if (entity == null) {
            return null;
        }

        HashMap<String, String> dataMap = entity.getDataMap();
        String value1 = dataMap.get(key1);
        String value2 = dataMap.get(key2);
        return new String[] {value1, value2};
    }
}

在代码中,调用Dao方法,并为要从HashMap获取的值提供主键和键。例如:

// Get an instance of the database
MyDatabase database = MyDatabase.getInstance(context);

// Get the Dao
MyDao dao = database.myDao();

// Call the Dao method and pass the primary key and the keys for the values you want to get
int id = 1;
String[] data = dao.getData(id, "key1", "key2");

这将返回一个字符串数组,其中包含HashMap中指定键的值。然后,您可以根据需要在代码中使用这些值。

相关问题