我使用Sugar ORM 1.5帮助我完成数据库逻辑,但每当我打开应用程序时,我似乎无法创建数据库或表格。
注意课程:
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();
}
}