Database Integration and vocabulary insertion to database finished

This commit is contained in:
peet 2020-12-09 21:09:25 +01:00
parent 9f7623869a
commit 1615ed3edf
11 changed files with 169 additions and 117 deletions

View File

@ -3,18 +3,9 @@
<JetCodeStyleSettings>
<option name="PACKAGES_TO_USE_STAR_IMPORTS">
<value>
<package name="java.util" alias="false" withSubpackages="false" />
<package name="kotlinx.android.synthetic" alias="false" withSubpackages="true" />
<package name="io.ktor" alias="false" withSubpackages="true" />
</value>
</option>
<option name="PACKAGES_IMPORT_LAYOUT">
<value>
<package name="" alias="false" withSubpackages="true" />
<package name="java" alias="false" withSubpackages="true" />
<package name="javax" alias="false" withSubpackages="true" />
<package name="kotlin" alias="false" withSubpackages="true" />
<package name="" alias="true" withSubpackages="true" />
<package name="java.util" withSubpackages="false" static="false" />
<package name="kotlinx.android.synthetic" withSubpackages="true" static="false" />
<package name="io.ktor" withSubpackages="true" static="false" />
</value>
</option>
</JetCodeStyleSettings>

View File

@ -15,6 +15,7 @@
</set>
</option>
<option name="resolveModulePerSourceSet" value="false" />
<option name="useQualifiedModuleNames" value="true" />
</GradleProjectSettings>
</option>
</component>

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">

View File

@ -10,7 +10,7 @@
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:name=".ui.MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

View File

@ -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();
}
}

View File

@ -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 + '\'' +
'}';
}
}

View File

@ -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) {
}
}
}

View File

@ -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();
}
}

View File

@ -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" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

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

View File

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