Search code examples
androidandroid-sqliteandroid-contentproviderandroid-roomandroid-architecture-components

Combine Room and Content Resolver data into one object


My app supports adding contacts to a note.

In this process, I'm storing a Contact entity in my apps database, where I primarily use the contactId to identify the contact.

In my UI, the name should be displayed, too. Therefore, I'm looking for a possibility to create a combined query (like a INNER JOIN) for fetching my apps contact data together with the name of the contact from the device's address book.

Is this possible?

My contact entity:

data class Contact(
    @PrimaryKey(autoGenerate = true) @ColumnInfo(name = Database.Contacts.COL_ID) val id: Long,
    @ColumnInfo(name = Database.Contacts.COL_CONTACT_ID) val contactId: Long,
    /* This should be fetched from the content provider. */ val contactName: String,
    @ColumnInfo(name = Database.Contacts.COL_TRANSACTION_ID) val transactionId: Long
) : Model

Thank you!


Solution

  • You can't fetch data using single inner join from your app DB and contacts of the phone. You need to retrieve both of them separately. After retrieving them, combine the results. Hope this will help you.