Mad 26
Mad 26
Palikundwar Practical No :- 26
Roll No :- 27
1. activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.Constra android:id="@+id/buttonNormalInsert"
intLayout
android:layout_width="wrap_content"
xmlns:android="http://schemas.android.co
m/apk/res/android" android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/a android:layout_below="@+id/editTextRec
pk/res-auto" ordNum"
xmlns:tools="http://schemas.android.com/t android:layout_alignLeft="@+id/editText
ools" RecordNum"
android:layout_width="match_parent" android:layout_marginStart="44dp"
android:layout_height="match_parent" android:layout_marginLeft="44dp"
tools:context=".MainActivity"> android:layout_marginTop="44dp"
<EditText
android:fontFamily="@font/poppins_medi
android:id="@+id/editTextRecordNum" um"
android:layout_width="318dp" android:text="Normal Insert"
android:layout_height="50dp"
app:layout_constraintStart_toStartOf="par
android:layout_alignParentLeft="true" ent"
android:layout_alignParentTop="true" app:layout_constraintTop_toBottomOf="
android:layout_marginStart="44dp" @+id/editTextRecordNum" />
android:layout_marginLeft="44dp" <Button
android:layout_marginTop="124dp" android:id="@+id/buttonFastInsert"
android:ems="10"
android:layout_width="wrap_content"
android:fontFamily="@font/poppins_medi
um" android:layout_height="wrap_content"
android:inputType="number"
android:singleLine="true" android:layout_alignBaseline="@+id/butt
android:textColor="#FFFFFF" onNormalInsert"
app:layout_constraintStart_toStartOf="par android:layout_alignBottom="@+id/butto
ent" nNormalInsert"
android:layout_marginTop="44dp"
app:layout_constraintTop_toTopOf="pare android:layout_marginEnd="48dp"
nt" android:layout_marginRight="48dp"
tools:ignore="MissingConstraints" />
<Button android:layout_toRightOf="@+id/buttonN
Name :- Shravan M. Palikundwar Practical No :- 26
Roll No :- 27
ormalInsert" android:layout_alignLeft="@+id/buttonNo
rmalInsert"
android:fontFamily="@font/poppins_medi android:layout_marginStart="16dp"
um" android:layout_marginLeft="16dp"
android:text="Fast Insert" android:layout_marginTop="36dp"
app:layout_constraintEnd_toEndOf="pare android:fontFamily="@font/poppins_medi
nt" um"
android:padding="10dp"
app:layout_constraintTop_toBottomOf=" android:text="Status"
@+id/editTextRecordNum" /> android:textColor="#FFFFFF"
<TextView android:textSize="18sp"
android:id="@+id/textViewStatus"
app:layout_constraintStart_toStartOf="par
android:layout_width="wrap_content" ent"
android:layout_height="wrap_content" app:layout_constraintTop_toBottomOf="
@+id/buttonNormalInsert" />
android:layout_below="@+id/buttonNorm </androidx.constraintlayout.widget.Constr
alInsert" aintLayout>
1. MainActivity.java
@Override findViewById(R.id.buttonFastInsert).setO
protected void onCreate(Bundle nClickListener(handler);
savedInstanceState) { // status TextView tvStatus =
super.onCreate(savedInstanceState); (TextView)
findViewById(R.id.textViewStatus);
setContentView(R.layout.activity_main); }
View.OnClickListener handler = new // we used AsyncTask so it won't block the
View.OnClickListener() { UI thread during inserts.
public void onClick(View v) { class AsyncInsertData extends AsyncTask<String,
switch (v.getId()) { String, String> {
case R.id.buttonNormalInsert: DatabaseHandler databaseHandler;
new String type;
AsyncInsertData("normal").execute();
break;
Name :- Shravan M. Palikundwar Practical No :- 26
Roll No :- 27
long timeElapsed; long lStartTime =
protected AsyncInsertData(String System.nanoTime();
type){ if (type.equals("normal")) {
this.type = type;
this.databaseHandler = new databaseHandler.insertNormal(insertCount
DatabaseHandler(MainActivity.this); );
} } else {
// @type - can be 'normal' or 'fast'
@Override databaseHandler.insertFast(insertCount);
protected void onPreExecute() { }
super.onPreExecute(); // execution finished
tvStatus.setText("Inserting " + long lEndTime =
editTextRecordNum.getText() + " System.nanoTime();
records..."); // display execution time
} timeElapsed = lEndTime -
@Override lStartTime;
protected String } catch (Exception e) {
doInBackground(String... aurl) { e.printStackTrace();
try { }
// get number of records to be return null;
inserted }
int insertCount = protected void onPostExecute(String
Integer.parseInt(editTextRecordNum.getT unused) {
ext().toString()); tvStatus.setText("Done inserting "
+ databaseHandler.countRecords() + "
// empty the table records. Time elapsed: " + timeElapsed /
1000000 + " ms.");
databaseHandler.deleteRecords(); }}}
// keep track of execution time
2. DatabaseHandler.java