Search code examples
androidandroid-sqlitesugarorm

Database and Table Not Being Created With SugarORM


I'm using Sugar ORM 1.5 to help me do my database logic, however I can't seem to be creating a database or tables whenever I open the application...

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

Then I updated my AndroidManifest File to to this.

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

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

Solution

  • There is a known issue with sugar orm - you must disable instant run before you run your app for the first time, or the tables will not be created. After that, you can turn it on again. Take a look here.