In this example i'm going to show you how to perform crud operation in sqlite database and how we can pass SQLite Database Data to next activity.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:padding="10dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter name" />
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Email" />
<EditText
android:id="@+id/contact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Contact" />
<Button
android:id="@+id/insert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Insert" />
<Button
android:id="@+id/view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="View" />
<Button
android:id="@+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Search" />
<Button
android:id="@+id/update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Update" />
</LinearLayout>
list_items_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/name_here"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:padding="10dp"
android:text="Name here"
android:textSize="25sp" />
<TextView
android:id="@+id/email_here"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:padding="10dp"
android:text="Email here"
android:textSize="20sp" />
<TextView
android:id="@+id/contact_here"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:padding="10dp"
android:text="Contact here"
android:textSize="20sp" />
</LinearLayout>
DatabaseHelper.java
package com.example.malik.sqlitepractise2;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
/**
* Created by malik on 14/12/2017.
*/
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String NAME = "name";
public static final String EMAIl = "email";
public static final String CONTACT = "contact";
public static final String TABLE_NAME = "personTable";
public static final String DATABASE_NAME = "personDatabase";
public static final int DATABASE_VERSION = 1;
public static final String QUERY_FOR_CREATE_TABLE =
"create table "+TABLE_NAME+"("+NAME+" text,"+EMAIl+" text,"+CONTACT+" text);";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
Log.d("SQLite", "Database created");
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(QUERY_FOR_CREATE_TABLE);
Log.d("SQLite", "Table created");
}
// for insert data
public void insertDataMethod(String name, String email, String contact, SQLiteDatabase sqLiteDatabase){
ContentValues contentValues = new ContentValues();
contentValues.put(NAME,name);
contentValues.put(EMAIl,email);
contentValues.put(CONTACT,contact);
sqLiteDatabase.insert(TABLE_NAME,null,contentValues);
Log.d("SQLite", "Data inserted");
}
// for view data
public Cursor getAllData(SQLiteDatabase sqLiteDatabase){
Cursor cursor;
String columns[] = {NAME,EMAIl,CONTACT};
cursor = sqLiteDatabase.query(TABLE_NAME,columns,null,null,null,null,null);
return cursor;
}
// search contact
public Cursor searchContacts(String name, SQLiteDatabase sqLiteDatabase){
Cursor cursor;
String column[] = {EMAIl,CONTACT};
String searchQry = NAME + " LIKE ?";
String string[] = {name};
cursor = sqLiteDatabase.query(TABLE_NAME,column,searchQry,string,null,null,null);
return cursor;
}
// delete data
public void deleteDatafromSQLiteDatabase(String name, SQLiteDatabase sqLiteDatabase){
String deleteQry = NAME + " LIKE ?";
String string[] = {name};
sqLiteDatabase.delete(TABLE_NAME,deleteQry,string);
}
// update contact
public void updateContactDetail(String p_name, String n_name, String n_email, String n_contact, SQLiteDatabase sqLiteDatabase){
ContentValues contentValues = new ContentValues();
contentValues.put(NAME,n_name);
contentValues.put(EMAIl,n_email);
contentValues.put(CONTACT,n_contact);
String updateQry = NAME + " LIKE ?";
String string[] = {p_name};
sqLiteDatabase.update(TABLE_NAME,contentValues,updateQry,string);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:padding="10dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter name" />
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Email" />
<EditText
android:id="@+id/contact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Contact" />
<Button
android:id="@+id/insert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Insert" />
<Button
android:id="@+id/view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="View" />
<Button
android:id="@+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Search" />
<Button
android:id="@+id/update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Update" />
</LinearLayout>
MainActivity.java
package com.example.malik.sqlitepractise2;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
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 {
private EditText name, email, contact;
private Button insert, view, search, update;
DatabaseHelper databaseHelper;
SQLiteDatabase sqLiteDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (EditText) findViewById(R.id.name);
email = (EditText) findViewById(R.id.email);
contact = (EditText) findViewById(R.id.contact);
insert = (Button) findViewById(R.id.insert);
view = (Button) findViewById(R.id.view);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ViewDataActivity.class);
startActivity(intent);
}
});
search = (Button) findViewById(R.id.search);
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SearchContactActivity.class);
startActivity(intent);
}
});
update = (Button) findViewById(R.id.update);
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, UpdateContactActivity.class);
startActivity(intent);
}
});
insert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String NAME = name.getText().toString();
String EMAIL = email.getText().toString();
String CONTACT = contact.getText().toString();
databaseHelper = new DatabaseHelper(MainActivity.this);
sqLiteDatabase = databaseHelper.getWritableDatabase();
databaseHelper.insertDataMethod(NAME,EMAIL,CONTACT,sqLiteDatabase);
Toast.makeText(getApplicationContext(),"Data Inserted",Toast.LENGTH_LONG).show();
databaseHelper.close();
name.setText("");
email.setText("");
contact.setText("");
}
});
}
}
Person.java
package com.example.malik.sqlitepractise2;
/**
* Created by malik on 14/12/2017.
*/
public class Person {
String name;
String email;
String contact;
public Person() {
}
public Person(String name, String email, String contact) {
this.name = name;
this.email = email;
this.contact = contact;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
}
MyAdapter.java
package com.example.malik.sqlitepractise2;
import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by malik on 14/12/2017.
*/
public class MyAdapter extends ArrayAdapter {
List list = new ArrayList();
public MyAdapter(@NonNull Context context, @LayoutRes int resource) {
super(context, resource);
}
@Override
public void add(@Nullable Object object) {
super.add(object);
list.add(object);
}
@Override
public int getCount() {
return list.size();
}
@Nullable
@Override
public Object getItem(int position) {
return list.get(position);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_items_layout,null);
TextView _name = (TextView) convertView.findViewById(R.id.name_here);
TextView _email = (TextView) convertView.findViewById(R.id.email_here);
TextView _contact = (TextView) convertView.findViewById(R.id.contact_here);
Person person = (Person) list.get(position);
_name.setText(person.getName());
_email.setText(person.getEmail());
_contact.setText(person.getContact());
return convertView;
}
}
list_items_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/name_here"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:padding="10dp"
android:text="Name here"
android:textSize="25sp" />
<TextView
android:id="@+id/email_here"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:padding="10dp"
android:text="Email here"
android:textSize="20sp" />
<TextView
android:id="@+id/contact_here"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:padding="10dp"
android:text="Contact here"
android:textSize="20sp" />
</LinearLayout>
DatabaseHelper.java
package com.example.malik.sqlitepractise2;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
/**
* Created by malik on 14/12/2017.
*/
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String NAME = "name";
public static final String EMAIl = "email";
public static final String CONTACT = "contact";
public static final String TABLE_NAME = "personTable";
public static final String DATABASE_NAME = "personDatabase";
public static final int DATABASE_VERSION = 1;
public static final String QUERY_FOR_CREATE_TABLE =
"create table "+TABLE_NAME+"("+NAME+" text,"+EMAIl+" text,"+CONTACT+" text);";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
Log.d("SQLite", "Database created");
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(QUERY_FOR_CREATE_TABLE);
Log.d("SQLite", "Table created");
}
// for insert data
public void insertDataMethod(String name, String email, String contact, SQLiteDatabase sqLiteDatabase){
ContentValues contentValues = new ContentValues();
contentValues.put(NAME,name);
contentValues.put(EMAIl,email);
contentValues.put(CONTACT,contact);
sqLiteDatabase.insert(TABLE_NAME,null,contentValues);
Log.d("SQLite", "Data inserted");
}
// for view data
public Cursor getAllData(SQLiteDatabase sqLiteDatabase){
Cursor cursor;
String columns[] = {NAME,EMAIl,CONTACT};
cursor = sqLiteDatabase.query(TABLE_NAME,columns,null,null,null,null,null);
return cursor;
}
// search contact
public Cursor searchContacts(String name, SQLiteDatabase sqLiteDatabase){
Cursor cursor;
String column[] = {EMAIl,CONTACT};
String searchQry = NAME + " LIKE ?";
String string[] = {name};
cursor = sqLiteDatabase.query(TABLE_NAME,column,searchQry,string,null,null,null);
return cursor;
}
// delete data
public void deleteDatafromSQLiteDatabase(String name, SQLiteDatabase sqLiteDatabase){
String deleteQry = NAME + " LIKE ?";
String string[] = {name};
sqLiteDatabase.delete(TABLE_NAME,deleteQry,string);
}
// update contact
public void updateContactDetail(String p_name, String n_name, String n_email, String n_contact, SQLiteDatabase sqLiteDatabase){
ContentValues contentValues = new ContentValues();
contentValues.put(NAME,n_name);
contentValues.put(EMAIl,n_email);
contentValues.put(CONTACT,n_contact);
String updateQry = NAME + " LIKE ?";
String string[] = {p_name};
sqLiteDatabase.update(TABLE_NAME,contentValues,updateQry,string);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
activity_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".ViewDataActivity">
<ListView
android:id="@+id/list1"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</LinearLayout>
ViewDataActivity.java
package com.example.malik.sqlitepractise2;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class ViewDataActivity extends AppCompatActivity {
ListView listView;
MyAdapter myAdapter;
DatabaseHelper databaseHelper;
SQLiteDatabase sqLiteDatabase;
Cursor cursor;
List<Person> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
listView = (ListView) findViewById(R.id.list1);
myAdapter = new MyAdapter(ViewDataActivity.this, R.layout.list_items_layout);
listView.setAdapter(myAdapter);
list = new ArrayList<>();
databaseHelper = new DatabaseHelper(ViewDataActivity.this);
sqLiteDatabase = databaseHelper.getReadableDatabase();
cursor = databaseHelper.getAllData(sqLiteDatabase);
if(cursor.moveToFirst()){
do{
String name, email, contact;
name = cursor.getString(0);
email = cursor.getString(1);
contact = cursor.getString(2);
Person person = new Person(name,email,contact);
myAdapter.add(person);
list.add(person);
} while (cursor.moveToNext());
}
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Person person = list.get(position);
Intent intent = new Intent(ViewDataActivity.this, ReceiveDataActivity.class);
intent.putExtra("NAME", person.getName());
intent.putExtra("EMAIL", person.getEmail());
intent.putExtra("CONTACT", person.getContact());
startActivity(intent);
}
});
}
}
activity_search_contact.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:padding="10dp"
tools:context="com.example.malik.sqlitepractise2.SearchContactActivity">
<EditText
android:id="@+id/searchName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter name" />
<Button
android:id="@+id/searchButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="30dp"
android:layout_marginTop="30dp"
android:text="Search" />
<EditText
android:id="@+id/searchEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email Here" />
<EditText
android:id="@+id/searchContact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Contact here" />
<Button
android:id="@+id/deleteButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="30dp"
android:layout_marginTop="30dp"
android:text="Delete data" />
</LinearLayout>
SearchContactActivity.java
package com.example.malik.sqlitepractise2;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class SearchContactActivity extends AppCompatActivity {
EditText sName, sEmail,sContact;
Button sButton, sDelete;
DatabaseHelper databaseHelper;
SQLiteDatabase sqLiteDatabase;
Cursor cursor;
String NAME;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_contact);
sName = (EditText) findViewById(R.id.searchName);
sEmail = (EditText) findViewById(R.id.searchEmail);
sContact = (EditText) findViewById(R.id.searchContact);
sDelete = (Button) findViewById(R.id.deleteButton1);
sButton = (Button) findViewById(R.id.searchButton);
sButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NAME = sName.getText().toString();
databaseHelper = new DatabaseHelper(SearchContactActivity.this);
sqLiteDatabase = databaseHelper.getReadableDatabase();
cursor = databaseHelper.searchContacts(NAME,sqLiteDatabase);
if(cursor.moveToFirst()){
do{
String email, contact;
email = cursor.getString(0);
contact = cursor.getString(1);
sEmail.setText(email);
sContact.setText(contact);
}while (cursor.moveToNext());
}
}
});
sDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
databaseHelper = new DatabaseHelper(SearchContactActivity.this);
sqLiteDatabase = databaseHelper.getReadableDatabase();
databaseHelper.deleteDatafromSQLiteDatabase(NAME,sqLiteDatabase);
Toast.makeText(getApplicationContext(), "Data deleted",Toast.LENGTH_LONG).show();
finish();
Intent intent = new Intent(SearchContactActivity.this, ViewDataActivity.class);
startActivity(intent);
}
});
}
}
activity_update_contact.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:padding="10dp"
tools:context="com.example.malik.sqlitepractise2.UpdateContactActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:orientation="horizontal">
<EditText
android:id="@+id/UPName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Enter name for search" />
<Button
android:id="@+id/UPButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search" />
</LinearLayout>
<EditText
android:id="@+id/uName1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name here" />
<EditText
android:id="@+id/uEmail1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email here" />
<EditText
android:id="@+id/uContact1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Contact here" />
<Button
android:id="@+id/uButtonUpdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:text="Update Contact Details" />
</LinearLayout>
UpdateContactActivity.java
package com.example.malik.sqlitepractise2;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class UpdateContactActivity extends AppCompatActivity {
EditText p_editText;
Button p_button;
EditText uName, uEmail, uContact;
Button updateButton;
DatabaseHelper databaseHelper;
SQLiteDatabase sqLiteDatabase;
Cursor cursor;
String NAME;
String getName, getEmail, getContact;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update_contact);
p_editText = (EditText) findViewById(R.id.UPName);
p_button = (Button) findViewById(R.id.UPButton);
uName = (EditText) findViewById(R.id.uName1);
uEmail = (EditText) findViewById(R.id.uEmail1);
uContact = (EditText) findViewById(R.id.uContact1);
updateButton = (Button) findViewById(R.id.uButtonUpdate);
p_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NAME = p_editText.getText().toString();
databaseHelper = new DatabaseHelper(UpdateContactActivity.this);
sqLiteDatabase = databaseHelper.getReadableDatabase();
cursor = databaseHelper.searchContacts(NAME,sqLiteDatabase);
if(cursor.moveToFirst()){
do{
String email, contact;
email = cursor.getString(0);
contact = cursor.getString(1);
getName = NAME;
getEmail = email;
getContact = contact;
uName.setText(getName);
uEmail.setText(getEmail);
uContact.setText(getContact);
} while (cursor.moveToNext());
}
}
});
updateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name, email, contact;
name = uName.getText().toString();
email = uEmail.getText().toString();
contact = uContact.getText().toString();
databaseHelper = new DatabaseHelper(UpdateContactActivity.this);
sqLiteDatabase = databaseHelper.getWritableDatabase();
databaseHelper.updateContactDetail(NAME,name,email,contact,sqLiteDatabase);
Toast.makeText(getApplicationContext(),"Contact updated",Toast.LENGTH_LONG).show();
finish();
Intent intent = new Intent(UpdateContactActivity.this, ViewDataActivity.class);
startActivity(intent);
}
});
}
}
activity_receive_data.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.example.malik.sqlitepractise2.ReceiveDataActivity">
<TextView
android:id="@+id/rec_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Name here"
android:textSize="25sp"
android:layout_gravity="center"
/>
<TextView
android:id="@+id/rec_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Email here"
android:textSize="25sp"
android:layout_gravity="center"
/>
<TextView
android:id="@+id/rec_contact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Contact here"
android:textSize="25sp"
android:layout_gravity="center"
/>
</LinearLayout>
ReceiveDataActivity.java
package com.example.malik.sqlitepractise2;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class ReceiveDataActivity extends AppCompatActivity {
TextView name, email, contact;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receive_data);
name = (TextView) findViewById(R.id.rec_name);
email = (TextView) findViewById(R.id.rec_email);
contact = (TextView) findViewById(R.id.rec_contact);
Intent intent = getIntent();
String NAME = intent.getStringExtra("NAME");
name.setText(NAME);
String EMAIL = intent.getStringExtra("EMAIL");
email.setText(EMAIL);
String CONTACT = intent.getStringExtra("CONTACT");
contact.setText(CONTACT);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.malik.sqlitepractise2">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ViewDataActivity" />
<activity android:name=".SearchContactActivity" />
<activity android:name=".UpdateContactActivity" />
<activity android:name=".ReceiveDataActivity"></activity>
</application>
</manifest>