Database Integration and vocabulary insertion to database finished
This commit is contained in:
@@ -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" />
|
||||
|
@@ -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();
|
||||
}
|
||||
}
|
@@ -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 + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
@@ -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) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@@ -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();
|
||||
}
|
||||
}
|
@@ -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>
|
Reference in New Issue
Block a user