数据库和 Table 不是使用 SugarORM 创建的

Database and Table Not Being Created With SugarORM

我正在使用 Sugar ORM 1.5 来帮助我执行数据库逻辑,但是我似乎无法在每次打开应用程序时创建数据库或表...

备注class:

package com.example.gaetano.notebook;

import com.orm.SugarRecord;

public class Note extends SugarRecord{

String title;
String note;
boolean done;

public Note() {

}

public Note(String title, String note, boolean done) {
    this.title = title;
    this.note = note;
    this.done = done;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getNote() {
    return note;
}

public void setNote(String note) {
    this.note = note;
}

public boolean isDone() {
    return done;
}

public void setDone(boolean done) {
    this.done = done;
}
}

然后我将我的 AndroidManifest 文件更新为此。

<application
    android:allowBackup="true"
    android:label="@string/app_name"
    android:icon="@mipmap/ic_launcher"
    android:theme="@style/AppTheme"
    android:name="com.orm.SugarApp">

    <meta-data
        android:name="DATABASE"
        android:value="notes.db" />
    <meta-data
        android:name="VERSION"
        android:value="3" />
    <meta-data
        android:name="QUERY_LOG"
        android:value="false" />
    <meta-data
        android:name="DOMAIN_PACKAGE_NAME"
        android:value="com.example.gaetano.notebook" />
</application>

这是我的 MainActivity:

package com.example.gaetano.notebook;

public class MainActivity extends AppCompatActivity implements 
AddNoteFragment.OnNoteAdded,
    DeleteNotesFragment.OnDeleteNotes {

public List<Note> notes = new ArrayList<>();
public List<Note> completedNotes = new ArrayList<>();

public ListView listNotes;
public ListView listCompletedNotes;

Cursor cursor;
SQLiteDatabase db;

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

    Stetho.initializeWithDefaults(this);

    NoteAdapter noteAdapter = new NoteAdapter(this, notes);

    listNotes = (ListView) findViewById(R.id.list_notes);
    listCompletedNotes = (ListView) findViewById(R.id.list_completed_notes);

    listNotes.setAdapter(noteAdapter);

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.add_note) {
        showNoteFragment();
        return true;
    }
    if(id == R.id.delete_notes){
        showDeleteFragment();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

private void showDeleteFragment() {
    DialogFragment deleteFragment = new DeleteNotesFragment();
    deleteFragment.show(getFragmentManager(), "DeleteNoteFragment");
}

private void showNoteFragment() {
    DialogFragment noteFragment = new AddNoteFragment();
    noteFragment.show(getFragmentManager(), "AddNoteFragment");
}

@Override
public void onNoteAdded(Note note) {

}

@Override
public void onNotesDeleted() {
    //noteAdapter.clear();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    cursor.close();
    db.close();
}
}

sugar orm 存在一个已知问题 - 您必须在首次 运行 您的应用程序之前禁用即时 运行,否则将不会创建表格。之后,您可以再次打开它。看看here.