Exp26 MAD
Exp26 MAD
Exp26 MAD
activity_main.xml :-
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<AbsoluteLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/nm"
android:layout_width="297dp"
android:layout_height="wrap_content"
android:layout_x="53dp"
android:layout_y="92dp"
android:hint="Enter Student Name" />
<EditText
android:id="@+id/no"
android:layout_width="297dp"
android:layout_height="wrap_content"
android:layout_x="52dp"
android:layout_y="158dp"
android:hint="Enter Student Mob.No" />
<EditText
android:id="@+id/mail"
android:layout_width="297dp"
android:layout_height="wrap_content"
android:layout_x="53dp"
android:layout_y="225dp"
android:hint="Enter Student Email" />
<Button
android:layout_width="147dp"
android:layout_height="wrap_content"
android:layout_x="200dp"
android:layout_y="326dp"
android:background="@color/white"
android:onClick="save"
android:text="Save"
android:textSize="20sp"/>
<Button
android:layout_width="147dp"
android:layout_height="wrap_content"
android:layout_x="40dp"
android:layout_y="326dp"
android:background="@color/white"
android:onClick="display"
android:text="Display"
android:textSize="20sp"/>
</AbsoluteLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java :-
package com.example.exp26_st1_08;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nm = findViewById(R.id.nm);
no = findViewById(R.id.no);
mail = findViewById(R.id.mail);
db = new Database(this);
async = new MyAsyncTask(this);
}
Asynctask.java :-
package com.example.exp26_st1_08;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
MyAsyncTask(Context c) {
context = c;
db = new Database(context);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
al = new AlertDialog.Builder(context);
}
@Override
protected Long doInBackground(String... string) {
String nm = string[0];
String no = string[1];
String mail = string[2];
long result = db.insert(nm, no, mail);
return result;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(Long aLong) {
super.onPostExecute(aLong);
Toast.makeText(context, "Values inserted", Toast.LENGTH_SHORT).show();
}
}
Database.java :-
package com.example.exp26_st1_08;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
class Database extends SQLiteOpenHelper {
public Database(Context c) {
super(c, "std_info_db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE std_tbl (name TEXT, contact TEXT, mail TEXT);");
}
public long insert(String nm, String no, String mail) {
SQLiteDatabase sd = this.getWritableDatabase();
ContentValues ctx = new ContentValues();
ctx.put("name", nm);
ctx.put("contact", no);
ctx.put("mail", mail);
return sd.insert("std_tbl", null, ctx);
}
public Cursor getData() {
SQLiteDatabase db = this.getReadableDatabase();
return db.rawQuery("SELECT * FROM std_tbl", null);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// This method is optional, you can leave it empty if you don't need to upgrade the
database schema
}
}
• Output :-