KEMBAR78
Android CRUD App with SQLite | PDF | Android (Operating System) | String (Computer Science)
0% found this document useful (0 votes)
56 views3 pages

Android CRUD App with SQLite

This Android code defines a class called MainActivity that allows for CRUD (create, read, update, delete) database operations. It initializes database and UI objects, then defines methods to add, view, update, and delete data from the database. The add method inserts data on a button click, view retrieves and displays all data, update modifies a record, and delete removes a record by ID.

Uploaded by

Aqeel Abbasi
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)
56 views3 pages

Android CRUD App with SQLite

This Android code defines a class called MainActivity that allows for CRUD (create, read, update, delete) database operations. It initializes database and UI objects, then defines methods to add, view, update, and delete data from the database. The add method inserts data on a button click, view retrieves and displays all data, update modifies a record, and delete removes a record by ID.

Uploaded by

Aqeel Abbasi
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/ 3

package com.example.

crud_operation;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


DatabaseHelper myDb;
EditText name,surname,marks,editTextId;
Button btnAddData;
Button btnview;
Button Update;
Button delete;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDb = new DatabaseHelper(this);
name= (EditText)findViewById(R.id.edtname);
surname= (EditText)findViewById(R.id.edtsur);
marks= (EditText)findViewById(R.id.edtmarks);
editTextId=(EditText)findViewById(R.id.edtid);
btnAddData=(Button)findViewById(R.id.btnadd);
btnview=(Button)findViewById(R.id.btnview);
Update=(Button)findViewById(R.id.btnupdate) ;
delete=(Button)findViewById(R.id.btndelete) ;
AddData();
ViewData();
updatedata();
deletedata();
}
public void AddData()
{
btnAddData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isInserted = myDb.insertdata(name.getText().toString(),
surname.getText().toString(),marks.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 ViewData()


{
btnview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor res =myDb.getAllData();
if (res.getCount()==0){
//show message
showmessage("Error","No data 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 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();

}
public void updatedata(){
Update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean
isupdate=myDb.updatedata(editTextId.getText().toString(),name.getText().toString(),
surname.getText().toString(),marks.getText().toString());
if (isupdate==true)
Toast.makeText(MainActivity.this,"Data is
Updated",Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this," Data not
Update",Toast.LENGTH_LONG).show();

}
});
}
public void deletedata()
{
delete.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 is
Deleted",Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this," Data not
Deleted",Toast.LENGTH_LONG).show();
}
});
}
}

You might also like