From 1615ed3edf2047ff0a894b68c5d707006e869081 Mon Sep 17 00:00:00 2001 From: peet Date: Wed, 9 Dec 2020 21:09:25 +0100 Subject: [PATCH] Database Integration and vocabulary insertion to database finished --- .idea/codeStyles/Project.xml | 15 +-- .idea/gradle.xml | 1 + .idea/misc.xml | 2 +- app/src/main/AndroidManifest.xml | 2 +- .../git/wdg/vokabel_trainer/MainActivity.java | 52 ---------- .../git/wdg/vokabel_trainer/VocabModel.java | 47 --------- .../wdg/vokabel_trainer/ui/DataHandler.java | 97 +++++++++++++++++++ .../wdg/vokabel_trainer/ui/MainActivity.java | 61 ++++++++++++ .../res/layout/fragment_notifications.xml | 3 +- build.gradle | 2 +- gradle/wrapper/gradle-wrapper.properties | 4 +- 11 files changed, 169 insertions(+), 117 deletions(-) delete mode 100644 app/src/main/java/org/ddnss/sfs/git/wdg/vokabel_trainer/MainActivity.java delete mode 100644 app/src/main/java/org/ddnss/sfs/git/wdg/vokabel_trainer/VocabModel.java create mode 100644 app/src/main/java/org/ddnss/sfs/git/wdg/vokabel_trainer/ui/DataHandler.java create mode 100644 app/src/main/java/org/ddnss/sfs/git/wdg/vokabel_trainer/ui/MainActivity.java diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml index 2ea5962..d6c2ebe 100644 --- a/.idea/codeStyles/Project.xml +++ b/.idea/codeStyles/Project.xml @@ -3,18 +3,9 @@ - diff --git a/.idea/gradle.xml b/.idea/gradle.xml index ac6b0ae..23a89bb 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -15,6 +15,7 @@ diff --git a/.idea/misc.xml b/.idea/misc.xml index 39c65f6..d5d35ec 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index ebeeae7..23b592f 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -10,7 +10,7 @@ android:supportsRtl="true" android:theme="@style/AppTheme"> diff --git a/app/src/main/java/org/ddnss/sfs/git/wdg/vokabel_trainer/MainActivity.java b/app/src/main/java/org/ddnss/sfs/git/wdg/vokabel_trainer/MainActivity.java deleted file mode 100644 index ddec99b..0000000 --- a/app/src/main/java/org/ddnss/sfs/git/wdg/vokabel_trainer/MainActivity.java +++ /dev/null @@ -1,52 +0,0 @@ -package org.ddnss.sfs.git.wdg.vokabel_trainer; - -import android.annotation.SuppressLint; -import android.os.Bundle; -import android.view.View; -import android.widget.*; - -import androidx.appcompat.app.AppCompatActivity; -import androidx.navigation.NavController; -import androidx.navigation.Navigation; -import androidx.navigation.ui.AppBarConfiguration; -import androidx.navigation.ui.NavigationUI; -import androidx.recyclerview.widget.RecyclerView; - -import com.google.android.material.bottomnavigation.BottomNavigationView; - -public class MainActivity extends AppCompatActivity { - - //references - Button btn_add; - EditText text_german_input; - EditText text_english_input; - RecyclerView dbview; - - @SuppressLint("WrongViewCast") - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_main); - BottomNavigationView navView = findViewById(R.id.nav_view); - // Passing each menu ID as a set of Ids because each - // menu should be considered as top level destinations. - AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder( - R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications) - .build(); - NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment); - NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration); - NavigationUI.setupWithNavController(navView, navController); - - btn_add = (Button) findViewById(R.id.btn_add); - text_german_input = (EditText) findViewById(R.id.text_german_input); - text_english_input = (EditText) findViewById(R.id.text_english_input); - dbview = (RecyclerView) findViewById(R.id.dbview); - } - - public void add_entry(){ - //VocabModel vocabmodel = new VocabModel(-1, text_german_input.getText().toString(), text_english_input.getText().toString()); - VocabModel vocabmodel = new VocabModel(1, "hello", "hallo"); - - Toast.makeText(MainActivity.this, vocabmodel.toString(), Toast.LENGTH_LONG).show(); - } -} \ No newline at end of file diff --git a/app/src/main/java/org/ddnss/sfs/git/wdg/vokabel_trainer/VocabModel.java b/app/src/main/java/org/ddnss/sfs/git/wdg/vokabel_trainer/VocabModel.java deleted file mode 100644 index 7a8f051..0000000 --- a/app/src/main/java/org/ddnss/sfs/git/wdg/vokabel_trainer/VocabModel.java +++ /dev/null @@ -1,47 +0,0 @@ -package org.ddnss.sfs.git.wdg.vokabel_trainer; - -public class VocabModel { - - private int id; - private String deutsch; - private String english; - - public VocabModel(int id, String deutsch, String english) { - this.id = id; - this.deutsch = deutsch; - this.english = english; - } - - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - public String getDeutsch() { - return deutsch; - } - - public void setDeutsch(String deutsch) { - this.deutsch = deutsch; - } - - public String getEnglish() { - return english; - } - - public void setEnglish(String english) { - this.english = english; - } - - @Override - public String toString() { - return "VocabModel{" + - "id=" + id + - ", deutsch='" + deutsch + '\'' + - ", english='" + english + '\'' + - '}'; - } -} diff --git a/app/src/main/java/org/ddnss/sfs/git/wdg/vokabel_trainer/ui/DataHandler.java b/app/src/main/java/org/ddnss/sfs/git/wdg/vokabel_trainer/ui/DataHandler.java new file mode 100644 index 0000000..35ebc7a --- /dev/null +++ b/app/src/main/java/org/ddnss/sfs/git/wdg/vokabel_trainer/ui/DataHandler.java @@ -0,0 +1,97 @@ +package org.ddnss.sfs.git.wdg.vokabel_trainer.ui; + +import android.content.Context; +import android.content.CursorLoader; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; + +public class DataHandler { + + public static final String DATABASE_NAME = "vocDB"; + public static final int DATABASE_VERSION = 1; + + public static final String TABLE_NAME = "vocTable"; + public static final String DEUTSCH = "deutsch"; + public static final String ENGLISCH = "englisch"; + + private DataBaseHelper dbHelper; + private Context ctx; + private SQLiteDatabase db; + + public DataHandler(Context ctx) { + this.ctx = ctx; + dbHelper = new DataBaseHelper(ctx); + } + + public void openWrite() { + db = dbHelper.getWritableDatabase(); + } + + public void openRead() { + db = dbHelper.getReadableDatabase(); + } + + public void close() { + dbHelper.close(); + } + + public void addEntry(String german, String english) { + db.execSQL(String.format("INSERT INTO %s VALUES ('%s', '%s');", TABLE_NAME, german, english)); + } + + public int tableSize() { + Cursor c = db.rawQuery("select deutsch ,englisch from vocTable;", null); + + int i = 0; + + if (c.moveToFirst()) { + i++; + c.getString(0); + c.getString(1); + while (c.moveToNext()) { + c.getString(0); + c.getString(1); + i++; + } + } + + c.close(); + return i; + } + + public String[] getRandomData() { + Cursor c = db.rawQuery("SELECT * FROM vocTable ORDER BY RANDOM() LIMIT 1", null); + + return (c.moveToFirst()) ? (new String[] {c.getString(0), c.getString(1)}) : (null); + } + + public String[] getSorted(boolean german) { + Cursor c; + if (german) { + c = db.rawQuery("SELECT * FROM vocTable ORDER BY RANDOM() LIMIT 1", null); + } else { + c = db.rawQuery("SELECT * FROM vocTable ORDER BY RANDOM() LIMIT 1", null); + } + + return (c.moveToFirst()) ? (new String[] {c.getString(0), c.getString(1)}) : (null); + } + + + private static class DataBaseHelper extends SQLiteOpenHelper { + + public DataBaseHelper(Context ctx) { + super(ctx, DATABASE_NAME, null, DATABASE_VERSION); + } + + @Override + public void onCreate(SQLiteDatabase db) { + db.execSQL("create table if not exists vocTable(deutsch text not null, englisch text not null);"); + } + + @Override + public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { + + } + } +} diff --git a/app/src/main/java/org/ddnss/sfs/git/wdg/vokabel_trainer/ui/MainActivity.java b/app/src/main/java/org/ddnss/sfs/git/wdg/vokabel_trainer/ui/MainActivity.java new file mode 100644 index 0000000..ad7db65 --- /dev/null +++ b/app/src/main/java/org/ddnss/sfs/git/wdg/vokabel_trainer/ui/MainActivity.java @@ -0,0 +1,61 @@ +package org.ddnss.sfs.git.wdg.vokabel_trainer.ui; + +import android.os.Bundle; +import android.view.View; +import android.widget.*; + +import androidx.appcompat.app.AppCompatActivity; +import androidx.navigation.NavController; +import androidx.navigation.Navigation; +import androidx.navigation.ui.AppBarConfiguration; +import androidx.navigation.ui.NavigationUI; + +import com.google.android.material.bottomnavigation.BottomNavigationView; + +import org.ddnss.sfs.git.wdg.vokabel_trainer.R; +import org.ddnss.sfs.git.wdg.vokabel_trainer.ui.DataHandler; + +public class MainActivity extends AppCompatActivity { + + //references + private DataHandler dbHandler; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + BottomNavigationView navView = findViewById(R.id.nav_view); + // Passing each menu ID as a set of Ids because each + // menu should be considered as top level destinations. + AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder( + R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications) + .build(); + NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment); + NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration); + NavigationUI.setupWithNavController(navView, navController); + dbHandler = new DataHandler(this); + } + + public void addButtonClicked(View view) { + EditText germanText = findViewById(R.id.text_german_input); + EditText englishText = findViewById(R.id.text_english_input); + + if (String.valueOf(germanText.getText()).equals("")) { + makeToast("Das deutsche Feld darf nicht leer sein!", true, this); + } else if (String.valueOf(englishText.getText()).equals("")) { + makeToast("Das englische Feld darf nicht leer sein!", true, this); + } else { + String gText = String.valueOf(germanText.getText()); + String eText = String.valueOf(englishText.getText()); + dbHandler.openWrite(); + dbHandler.addEntry(gText, eText); + int size = dbHandler.tableSize(); + dbHandler.close(); + makeToast(String.format("Vokabel \" %s - %s \" hinzugefügt! Anzahl der eingetragenen Vokabeln: %s!", gText, eText, size), true, this); + } + } + + public static void makeToast(String txt, boolean isLong, Object obj) { + Toast.makeText((AppCompatActivity) obj, txt, ((isLong) ? (Toast.LENGTH_LONG) : (Toast.LENGTH_SHORT))).show(); + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_notifications.xml b/app/src/main/res/layout/fragment_notifications.xml index fe76475..16d9aed 100644 --- a/app/src/main/res/layout/fragment_notifications.xml +++ b/app/src/main/res/layout/fragment_notifications.xml @@ -12,7 +12,7 @@ android:layout_height="wrap_content" android:layout_marginStart="161dp" android:layout_marginTop="388dp" - android:onClick="add_entry" + android:onClick="addButtonClicked" android:text="Einfügen" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> @@ -49,4 +49,5 @@ android:text="English" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> + \ No newline at end of file diff --git a/build.gradle b/build.gradle index 1e1003e..04baf1e 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:4.0.2' + classpath 'com.android.tools.build:gradle:4.1.1' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 939656d..edebb6b 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Tue Sep 29 18:46:28 CEST 2020 +#Wed Dec 09 20:45:31 CET 2020 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip