Fazal Jarral
Fazal Jarral

Reputation: 170

How to fix 'Not sure how to convert a Cursor to this method's return type' In ROOM android

I am using room to build simple note application. Here is my note entity class:

@Entity(tableName = "notes")
public class Note {
    @PrimaryKey(autoGenerate = true)
    private long id;
    @ColumnInfo(name = "description")
    private String description;
    @Ignore
    public Note() {
    }

    public Note(String description) {
        this.description = description;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

Here is my DAO:

@Dao
public interface NoteDAO {


    @Insert
    public long[] insertNote(Note... note);

    @Update
    public void updateNote(Note note);

    @Delete
    public void deleteNote(Note note);

    @Query("Select * from notes")
     LiveData<List<Note>> getAllNotes();
}

Here is my Repository :

public class NoteRepository {
    NoteDatabase mNoteDatabase;

    public NoteRepository(Context context) {
        this.mNoteDatabase = NoteDatabase.getInstance(context);
    }
    public void insertNote(Note note){
//        mNoteDatabase.getNodeDAO().insertNote(note);
        new InsertAsync(mNoteDatabase.getNodeDAO()).execute(note);
    }
    public void updateNote(Note note){
    mNoteDatabase.getNodeDAO().updateNote(note);
    }
    public void deleteNote(Note note){
    mNoteDatabase.getNodeDAO().deleteNote(note);
    }
    public LiveData<List<Note>> getAllNotes(){
       return  mNoteDatabase.getNodeDAO().getAllNotes();

    }
}

Here is the method where i am reading allNotes.

private void retrieveNote() {
    mNoteRepository.getAllNotes().observe(this, new Observer<List<Note>>() {
        @Override
        public void onChanged(@Nullable List<Note> notes) {
            if(data.size()>0){
                data.clear();
            }
            if(data!=null){
                data.addAll(notes);
            }
            adapter.notifyDataSetChanged();
        }
    });
    }

The return type of data is ArrayList. I read about the problem that DAO uses list and Livedata only , and i am using the same but i cant figure out the problem.

I expected that it should return a list of notes. But My App crashes and this Error Message Shows "Not sure how to convert a Cursor to this method's return type".

Upvotes: 0

Views: 2544

Answers (2)

Fazal Jarral
Fazal Jarral

Reputation: 170

I fixed the issue by updating to Androidx. You can find the link here: https://developer.android.com/jetpack/androidx/migrate

If you are using android studio 3.2 , simply go to REFACTOR -> MIGRATE TO ANDROIDx.

Upvotes: 1

Hardik Bambhania
Hardik Bambhania

Reputation: 1792

Your code is working for me.Se below my code

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {


    private TextView txt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txt = findViewById(R.id.txt);

        Note note1 = new Note();
        note1.setDescription("Hello");

        Note note2 = new Note();
        note1.setDescription("Hello 2");

        Note note3 = new Note();
        note1.setDescription("Hello 3");

        NoteRepository repository = new NoteRepository(this);
        repository.insertNote(note1);
        repository.insertNote(note2);
        repository.insertNote(note3);


        repository.getAllNotes().observe(this, new Observer<List<Note>>() {
            @Override
            public void onChanged(@Nullable List<Note> notes) {

                for (int i = 0; i < notes.size(); i++) {

                    String data = txt.getText() + notes.get(i).getDescription();
                    txt.setText("," + data);
                }

            }
        });

    }


}

build.gradle(app)

apply plugin: 'com.android.application'
android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.hardik.demo_java"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.support:design:28.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'android.arch.persistence.room:runtime:1.1.1'
    annotationProcessor 'android.arch.persistence.room:compiler:1.1.1'
}

NoteRepository.java

public class NoteRepository {
    NoteDatabase mNoteDatabase;

    public NoteRepository(Context context) {
        this.mNoteDatabase = NoteDatabase.getInstance(context);
    }

    public void insertNote(Note note) {
        mNoteDatabase.getNodeDAO().insertNote(note);
    }

    public void updateNote(Note note) {
        mNoteDatabase.getNodeDAO().updateNote(note);
    }

    public void deleteNote(Note note) {
        mNoteDatabase.getNodeDAO().deleteNote(note);
    }

    public LiveData<List<Note>> getAllNotes() {
        return mNoteDatabase.getNodeDAO().getAllNotes();

    }
}

NoteDatabase.java

@Database(entities = {Note.class}, version = 1)
public abstract class NoteDatabase extends RoomDatabase {

    private static NoteDatabase INSTANCE;

    public abstract NoteDAO getNodeDAO();

    public static NoteDatabase getInstance(Context context) {
        if (INSTANCE == null) {
            INSTANCE =
                    Room.databaseBuilder(context.getApplicationContext(), NoteDatabase.class, "user-database")
                            // allow queries on the main thread.
                            // Don't do this on a real app! See PersistenceBasicSample for an example.
                            .allowMainThreadQueries()
                            .build();
        }
        return INSTANCE;
    }

    public static void destroyInstance() {
        INSTANCE = null;
    }
}

NoteDAO.java

@Dao
public interface NoteDAO {


    @Insert
    public long[] insertNote(Note... note);

    @Update
    public void updateNote(Note note);

    @Delete
    public void deleteNote(Note note);

    @Query("Select * from notes")
    LiveData<List<Note>> getAllNotes();
}

Note.java

@Entity(tableName = "notes")
    public class Note {
        @PrimaryKey(autoGenerate = true)
        private long id;
        @ColumnInfo(name = "description")
        private String description;

        @Ignore
        public Note() {
        }

        public Note(String description) {
            this.description = description;
        }

        public long getId() {
            return id;
        }

        public void setId(long id) {
            this.id = id;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }
    }

Try with my code and let know for more help

Upvotes: 0

Related Questions