Home > Enterprise >  Room database - adding "whole pojo schema" into the database, instead of the data I want
Room database - adding "whole pojo schema" into the database, instead of the data I want

Time:08-14

I've tried to follow along some room tutorial, and wanted to display data from the database in spinner, but I've got some strange output. How I can just add the data I want, instead of this weird schema?

What I can see in spinner

CurrencyDatabaseModel(currency=USD)
CurrencyDatabaseModel(currency=EUR)

and so on.

What I would like to get

EUR
USD
...

CurrencyDatabaseModel

    @Entity(tableName = "currencies")
data class CurrencyDatabaseModel(
    @PrimaryKey @ColumnInfo(name = "currency_name") val currency: String
)

DAO

    @Dao
interface CurrencyDAO {

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    suspend fun insertNewCurrency(databaseModel: CurrencyDatabaseModel)

    @Query("SELECT * FROM currencies ORDER BY currency_name")
    fun getAllCurrencies(): Flow<List<CurrencyDatabaseModel>>
}

Repository

    class CurrencyDatabaseRepository(private val currencyDAO: CurrencyDAO) {

    val allCurrencyNamesModel: Flow<List<CurrencyDatabaseModel>> = currencyDAO.getAllCurrencies()

    @Suppress("RedundantSuspendModifier")
    @WorkerThread
    suspend fun insertNewCurrency(currencyDatabaseModel: CurrencyDatabaseModel){
        currencyDAO.insertNewCurrency(currencyDatabaseModel)
    }
}

ViewModel

class CurrencyDatabaseViewModel(private val currencyDatabaseRepository: CurrencyDatabaseRepository) :
ViewModel() {

    val currencyNames = mutableListOf<CurrencyDatabaseModel>()

    val allCurrencies: LiveData<List<CurrencyDatabaseModel>> = currencyDatabaseRepository.allCurrencyNamesModel.asLiveData()
}


class CurrencyDatabaseFactory(private val currencyDatabaseRepository: CurrencyDatabaseRepository) :
    ViewModelProvider.Factory {
    override fun <T : ViewModel> create(modelClass: Class<T>): T {
        if(modelClass.isAssignableFrom(CurrencyDatabaseViewModel::class.java)){
            @Suppress("UNCHECKED_CAST")
            return CurrencyDatabaseViewModel(currencyDatabaseRepository) as T
        }
        throw IllegalArgumentException("Unknown ViewModel class DB")
    }
}

CodePudding user response:

So this is basically what is happening and how you code works:

  1. you add data into spinner.
  2. Spinner Calls toString() function of data class CurrencyDatabaseModel()
  3. which looks something like this:
@Entity(tableName = "currencies")
data class CurrencyDatabaseModel(
    @PrimaryKey
    @ColumnInfo(name = "currency_name")
    val currency: String
) {
    override fun toString(): String {
        return super.toString()
    }
}

This is what is the cause of your problem.

Try changing the Data class as follows:

@Entity(tableName = "currencies")
data class CurrencyDatabaseModel(
    @PrimaryKey
    @ColumnInfo(name = "currency_name")
    val currency: String
) {
    override fun toString(): String {
        return this.currency
    }
}

I believe this should do the trick.

  • Related