0% found this document useful (0 votes)
20 views11 pages

Ex 26

The document provides a detailed implementation of an Android application that utilizes SQLite for data storage. It includes XML layout files for the main activity and display contacts, as well as Java code for managing database operations using AsyncTask. The application allows users to insert, update, and delete contact information within a user-friendly interface.

Uploaded by

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

Ex 26

The document provides a detailed implementation of an Android application that utilizes SQLite for data storage. It includes XML layout files for the main activity and display contacts, as well as Java code for managing database operations using AsyncTask. The application allows users to insert, update, and delete contact information within a user-friendly interface.

Uploaded by

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

X.

Exercise
1. Write a program to insert data in SQLite database using AsyncTask.

activity_main.xml android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout <ListView
xmlns:android="http://schemas.android.com/apk/r
android:layout_width="match_parent"
es/android"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/apk/tool android:id="@+id/listView1"
s"
android:layout_centerHorizontal="true"
android:layout_width="fill_parent"
android:layout_centerVertical="true">
android:layout_height="wrap_content"
android:padding="20dp"
</ListView>
tools:context=".MainActivity">
</ScrollView>

<TextView
</RelativeLayout>
android:id="@+id/textView"
android:layout_width="wrap_content"
MainActivity.java
android:layout_height="wrap_content"
package com.example.ex2601;
android:layout_centerVertical="true"
android:textStyle="bold"
import
android:layout_alignParentTop="true" androidx.appcompat.app.AppCompatActivity;
android:layout_centerHorizontal="true" import android.os.AsyncTask;
android:textSize="30dp" import android.content.Intent;
android:text="Database"/> import android.database.Cursor;
import android.view.KeyEvent;
<ScrollView import android.view.Menu;
android:layout_width="wrap_content" import android.view.MenuItem;
android:layout_height="wrap_content" import android.view.View;
android:id="@+id/imageView" import
android.view.MenuItem.OnMenuItemClickListen
android:layout_alignParentLeft="true"
er;
android:layout_alignParentStart="true"
import android.widget.AdapterView;
android:layout_alignParentBottom="true"
import android.widget.ArrayAdapter;
import android.widget.CursorAdapter; int id_To_Search = arg2 + 1;
import android.widget.ListView; Bundle dataBundle = new Bundle();
import android.widget.SimpleCursorAdapter; dataBundle.putInt("id",id_To_Search);
import Intent intent = new
android.widget.AdapterView.OnItemClickListene Intent(getApplicationContext(),DisplayContact.cla
r; ss);
import android.os.Bundle; intent.putExtras(dataBundle);
startActivity(intent);
import java.util.ArrayList; }
});
public class MainActivity extends }
AppCompatActivity {

@Override
public final static String EXTRA_MESSAGE =
public boolean onCreateOptionsMenu(Menu
"MESSAGE";
menu) {
private ListView obj;
DatabaseHelper mydb; getMenuInflater().inflate(R.menu.main_menu,me
nu);
return true;
@Override
}
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState); @Override
setContentView(R.layout.activity_main); public boolean
onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
mydb = new DatabaseHelper(this);
switch(item.getItemId()) {
ArrayList array_list = mydb.getAllContacts();
case R.id.item1:Bundle dataBundle = new
ArrayAdapter arrayAdapter = new
Bundle();
ArrayAdapter(this,
android.R.layout.simple_list_item_1,array_list); dataBundle.putInt("id",0);
obj = (ListView) Intent intent = new
findViewById(R.id.listView1); Intent(getApplicationContext(),DisplayContact.cla
ss);
obj.setAdapter(arrayAdapter);
startActivity(intent);
obj.setOnItemClickListener(new
OnItemClickListener() { return true;
@Override default:
public void onItemClick(AdapterView<?> return
arg0, View arg1, int arg2, long arg3) { super.onOptionsItemSelected(item);
} android:id="@+id/editTextName"
} android:layout_width="wrap_content"
android:layout_height="wrap_content"
public boolean onKeyDown(int keycode, android:layout_alignParentLeft="true"
KeyEvent event) {
android:layout_marginLeft="156dp"
if(keycode ==
android:layout_marginTop="5dp"
KeyEvent.KEYCODE_BACK) {
android:ems="10"
moveTaskToBack(true);
android:inputType="text">
}
<requestFocus
return super.onKeyDown(keycode, event);
android:layout_width="wrap_content"
}
android:layout_height="wrap_content"
}
/>

activity_display_contacts.xml
</EditText>

<?xml version="1.0" encoding="utf-8"?>


<EditText
<ScrollView
android:id="@+id/editTextEmail"
xmlns:android="http://schemas.android.com/apk/r
es/android" android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-
auto"
android:layout_below="@+id/editTextStreet"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollView1" android:layout_alignLeft="@+id/editTextStreet"
android:layout_width="match_parent" android:layout_marginTop="22dp"
android:layout_height="wrap_content" android:ems="10"
android:layout_gravity="center" android:inputType="textEmailAddress" />
app:layout_anchorGravity="center"
tools:context=".DisplayContact"> <TextView
android:id="@+id/textView1"
<RelativeLayout android:layout_width="wrap_content"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_height="370dp"
android:layout_alignBottom="@+id/editTextNam
android:layout_gravity="center">
e"
android:layout_alignParentLeft="true"
<EditText
android:layout_marginLeft="36dp"
android:layout_alignLeft="@+id/textView1"
android:layout_marginBottom="1dp"
android:text="Name"
android:layout_alignBottom="@+id/editTextPhon
e"
android:textAppearance="?android:attr/textAppea
android:text="Phone"
ranceMedium" />

android:textAppearance="?android:attr/textAppea
<Button ranceMedium" />

android:id="@+id/button1"
android:layout_width="wrap_content" <TextView

android:layout_height="wrap_content" android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_alignLeft="@+id/editTextCity"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="28dp" android:layout_above="@+id/editTextEmail"

android:onClick="run"
android:layout_alignLeft="@+id/textView5"
android:text="save" />
android:text="Street"

<TextView android:textAppearance="?android:attr/textAppea
android:id="@+id/textView2" ranceMedium" />

android:layout_width="wrap_content"
android:layout_height="wrap_content" <EditText
android:id="@+id/editTextCity"
android:layout_alignLeft="@+id/textView1" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/editTextEmai
l"
android:layout_below="@+id/editTextEmail"
android:text="Email"

android:layout_alignRight="@+id/editTextName"
android:textAppearance="?android:attr/textAppea
ranceMedium" /> android:layout_marginTop="30dp"
android:ems="10"

<TextView android:inputType="text" />

android:id="@+id/textView5"
android:layout_width="wrap_content" <TextView

android:layout_height="wrap_content" android:id="@+id/textView3"
android:layout_width="wrap_content" android:ems="10"
android:layout_height="wrap_content" android:inputType="phone|text" />

android:layout_alignBaseline="@+id/editTextCit
</RelativeLayout>
y"
</ScrollView>
android:layout_alignBottom="@+id/editTextCity"
android:layout_alignParentLeft="true"
android:layout_marginLeft="36dp" DisplayContact.java
android:layout_marginRight="-36dp"
android:layout_marginBottom="13dp" package com.example.ex2601;

android:layout_toLeftOf="@+id/editTextEmail"
import androidx.appcompat.app.AlertDialog;
android:text="Country"
import
androidx.appcompat.app.AppCompatActivity;
android:textAppearance="?android:attr/textAppea
ranceMedium" />
import android.content.DialogInterface;

<EditText import android.os.AsyncTask;

android:id="@+id/editTextStreet" import android.content.Intent;

android:layout_width="wrap_content" import android.database.Cursor;

android:layout_height="wrap_content" import android.view.Menu;


import android.view.MenuItem;
android:layout_below="@+id/editTextPhone" import android.view.View;
import
android:layout_alignLeft="@+id/editTextName" android.view.MenuItem.OnMenuItemClickListen
android:ems="10" er;

android:inputType="text"/> import android.widget.AdapterView;


import android.widget.Button;

<EditText import android.widget.CursorAdapter;

android:id="@+id/editTextPhone" import android.widget.ListView;

android:layout_width="wrap_content" import android.widget.SimpleCursorAdapter;

android:layout_height="wrap_content" import
android.widget.AdapterView.OnItemClickListene
r;
android:layout_below="@+id/editTextName"
import android.os.Bundle;

android:layout_alignLeft="@+id/editTextStreet" import android.widget.TextView;


import android.widget.Toast;
int Value = extras.getInt("id");
public class DisplayContact extends if(Value>0) {
AppCompatActivity {
Cursor rs = mydb.getData(Value);
int from_Where_I_Am_Coming = 0;
id_To_Update = Value;
private DatabaseHelper mydb;
rs.moveToFirst();
TextView name;
String nam =
TextView phone; rs.getString(rs.getColumnIndex(DatabaseHelper.C
OLUMN_NAME));
TextView email;
String phon =
TextView street;
rs.getString(rs.getColumnIndex(DatabaseHelper.C
TextView place; OLUMN_PHONE));

int id_To_Update = 0; String emai =


rs.getString(rs.getColumnIndex(DatabaseHelper.C
OLUMN_EMAIL));
@Override String stree =
protected void onCreate(Bundle rs.getString(rs.getColumnIndex(DatabaseHelper.C
savedInstanceState) { OLUMN_STREET));

super.onCreate(savedInstanceState); String plac =


rs.getString(rs.getColumnIndex(DatabaseHelper.C
OLUMN_CITY));
setContentView(R.layout.activity_display_contact
);
if(!rs.isClosed()) {

name = (TextView) rs.close();


findViewById(R.id.editTextName); }
phone = (TextView)
findViewById(R.id.editTextPhone);
Button b = (Button)
email = (TextView) findViewById(R.id.button1);
findViewById(R.id.editTextEmail);
b.setVisibility(View.INVISIBLE);
street = (TextView)
findViewById(R.id.editTextStreet); name.setText((CharSequence) nam);

place = (TextView) name.setFocusable(false);


findViewById(R.id.editTextCity);
name.setClickable(false);

mydb = new DatabaseHelper(this);


phone.setText((CharSequence) phon);
phone.setFocusable(false);
Bundle extras = getIntent().getExtras();
phone.setClickable(false);

if (extras != null) {
email.setText((CharSequence) emai); public boolean
onOptionsItemSelected(MenuItem item) {
email.setFocusable(false);
super.onOptionsItemSelected(item);
email.setClickable(false);
switch(item.getItemId()) {
case R.id.Edit_Contact:
street.setText((CharSequence) stree);
Button b = (Button)
street.setFocusable(false);
findViewById(R.id.button1);
street.setClickable(false);
b.setVisibility(View.VISIBLE);
name.setEnabled(true);
place.setText((CharSequence) plac);
name.setFocusableInTouchMode(true);
place.setFocusable(false);
name.setClickable(true);
place.setClickable(false);

phone.setEnabled(true);
}
} phone.setFocusableInTouchMode(false);

} phone.setClickable(false);

@Override email.setEnabled(true);

public boolean onCreateOptionsMenu(Menu email.setFocusableInTouchMode(false);


menu) {
email.setClickable(false);
Bundle extras = getIntent().getExtras();
if(extras != null) {
street.setEnabled(true);
int Value = extras.getInt("id");
street.setFocusableInTouchMode(false);
if(Value>0){
street.setClickable(false);

getMenuInflater().inflate(R.menu.display_contact,
menu); place.setEnabled(true);
}else { place.setFocusableInTouchMode(false);
place.setClickable(false);
getMenuInflater().inflate(R.menu.main_menu,me
nu);
return true;
}
}
case R.id.Delete_Contact:
return true;
AlertDialog.Builder builder = new
}
AlertDialog.Builder(this);
public void run(View view) {
builder.setMessage(R.string.deleteContact)
Bundle extras = getIntent().getExtras();
.setPositiveButton(R.string.yes,
new DialogInterface.OnClickListener() {
if(extras != null) {
@Override
int Value = extras.getInt("id");
public void
onClick(DialogInterface dialogInterface, int id) { if(Value>0) {

mydb.deleteContact(id_To_Update); if(mydb.updateContact(id_To_Update,name.getTe
xt().toString(),
Toast.makeText(getApplicationContext(),"Delete
d Successfully", phone.getText().toString(),email.getText().toStrin
g(),
Toast.LENGTH_SHORT).show();
street.getText().toString(),place.getText().toString
Intent intent = new
())){
Intent(getApplicationContext(),MainActivity.class
);
Toast.makeText(getApplicationContext(),"Update
startActivity(intent);
d",Toast.LENGTH_SHORT).show();
}
} else {
})
.setNegativeButton(R.string.no, Toast.makeText(getApplicationContext(),"not
new DialogInterface.OnClickListener() { Updated", Toast.LENGTH_SHORT).show();
@Override }
public void } else {
onClick(DialogInterface dialogInterface, int id) {
if(mydb.insertContact(name.getText().toString(),
}
phone.getText().toString(),email.getText().toStrin
});
g(),
AlertDialog d = builder.create();
d.setTitle("Are you sure"); street.getText().toString(),place.getText().toString
())) {
d.show();
return true; Toast.makeText(getApplicationContext(),"Done",
default: Toast.LENGTH_SHORT).show();

return } else {
super.onOptionsItemSelected(item);
} Toast.makeText(getApplicationContext(),"Not
done",Toast.LENGTH_SHORT).show();
}
}
Intent intent = new android:layout_height="wrap_content"
Intent(getApplicationContext(),MainActivity.class
android:hint="noteEdit"
);
android:paddingTop="20dp"
startActivity(intent);
android:inputType="textMultiLine" />
}
</LinearLayout>
}
}
</ScrollView>
}

activity_add_edit_notes.xml
activity_view_note.xml

<?xml version="1.0" encoding="utf-8"?>


<ScrollView
xmlns:android="http://schemas.android.com/apk/r <?xml version="1.0" encoding="utf-8"?>
es/android"
<RelativeLayout
android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/r
es/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1" >
android:layout_height="fill_parent"
android:padding="5dp" >
<LinearLayout
android:id="@+id/linearLayout"
<TextView
android:layout_width="fill_parent"
android:id="@+id/Yourtitle"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_height="wrap_content"
android:padding="5dp" >
android:text="YourTitle"
android:textStyle="bold" />
<EditText
android:id="@+id/titleEdit"
<TextView
android:layout_width="fill_parent"
android:id="@+id/TitleText"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:hint="titleEdit" />
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/Yourtitle"
<EditText
android:paddingBottom="10dp"
android:id="@+id/noteEdit"
android:textStyle="bold" />
android:layout_width="fill_parent"
</menu>
<TextView
android:id="@+id/YourNotes" menu/main_menu.xml
android:layout_width="wrap_content"
android:layout_height="wrap_content" <?xml version="1.0" encoding="utf-8"?>
android:layout_below="@+id/TitleText" <menu
xmlns:android="http://schemas.android.com/apk/r
android:text="YourNotes"
es/android">
android:textStyle="bold" />
<item
android:id="@+id/item1"
<TextView
android:id="@+id/NoteText" android:icon="@android:drawable/ic_input_add"

android:layout_width="wrap_content" android:title="Add_New"></item>

android:layout_height="wrap_content" </menu>

android:layout_below="@+id/TitleText"
values/strings.xml
android:layout_toRightOf="@+id/YourNotes"
android:textStyle="bold" />
<resources>
<string name="app_name">EX2601</string>
</RelativeLayout>
<string
name="action_settings">Settings</string>

menu/display_contact.xml <string name="hello_world">Hello


world!</string>
<string name="Add_New">Add New</string>
<?xml version="1.0" encoding="utf-8"?>
<string name="edit">Edit</string>
<menu
xmlns:android="http://schemas.android.com/apk/r <string name="delete">Delete Contact</string>
es/android">
<string
<item name="title_activity_display_contact">DisplayCo
ntact</string>
android:id="@+id/Edit_Contact"
<string name="name">Name</string>
android:orderInCategory="100"
<string name="phone">Phone</string>
android:title="edit"/>
<string name="email">Email</string>
<string name="street">Street</string>
<item
<string
android:id="@+id/Delete_Contact" name="country">City/State/Zip</string>
android:orderInCategory="100" <string name="save">Save</string>
android:title="delete"/>
<string name="deleteContact">Are you sure, you want to delete it?</string>
<string name="yes">Yes</string>
<string name="no">No</string>
</resources>

You might also like