Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get list of cursor object from room dao access?

I am trying to chnage my code to make use of room database APIs. For documents table I have defined Entity class Document, when I query getAll() it returns me all the document.

Now I have old implementation of Adapter which makes user of Cursor ( Its a CursorAdapter ). In my DocumentDao class I defined one method to get list of cursor objects. My Dao class as follows :

@Dao
public interface DocumentDao {

    @Query("SELECT * FROM documents")
    List<com.myapp.room.entity.Document> getAll();

    @Query("SELECT * FROM documents")
    List<Cursor> getCursorAll();
}

During compile time I am getting following error :

Error:(20, 18) error: Not sure how to convert a Cursor to this method's return type

The official guide for Room which states that :

If your app's logic requires direct access to the return rows, you can return a Cursor object from your queries, as shown in the following code snippet:

@Dao
public interface MyDao {
    @Query("SELECT * FROM user WHERE age > :minAge LIMIT 5")
    public Cursor loadRawUsersOlderThan(int minAge);
}

My question is do I need to write converter for this purpose ?

like image 298
Prashant Avatar asked Jul 26 '17 12:07

Prashant


People also ask

What is DAO in room database?

When you use the Room persistence library to store your app's data, you interact with the stored data by defining data access objects, or DAOs. Each DAO includes methods that offer abstract access to your app's database. At compile time, Room automatically generates implementations of the DAOs that you define.

What is room DB Android?

Room is a persistence library that provides an abstraction layer over the SQLite database to allow a more robust database. With the help of room, we can easily create the database and perform CRUD operations very easily.

What is a DAO Android?

android.arch.persistence.room.Dao. Marks the class as a Data Access Object. Data Access Objects are the main classes where you define your database interactions. They can include a variety of query methods. The class marked with @Dao should either be an interface or an abstract class.


1 Answers

You're returning a List<Cursor> instead of a Cursor. Change:

@Query("SELECT * FROM documents")
List<Cursor> getCursorAll();

for

@Query("SELECT * FROM documents")
Cursor getCursorAll();
like image 79
Alberto S. Avatar answered Oct 24 '22 01:10

Alberto S.