mirror of
https://github.com/SimpleMobileTools/Simple-Notes.git
synced 2025-04-12 22:51:05 +02:00
insert the default note in the DB at creating
This commit is contained in:
parent
a4a4084260
commit
c5785752ae
@ -33,6 +33,7 @@ dependencies {
|
|||||||
compile 'com.jakewharton:butterknife:8.0.1'
|
compile 'com.jakewharton:butterknife:8.0.1'
|
||||||
compile 'com.github.yukuku:ambilwarna:2.0.1'
|
compile 'com.github.yukuku:ambilwarna:2.0.1'
|
||||||
compile 'com.android.support:design:23.4.0'
|
compile 'com.android.support:design:23.4.0'
|
||||||
|
compile 'com.facebook.stetho:stetho:1.4.1'
|
||||||
|
|
||||||
apt 'com.jakewharton:butterknife-compiler:8.0.1'
|
apt 'com.jakewharton:butterknife-compiler:8.0.1'
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<manifest
|
<manifest
|
||||||
package="com.simplemobiletools.notes"
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android">
|
package="com.simplemobiletools.notes">
|
||||||
|
|
||||||
<application
|
<application
|
||||||
|
android:name=".App"
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
android:icon="@mipmap/launcher"
|
android:icon="@mipmap/launcher"
|
||||||
android:label="@string/app_launcher_name"
|
android:label="@string/app_launcher_name"
|
||||||
|
12
app/src/main/java/com/simplemobiletools/notes/App.java
Normal file
12
app/src/main/java/com/simplemobiletools/notes/App.java
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package com.simplemobiletools.notes;
|
||||||
|
|
||||||
|
import android.app.Application;
|
||||||
|
|
||||||
|
import com.facebook.stetho.Stetho;
|
||||||
|
|
||||||
|
public class App extends Application {
|
||||||
|
public void onCreate() {
|
||||||
|
super.onCreate();
|
||||||
|
Stetho.initializeWithDefaults(this);
|
||||||
|
}
|
||||||
|
}
|
@ -19,8 +19,7 @@ import butterknife.BindView;
|
|||||||
import butterknife.ButterKnife;
|
import butterknife.ButterKnife;
|
||||||
|
|
||||||
public class MainActivity extends SimpleActivity {
|
public class MainActivity extends SimpleActivity {
|
||||||
@BindView(R.id.notes_view)
|
@BindView(R.id.notes_view) EditText mNotesView;
|
||||||
EditText mNotesView;
|
|
||||||
|
|
||||||
private SharedPreferences mPrefs;
|
private SharedPreferences mPrefs;
|
||||||
|
|
||||||
|
@ -1,17 +1,26 @@
|
|||||||
package com.simplemobiletools.notes.databases;
|
package com.simplemobiletools.notes.databases;
|
||||||
|
|
||||||
|
import android.content.ContentValues;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
import android.database.sqlite.SQLiteDatabase;
|
import android.database.sqlite.SQLiteDatabase;
|
||||||
import android.database.sqlite.SQLiteOpenHelper;
|
import android.database.sqlite.SQLiteOpenHelper;
|
||||||
|
|
||||||
|
import com.simplemobiletools.notes.Constants;
|
||||||
|
import com.simplemobiletools.notes.models.Note;
|
||||||
|
|
||||||
public class DBHelper extends SQLiteOpenHelper {
|
public class DBHelper extends SQLiteOpenHelper {
|
||||||
private static final String DB_NAME = "notes.db";
|
private static final String DB_NAME = "notes.db";
|
||||||
private static final int DB_VERSION = 1;
|
private static final int DB_VERSION = 1;
|
||||||
private static final String TABLE = "notes";
|
private static final String TABLE = "notes";
|
||||||
|
private static final String NOTE = "General note";
|
||||||
|
|
||||||
private static final String COL_ID = "id";
|
private static final String COL_ID = "id";
|
||||||
private static final String COL_NAME = "name";
|
private static final String COL_NAME = "name";
|
||||||
private static final String COL_TEXT = "text";
|
private static final String COL_TEXT = "value";
|
||||||
|
|
||||||
|
private Context mContext;
|
||||||
|
private SQLiteDatabase mDb;
|
||||||
|
|
||||||
public static DBHelper newInstance(Context context) {
|
public static DBHelper newInstance(Context context) {
|
||||||
return new DBHelper(context);
|
return new DBHelper(context);
|
||||||
@ -19,20 +28,48 @@ public class DBHelper extends SQLiteOpenHelper {
|
|||||||
|
|
||||||
private DBHelper(Context context) {
|
private DBHelper(Context context) {
|
||||||
super(context, DB_NAME, null, DB_VERSION);
|
super(context, DB_NAME, null, DB_VERSION);
|
||||||
|
mContext = context;
|
||||||
|
mDb = getWritableDatabase();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(SQLiteDatabase db) {
|
public void onCreate(SQLiteDatabase db) {
|
||||||
db.execSQL("CREATE TABLE " + TABLE + "(" +
|
db.execSQL("CREATE TABLE " + TABLE + " (" +
|
||||||
COL_ID + " INTEGER PRIMARY KEY, " +
|
COL_ID + " INTEGER PRIMARY KEY, " +
|
||||||
COL_NAME + " TEXT, " +
|
COL_NAME + " TEXT, " +
|
||||||
COL_TEXT + " TEXT" +
|
COL_TEXT + " TEXT" +
|
||||||
")"
|
")"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
insertFirstNote(db);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void insertFirstNote(SQLiteDatabase db) {
|
||||||
|
final SharedPreferences prefs = mContext.getSharedPreferences(Constants.PREFS_KEY, Context.MODE_PRIVATE);
|
||||||
|
final String text = prefs.getString(Constants.TEXT, "");
|
||||||
|
final Note note = new Note(0, NOTE, text);
|
||||||
|
insertNote(note, db);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void insertNote(Note note, SQLiteDatabase db) {
|
||||||
|
final ContentValues values = fillContentValues(note);
|
||||||
|
db.insert(TABLE, null, values);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void insertNote(Note note) {
|
||||||
|
final ContentValues values = fillContentValues(note);
|
||||||
|
mDb.insert(TABLE, null, values);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ContentValues fillContentValues(Note note) {
|
||||||
|
final ContentValues values = new ContentValues();
|
||||||
|
values.put(COL_NAME, note.getName());
|
||||||
|
values.put(COL_TEXT, note.getText());
|
||||||
|
return values;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user