diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..603b140 --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ +*.iml +.gradle +/local.properties +/.idea/caches +/.idea/libraries +/.idea/modules.xml +/.idea/workspace.xml +/.idea/navEditor.xml +/.idea/assetWizardSettings.xml +.DS_Store +/build +/captures +.externalNativeBuild +.cxx diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..0a8db87 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +CRUD App \ No newline at end of file diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..681f41a --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,116 @@ + + + + + + + +
+ + + + xmlns:android + + ^$ + + + +
+
+ + + + xmlns:.* + + ^$ + + + BY_NAME + +
+
+ + + + .*:id + + http://schemas.android.com/apk/res/android + + + +
+
+ + + + .*:name + + http://schemas.android.com/apk/res/android + + + +
+
+ + + + name + + ^$ + + + +
+
+ + + + style + + ^$ + + + +
+
+ + + + .* + + ^$ + + + BY_NAME + +
+
+ + + + .* + + http://schemas.android.com/apk/res/android + + + ANDROID_ATTRIBUTE_ORDER + +
+
+ + + + .* + + .* + + + BY_NAME + +
+
+
+
+
+
\ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..d291b3d --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,16 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..37a7509 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..7f68460 --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 0000000..c5aac66 --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,29 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 28 + buildToolsVersion "29.0.2" + defaultConfig { + applicationId "com.example.crudapp" + minSdkVersion 15 + targetSdkVersion 28 + versionCode 1 + versionName "1.0" + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) + implementation 'androidx.appcompat:appcompat:1.0.2' + implementation 'androidx.constraintlayout:constraintlayout:1.1.3' + testImplementation 'junit:junit:4.12' + androidTestImplementation 'androidx.test.ext:junit:1.1.0' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' +} diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..f1b4245 --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile diff --git a/app/src/androidTest/java/com/example/crudapp/ExampleInstrumentedTest.java b/app/src/androidTest/java/com/example/crudapp/ExampleInstrumentedTest.java new file mode 100644 index 0000000..fd4d996 --- /dev/null +++ b/app/src/androidTest/java/com/example/crudapp/ExampleInstrumentedTest.java @@ -0,0 +1,27 @@ +package com.example.crudapp; + +import android.content.Context; + +import androidx.test.platform.app.InstrumentationRegistry; +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; + +/** + * Instrumented test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + @Test + public void useAppContext() { + // Context of the app under test. + Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); + + assertEquals("com.example.crudapp", appContext.getPackageName()); + } +} diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..a5121df --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/example/crudapp/DatabaseHelper.java b/app/src/main/java/com/example/crudapp/DatabaseHelper.java new file mode 100644 index 0000000..c74a63f --- /dev/null +++ b/app/src/main/java/com/example/crudapp/DatabaseHelper.java @@ -0,0 +1,69 @@ +package com.example.crudapp; + +import android.content.ContentValues; +import android.content.Context; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; + +/** + * Created by ProgrammingKnowledge on 4/3/2015. + */ +public class DatabaseHelper extends SQLiteOpenHelper { + public static final String DATABASE_NAME = "Student.db"; + public static final String TABLE_NAME = "student_table"; + public static final String COL_1 = "ID"; + public static final String COL_2 = "NAME"; + public static final String COL_3 = "SURNAME"; + public static final String COL_4 = "MARKS"; + + public DatabaseHelper(Context context) { + super(context, DATABASE_NAME, null, 1); + } + + @Override + public void onCreate(SQLiteDatabase db) { + db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,SURNAME TEXT,MARKS INTEGER)"); + } + + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME); + onCreate(db); + } + + public boolean insertData(String name,String surname,String marks) { + SQLiteDatabase db = this.getWritableDatabase(); + ContentValues contentValues = new ContentValues(); + contentValues.put(COL_2,name); + contentValues.put(COL_3,surname); + contentValues.put(COL_4,marks); + long result = db.insert(TABLE_NAME,null ,contentValues); + if(result == -1) + return false; + else + return true; + } + + public Cursor getAllData() { + SQLiteDatabase db = this.getWritableDatabase(); + Cursor res = db.rawQuery("select * from "+TABLE_NAME,null); + return res; + } + + public boolean updateData(String id,String name,String surname,String marks) { + SQLiteDatabase db = this.getWritableDatabase(); + ContentValues contentValues = new ContentValues(); + contentValues.put(COL_1,id); + contentValues.put(COL_2,name); + contentValues.put(COL_3,surname); + contentValues.put(COL_4,marks); + db.update(TABLE_NAME, contentValues, "ID = ?",new String[] { id }); + return true; + } + + public Integer deleteData (String id) { + SQLiteDatabase db = this.getWritableDatabase(); + return db.delete(TABLE_NAME, "ID = ?",new String[] {id}); + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/crudapp/MainActivity.java b/app/src/main/java/com/example/crudapp/MainActivity.java new file mode 100644 index 0000000..8f27073 --- /dev/null +++ b/app/src/main/java/com/example/crudapp/MainActivity.java @@ -0,0 +1,144 @@ +package com.example.crudapp; + +import android.app.AlertDialog; +import android.database.Cursor; +import android.app.Activity; +import android.os.Bundle; +import android.view.Menu; +import android.view.MenuItem; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; +import android.widget.Toast; + +import androidx.appcompat.app.AppCompatActivity; + +import com.example.crudapp.DatabaseHelper; +import com.example.crudapp.R; + + +public class MainActivity extends AppCompatActivity { + DatabaseHelper myDb; + EditText editName,editSurname,editMarks ,editTextId; + Button btnAddData; + Button btnviewAll; + Button btnDelete; + + Button btnviewUpdate; + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + myDb = new DatabaseHelper(this); + + editName = (EditText)findViewById(R.id.editText_name); + editSurname = (EditText)findViewById(R.id.editText_surname); + editMarks = (EditText)findViewById(R.id.editText_Marks); + editTextId = (EditText)findViewById(R.id.editText_id); + btnAddData = (Button)findViewById(R.id.button_add); + btnviewAll = (Button)findViewById(R.id.button_viewAll); + btnviewUpdate= (Button)findViewById(R.id.button_update); + btnDelete= (Button)findViewById(R.id.button_delete); + AddData(); + viewAll(); + UpdateData(); + DeleteData(); + } + public void DeleteData() { + btnDelete.setOnClickListener( + new View.OnClickListener() { + @Override + public void onClick(View v) { + Integer deletedRows = myDb.deleteData(editTextId.getText().toString()); + if(deletedRows > 0) + Toast.makeText(MainActivity.this,"Data Deleted",Toast.LENGTH_LONG).show(); + else + Toast.makeText(MainActivity.this,"Data not Deleted",Toast.LENGTH_LONG).show(); + } + } + ); + } + public void UpdateData() { + btnviewUpdate.setOnClickListener( + new View.OnClickListener() { + @Override + public void onClick(View v) { + boolean isUpdate = myDb.updateData(editTextId.getText().toString(), + editName.getText().toString(), + editSurname.getText().toString(),editMarks.getText().toString()); + if(isUpdate == true) + Toast.makeText(MainActivity.this,"Data Update",Toast.LENGTH_LONG).show(); + else + Toast.makeText(MainActivity.this,"Data not Updated",Toast.LENGTH_LONG).show(); + } + } + ); + } + public void AddData() { + btnAddData.setOnClickListener( + new View.OnClickListener() { + @Override + public void onClick(View v) { + boolean isInserted = myDb.insertData(editName.getText().toString(), + editSurname.getText().toString(), + editMarks.getText().toString() ); + if(isInserted == true) + Toast.makeText(MainActivity.this,"Data Inserted",Toast.LENGTH_LONG).show(); + else + Toast.makeText(MainActivity.this,"Data not Inserted",Toast.LENGTH_LONG).show(); + } + } + ); + } + + public void viewAll() { + btnviewAll.setOnClickListener( + new View.OnClickListener() { + @Override + public void onClick(View v) { + Cursor res = myDb.getAllData(); + if(res.getCount() == 0) { + // show message + showMessage("Error","Nothing found"); + return; + } + + StringBuffer buffer = new StringBuffer(); + while (res.moveToNext()) { + buffer.append("Id :"+ res.getString(0)+"\n"); + buffer.append("Name :"+ res.getString(1)+"\n"); + buffer.append("Surname :"+ res.getString(2)+"\n"); + buffer.append("Marks :"+ res.getString(3)+"\n\n"); + } + + // Show all data + showMessage("Data",buffer.toString()); + } + } + ); + } + + public void showMessage(String title,String Message){ + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setCancelable(true); + builder.setTitle(title); + builder.setMessage(Message); + builder.show(); + } + + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + // Inflate the menu; this adds items to the action bar if it is present. + return super.onCreateOptionsMenu(menu); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + // Handle action bar item clicks here. The action bar will + // automatically handle clicks on the Home/Up button, so long + // as you specify a parent activity in AndroidManifest.xml. + + return super.onOptionsItemSelected(item); + } +} \ No newline at end of file diff --git a/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 0000000..1f6bb29 --- /dev/null +++ b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + diff --git a/app/src/main/res/drawable/ic_launcher_background.xml b/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..0d025f9 --- /dev/null +++ b/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..04385c3 --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,119 @@ + + + + + + + + + + + + + + +