如何解决“不确定如何将游标转换为此方法的返回类型”在ROOM Android中

时间:2019-07-25 08:10:06

标签: android android-room

我正在使用空间来构建简单的便笺应用程序。 这是我的笔记实体类:

@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;
    }
}

这是我的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();
}

这是我的存储库:

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();

    }
}

这是我阅读所有笔记的方法。

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();
        }
    });
    }

数据的返回类型为 ArrayList 。 我读到有关DAO仅使用list和Livedata的问题,并且我使用的是相同的问题,但我无法弄清楚该问题。

我希望它应该返回注释列表。但是我的应用程序崩溃了,该错误消息显示“不确定如何将游标转换为该方法的返回类型”。

2 个答案:

答案 0 :(得分:1)

我通过更新到Androidx解决了该问题。 您可以在此处找到链接: https://developer.android.com/jetpack/androidx/migrate

如果您使用的是android studio 3.2,只需转到REFACTOR-> MIGRATE TO ANDROROIDx。

答案 1 :(得分:0)

您的代码对我有用。我的代码下方

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;
        }
    }

尝试使用我的代码,让我们知道更多帮助