0% found this document useful (0 votes)
18 views2 pages

Old Code

This Java code defines a class called MainActivityFinal that extends the AppCompatActivity class. The activity allows users to add names to a SQLite database table called "names" by entering text into an EditText field and clicking an add button. It opens or creates the database, defines an onClick listener for the button to insert new rows into the names table, and closes the database on activity destruction.

Uploaded by

JESSAMAE MAJADAS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views2 pages

Old Code

This Java code defines a class called MainActivityFinal that extends the AppCompatActivity class. The activity allows users to add names to a SQLite database table called "names" by entering text into an EditText field and clicking an add button. It opens or creates the database, defines an onClick listener for the button to insert new rows into the names table, and closes the database on activity destruction.

Uploaded by

JESSAMAE MAJADAS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

package com.example.

storeapplication;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivityFinal extends AppCompatActivity {

private EditText nameEditText;


private Button addButton;
private SQLiteDatabase database;
private View baseline_add_24;

@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

nameEditText = findViewById(R.id.edit_text_name);
baseline_add_24 = findViewById(R.id.baseline_add_24);

// Create or open the database


database = openOrCreateDatabase("MyDatabase", Context.MODE_PRIVATE, null);
database.execSQL("CREATE TABLE IF NOT EXISTS names (id INTEGER PRIMARY KEY
AUTOINCREMENT, name TEXT);");

addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = nameEditText.getText().toString().trim();

if (!name.isEmpty()) {
// Create a new row in the database
ContentValues values = new ContentValues();
values.put("name", name);
long rowId = database.insert("names", null, values);

if (rowId != -1) {
Toast.makeText(MainActivityFinal.this, "Name successfully
added", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivityFinal.this, "Failed to add
name", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(MainActivityFinal.this, "Please enter a name",
Toast.LENGTH_SHORT).show();
}
}
});
}

@Override
protected void onDestroy() {
super.onDestroy();
// Close the database when the activity is destroyed
if (database != null) {
database.close();
}
}
}

You might also like