SQLite is a opensource SQL database that stores data to a text file on a device. Android comes in with built in SQLite database implementation.
In this tutorial i going to show you how to do CRUD (Create, Read, Update, Delete) on Android using Database SQLite.
VIDEO DEMO APLICATION
There are 3 buttons action (Insert, Update and Delete) and 1 Listview to display the saved data. You can see as realtime.
PREPARATION
Create some xml files that we in this project :
- activity_main.xml
- popup_delete.xml
- popup_update.xml
Create some java classes :
- DatabaseHandler.java
- ProfileModel.java
- MainActivity.java
In this project i store all the string in strings.xml. Here it is :
<resources> <string name="app_name">SQLLiteTutorial</string> <string name="input_your_name_bastard">Input your name, bastard</string> <string name="your_phone_number">your phone number</string> <string name="msg_cannot_allow_empty_field">Cannot allow empty field</string> <string name="lbl_update_data_pop_up">Update Data!</string> <string name="lbl_choose_an_id">Choose an ID</string> <string name="hint_update_phone_number">Update phone number</string> <string name="hint_update_name">Update name</string> <string name="lbl_save">Save</string> <string name="lbl_ok">OK</string> </resources>
Open activity_main.xml, paste the following code :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:id="@+id/activity_main" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
android:orientation="vertical" | |
tools:context="com.putuguna.sqllitetutorial.MainActivity"> | |
<TextView | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="Putuguna[dot]com\'s database" | |
android:textColor="@android:color/black" | |
android:textStyle="bold" | |
android:layout_marginBottom="10dp" | |
android:gravity="center"/> | |
<EditText | |
android:id="@+id/edit_text_name" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:hint="@string/input_your_name_bastard"/> | |
<EditText | |
android:id="@+id/edit_text_phone" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:inputType="number" | |
android:hint="@string/your_phone_number"/> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="horizontal"> | |
<Button | |
android:layout_weight="1" | |
android:id="@+id/button_insert" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="Insert" | |
android:background="@android:color/holo_blue_dark" | |
android:textColor="@android:color/white"/> | |
<Button | |
android:layout_weight="1" | |
android:id="@+id/button_delete" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="DELETE" | |
android:background="@color/colorPrimary" | |
android:textColor="@android:color/white"/> | |
<Button | |
android:layout_weight="1" | |
android:id="@+id/button_update" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="UPDATE" | |
android:background="@android:color/holo_green_light" | |
android:textColor="@android:color/white"/> | |
</LinearLayout> | |
<ListView | |
android:id="@+id/listview_contact" | |
android:layout_marginTop="10dp" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"></ListView> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="vertical" | |
android:layout_width="match_parent" | |
android:padding="@dimen/activity_vertical_margin" | |
android:layout_height="match_parent"> | |
<Spinner | |
android:id="@+id/spinner_id" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content"></Spinner> | |
<Button | |
android:id="@+id/btn_ok" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="right" | |
android:text="@string/lbl_ok"/> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="vertical" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:padding="@dimen/activity_vertical_margin"> | |
<Spinner | |
android:id="@+id/spinner_update" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content"></Spinner> | |
<EditText | |
android:id="@+id/et_update_name" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:hint="@string/hint_update_name"/> | |
<EditText | |
android:id="@+id/et_update_phone" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:hint="@string/hint_update_phone_number"/> | |
<Button | |
android:id="@+id/btn_save_update" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="@string/lbl_save"/> | |
</LinearLayout> |
First create the model of your object. In this project i created as ProfileModel.java. Paste the following code :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.putuguna.sqllitetutorial; | |
/** | |
* Created by putuguna on 03/11/16. | |
*/ | |
public class ProfileModel { | |
private int id; | |
private String name; | |
private String phoneNumber; | |
public ProfileModel(int id, String name, String phoneNumber) { | |
this.id = id; | |
this.name = name; | |
this.phoneNumber = phoneNumber; | |
} | |
public ProfileModel() { | |
} | |
public ProfileModel(String name, String phoneNumber) { | |
this.name = name; | |
this.phoneNumber = phoneNumber; | |
} | |
public int getId() { | |
return id; | |
} | |
public void setId(int id) { | |
this.id = id; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getPhoneNumber() { | |
return phoneNumber; | |
} | |
public void setPhoneNumber(String phoneNumber) { | |
this.phoneNumber = phoneNumber; | |
} | |
//this is used to display data in the spinner | |
@Override | |
public String toString() { | |
return "ID = " + id + ", Name = " + name; | |
} | |
} |
Open DatabaseHandler.java. This class will extends SQLiteOpenHelper, implement its methods. The methods that we implement after extends SQLiteOpenHelper are :
- (override) onCreate(SQLiteDatabase sqLiteDatabase). This method used to create the table of database
- (override) onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1). This method used to increase database version after any changes in its attribute of name automatically. Beside that this method also check if there any duplicated name of table or not.
Beside create database name, also in this class we create all the main function of CRUD :
Create the data or Insert :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void addProfile(ProfileModel profile){ | |
SQLiteDatabase db = this.getWritableDatabase(); | |
ContentValues values = new ContentValues(); | |
values.put(PROFILE_NAME, profile.getName()); | |
values.put(PROFILE_PHONE_NUMBER, profile.getPhoneNumber()); | |
db.insert(TABLE_PROFILE, null, values); | |
db.close(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public List<ProfileModel> getAllDataProfile(){ | |
List<ProfileModel> profileList = new ArrayList<>(); | |
String selectQuery = "SELECT * FROM " + TABLE_PROFILE; | |
SQLiteDatabase database = this.getWritableDatabase(); | |
Cursor cursor = database.rawQuery(selectQuery,null); | |
if(cursor.moveToFirst()){ | |
do { | |
ProfileModel profileModel = new ProfileModel(); | |
profileModel.setId(Integer.parseInt(cursor.getString(0))); | |
profileModel.setName(cursor.getString(1)); | |
profileModel.setPhoneNumber(cursor.getString(2)); | |
profileList.add(profileModel); | |
}while (cursor.moveToNext()); | |
} | |
return profileList; | |
} |
Update the data that we have inserted using parameter ID (ID of the profile) :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void updatedetails(int id, String name, String noPhone) { | |
SQLiteDatabase db = this.getWritableDatabase(); | |
ContentValues args = new ContentValues(); | |
args.put(PROFILE_ID, id); | |
args.put(PROFILE_NAME, name); | |
args.put(PROFILE_PHONE_NUMBER, noPhone); | |
db.update(TABLE_PROFILE, args, PROFILE_ID + "=" + id, null); | |
db.close(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void deleteRow(String value) { | |
SQLiteDatabase db = this.getWritableDatabase(); | |
db.execSQL("DELETE FROM " + TABLE_PROFILE + " WHERE "+ PROFILE_ID +"='"+value+"'"); | |
db.close(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.putuguna.sqllitetutorial; | |
import android.content.ContentValues; | |
import android.content.Context; | |
import android.database.Cursor; | |
import android.database.sqlite.SQLiteDatabase; | |
import android.database.sqlite.SQLiteOpenHelper; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Created by putuguna on 03/11/16. | |
*/ | |
public class DatabaseHandler extends SQLiteOpenHelper { | |
private static final int DATABASE_VERSION = 1; | |
private static final String DATABASE_NAME = "database_profile"; | |
private static final String TABLE_PROFILE = "myprofile"; | |
private static final String PROFILE_ID = "id"; | |
private static final String PROFILE_NAME = "name"; | |
private static final String PROFILE_PHONE_NUMBER = "phone_number"; | |
public DatabaseHandler(Context context) { | |
super(context, DATABASE_NAME, null,DATABASE_VERSION); | |
} | |
@Override | |
public void onCreate(SQLiteDatabase sqLiteDatabase) { | |
String CREATE_CONTACT_TABLE = "CREATE TABLE " + TABLE_PROFILE + "(" + | |
PROFILE_ID + " INTEGER PRIMARY KEY, " + PROFILE_NAME + " TEXT, " + | |
PROFILE_PHONE_NUMBER + " TEXT " + ")"; | |
sqLiteDatabase.execSQL(CREATE_CONTACT_TABLE); | |
} | |
@Override | |
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { | |
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_PROFILE); | |
onCreate(sqLiteDatabase); | |
} | |
/** | |
* this method used to add contact item | |
* @param profile | |
*/ | |
public void addProfile(ProfileModel profile){ | |
SQLiteDatabase db = this.getWritableDatabase(); | |
ContentValues values = new ContentValues(); | |
values.put(PROFILE_NAME, profile.getName()); | |
values.put(PROFILE_PHONE_NUMBER, profile.getPhoneNumber()); | |
db.insert(TABLE_PROFILE, null, values); | |
db.close(); | |
} | |
/** | |
* this method used to get all data contact and store to list | |
* @return | |
*/ | |
public List<ProfileModel> getAllDataProfile(){ | |
List<ProfileModel> profileList = new ArrayList<>(); | |
String selectQuery = "SELECT * FROM " + TABLE_PROFILE; | |
SQLiteDatabase database = this.getWritableDatabase(); | |
Cursor cursor = database.rawQuery(selectQuery,null); | |
if(cursor.moveToFirst()){ | |
do { | |
ProfileModel profileModel = new ProfileModel(); | |
profileModel.setId(Integer.parseInt(cursor.getString(0))); | |
profileModel.setName(cursor.getString(1)); | |
profileModel.setPhoneNumber(cursor.getString(2)); | |
profileList.add(profileModel); | |
}while (cursor.moveToNext()); | |
} | |
return profileList; | |
} | |
/** | |
* this method used to delete item of contact | |
* @param value | |
*/ | |
public void deleteRow(String value) { | |
SQLiteDatabase db = this.getWritableDatabase(); | |
db.execSQL("DELETE FROM " + TABLE_PROFILE + " WHERE "+ PROFILE_ID +"='"+value+"'"); | |
db.close(); | |
} | |
/** | |
* this method used to update the data | |
* @param id | |
* @param name | |
* @param noPhone | |
*/ | |
public void updatedetails(int id, String name, String noPhone) { | |
SQLiteDatabase db = this.getWritableDatabase(); | |
ContentValues args = new ContentValues(); | |
args.put(PROFILE_ID, id); | |
args.put(PROFILE_NAME, name); | |
args.put(PROFILE_PHONE_NUMBER, noPhone); | |
db.update(TABLE_PROFILE, args, PROFILE_ID + "=" + id, null); | |
db.close(); | |
} | |
} |
When button insert is pressed, program will call method insertData(handler) :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void insertData(final DatabaseHandler databaseHandler){ | |
String name = etName.getText().toString(); | |
String phone = etPhone.getText().toString(); | |
ProfileModel profileModel = new ProfileModel(); | |
profileModel.setName(name); | |
profileModel.setPhoneNumber(phone); | |
databaseHandler.addProfile(profileModel); | |
displayAllData(databaseHandler, profileModel); | |
etPhone.setText(""); | |
etName.setText(""); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void displayAllData(DatabaseHandler handler, ProfileModel profile){ | |
mListProfile.clear(); | |
List<ProfileModel> list = handler.getAllDataProfile(); | |
for(ProfileModel profileModel : list){ | |
String log = "ID : " + profileModel.getId() + "\n" + | |
"NAME : " + profileModel.getName() + "\n" + | |
"PHONE NUMBER : " + profileModel.getPhoneNumber(); | |
System.out.println(log); | |
//add to list | |
mListProfile.add(log); | |
//set to the model | |
profile.setId(profileModel.getId()); | |
profile.setName(profileModel.getName()); | |
profile.setPhoneNumber(profileModel.getPhoneNumber()); | |
} | |
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, mListProfile); | |
lvProfile.setAdapter(arrayAdapter); | |
lvProfile.invalidateViews(); | |
arrayAdapter.notifyDataSetChanged(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void updateDataFromPopup(){ | |
final Dialog dialog = new Dialog(this); | |
dialog.setTitle(R.string.lbl_update_data_pop_up); | |
dialog.setContentView(R.layout.popup_update); | |
Spinner spinnerUpdate = (Spinner) dialog.findViewById(R.id.spinner_update); | |
final EditText etUpdateName = (EditText) dialog.findViewById(R.id.et_update_name); | |
final EditText etUpdatePhone = (EditText) dialog.findViewById(R.id.et_update_phone); | |
Button btnSaveUpdate = (Button) dialog.findViewById(R.id.btn_save_update); | |
final List<ProfileModel> profileUpdate = handler.getAllDataProfile(); | |
final SharedPreferences sharedPreferences = getSharedPreferences("mPrefs", Context.MODE_PRIVATE); | |
final SharedPreferences.Editor editor = sharedPreferences.edit(); | |
//set into spinner adapter | |
ArrayAdapter<ProfileModel> spinnerArrayAdapter = new ArrayAdapter<ProfileModel>(this,android.R.layout.simple_spinner_item, profileUpdate); | |
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); | |
spinnerUpdate.setAdapter(spinnerArrayAdapter); | |
//spinner on item selected | |
spinnerUpdate.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { | |
@Override | |
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { | |
etUpdateName.setText(profileUpdate.get(i).getName()); | |
etUpdatePhone.setText(profileUpdate.get(i).getPhoneNumber()); | |
editor.putString("id-for-update", profileUpdate.get(i).getId()+""); | |
editor.commit(); | |
} | |
@Override | |
public void onNothingSelected(AdapterView<?> adapterView) { | |
} | |
}); | |
btnSaveUpdate.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
if(TextUtils.isEmpty(etUpdateName.getText().toString())){ | |
Toast.makeText(MainActivity.this, R.string.msg_cannot_allow_empty_field, Toast.LENGTH_SHORT).show(); | |
}else if(TextUtils.isEmpty(etUpdatePhone.getText().toString())){ | |
Toast.makeText(MainActivity.this, R.string.msg_cannot_allow_empty_field, Toast.LENGTH_SHORT).show(); | |
}else{ | |
String newName = etUpdateName.getText().toString(); | |
String newPhone = etUpdatePhone.getText().toString(); | |
String idProfile = sharedPreferences.getString("id-for-update",null); | |
//update data | |
handler.updatedetails(Integer.parseInt(idProfile), newName, newPhone); | |
displayAllData(handler,profile); | |
dialog.dismiss(); | |
} | |
} | |
}); | |
dialog.show(); | |
} |
Same as button update, when button delete pressed, there is a popup will appears. You just choose an ID that you want to delete. Here is the method popupDelete() :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void popupDelete(){ | |
final Dialog dialog = new Dialog(this); | |
dialog.setTitle(R.string.lbl_choose_an_id); | |
dialog.setContentView(R.layout.popup_delete); | |
final SharedPreferences sharedPreferences = getSharedPreferences("mPrefs", Context.MODE_PRIVATE); | |
final SharedPreferences.Editor editor = sharedPreferences.edit(); | |
final Spinner spinner= (Spinner) dialog.findViewById(R.id.spinner_id); | |
Button btnOk = (Button) dialog.findViewById(R.id.btn_ok); | |
final List<ProfileModel> profileModelList = handler.getAllDataProfile(); | |
//set into spinner adapter | |
ArrayAdapter<ProfileModel> spinnerArrayAdapter = new ArrayAdapter<ProfileModel>(this,android.R.layout.simple_spinner_item, profileModelList); | |
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); | |
spinner.setAdapter(spinnerArrayAdapter); | |
//spinner on item selected | |
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { | |
@Override | |
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { | |
editor.putString("IDprofile", profileModelList.get(i).getId()+""); | |
editor.commit(); | |
} | |
@Override | |
public void onNothingSelected(AdapterView<?> adapterView) { | |
} | |
}); | |
btnOk.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
String profileId = sharedPreferences.getString("IDprofile", null); | |
handler.deleteRow(profileId); | |
displayAllData(handler,profile); | |
dialog.dismiss(); | |
} | |
}); | |
dialog.show(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.putuguna.sqllitetutorial; | |
import android.app.Dialog; | |
import android.content.Context; | |
import android.content.SharedPreferences; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.text.TextUtils; | |
import android.view.View; | |
import android.widget.AdapterView; | |
import android.widget.ArrayAdapter; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.ListView; | |
import android.widget.Spinner; | |
import android.widget.Toast; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class MainActivity extends AppCompatActivity { | |
private EditText etName; | |
private EditText etPhone; | |
private Button btnInsert; | |
private Button btnUpdate; | |
private ListView lvProfile; | |
private List<String> mListProfile = new ArrayList<>(); | |
private Button btnDelete; | |
DatabaseHandler handler = new DatabaseHandler(this); | |
ProfileModel profile = new ProfileModel(); | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
etName = (EditText) findViewById(R.id.edit_text_name); | |
etPhone = (EditText) findViewById(R.id.edit_text_phone); | |
btnInsert = (Button) findViewById(R.id.button_insert); | |
lvProfile = (ListView) findViewById(R.id.listview_contact); | |
btnDelete = (Button) findViewById(R.id.button_delete); | |
btnUpdate = (Button) findViewById(R.id.button_update); | |
//insert | |
btnInsert.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
if(TextUtils.isEmpty(etName.getText().toString())){ | |
Toast.makeText(MainActivity.this, R.string.msg_cannot_allow_empty_field, Toast.LENGTH_SHORT).show(); | |
}else if(TextUtils.isEmpty(etPhone.getText().toString())){ | |
Toast.makeText(MainActivity.this, R.string.msg_cannot_allow_empty_field, Toast.LENGTH_SHORT).show(); | |
}else{ | |
insertData(handler); | |
} | |
} | |
}); | |
//reading all profileModel | |
displayAllData(handler, profile); | |
//delete | |
btnDelete.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
popupDelete(); | |
} | |
}); | |
//update | |
btnUpdate.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
updateDataFromPopup(); | |
} | |
}); | |
} | |
/** | |
* this method used to read all the data of contact | |
* @param handler | |
* @param profile | |
*/ | |
private void displayAllData(DatabaseHandler handler, ProfileModel profile){ | |
mListProfile.clear(); | |
List<ProfileModel> list = handler.getAllDataProfile(); | |
for(ProfileModel profileModel : list){ | |
String log = "ID : " + profileModel.getId() + "\n" + | |
"NAME : " + profileModel.getName() + "\n" + | |
"PHONE NUMBER : " + profileModel.getPhoneNumber(); | |
System.out.println(log); | |
//add to list | |
mListProfile.add(log); | |
//set to the model | |
profile.setId(profileModel.getId()); | |
profile.setName(profileModel.getName()); | |
profile.setPhoneNumber(profileModel.getPhoneNumber()); | |
} | |
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, mListProfile); | |
lvProfile.setAdapter(arrayAdapter); | |
lvProfile.invalidateViews(); | |
arrayAdapter.notifyDataSetChanged(); | |
} | |
/** | |
* this method used to insert data | |
* @param databaseHandler | |
*/ | |
private void insertData(final DatabaseHandler databaseHandler){ | |
String name = etName.getText().toString(); | |
String phone = etPhone.getText().toString(); | |
ProfileModel profileModel = new ProfileModel(); | |
profileModel.setName(name); | |
profileModel.setPhoneNumber(phone); | |
databaseHandler.addProfile(profileModel); | |
displayAllData(databaseHandler, profileModel); | |
etPhone.setText(""); | |
etName.setText(""); | |
} | |
/** | |
* this method used to delete item from popup | |
*/ | |
private void popupDelete(){ | |
final Dialog dialog = new Dialog(this); | |
dialog.setTitle(R.string.lbl_choose_an_id); | |
dialog.setContentView(R.layout.popup_delete); | |
final SharedPreferences sharedPreferences = getSharedPreferences("mPrefs", Context.MODE_PRIVATE); | |
final SharedPreferences.Editor editor = sharedPreferences.edit(); | |
final Spinner spinner= (Spinner) dialog.findViewById(R.id.spinner_id); | |
Button btnOk = (Button) dialog.findViewById(R.id.btn_ok); | |
final List<ProfileModel> profileModelList = handler.getAllDataProfile(); | |
//set into spinner adapter | |
ArrayAdapter<ProfileModel> spinnerArrayAdapter = new ArrayAdapter<ProfileModel>(this,android.R.layout.simple_spinner_item, profileModelList); | |
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); | |
spinner.setAdapter(spinnerArrayAdapter); | |
//spinner on item selected | |
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { | |
@Override | |
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { | |
editor.putString("IDprofile", profileModelList.get(i).getId()+""); | |
editor.commit(); | |
} | |
@Override | |
public void onNothingSelected(AdapterView<?> adapterView) { | |
} | |
}); | |
btnOk.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
String profileId = sharedPreferences.getString("IDprofile", null); | |
handler.deleteRow(profileId); | |
displayAllData(handler,profile); | |
dialog.dismiss(); | |
} | |
}); | |
dialog.show(); | |
} | |
/** | |
* this method used to update item from popup | |
*/ | |
private void updateDataFromPopup(){ | |
final Dialog dialog = new Dialog(this); | |
dialog.setTitle(R.string.lbl_update_data_pop_up); | |
dialog.setContentView(R.layout.popup_update); | |
Spinner spinnerUpdate = (Spinner) dialog.findViewById(R.id.spinner_update); | |
final EditText etUpdateName = (EditText) dialog.findViewById(R.id.et_update_name); | |
final EditText etUpdatePhone = (EditText) dialog.findViewById(R.id.et_update_phone); | |
Button btnSaveUpdate = (Button) dialog.findViewById(R.id.btn_save_update); | |
final List<ProfileModel> profileUpdate = handler.getAllDataProfile(); | |
final SharedPreferences sharedPreferences = getSharedPreferences("mPrefs", Context.MODE_PRIVATE); | |
final SharedPreferences.Editor editor = sharedPreferences.edit(); | |
//set into spinner adapter | |
ArrayAdapter<ProfileModel> spinnerArrayAdapter = new ArrayAdapter<ProfileModel>(this,android.R.layout.simple_spinner_item, profileUpdate); | |
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); | |
spinnerUpdate.setAdapter(spinnerArrayAdapter); | |
//spinner on item selected | |
spinnerUpdate.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { | |
@Override | |
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { | |
etUpdateName.setText(profileUpdate.get(i).getName()); | |
etUpdatePhone.setText(profileUpdate.get(i).getPhoneNumber()); | |
editor.putString("id-for-update", profileUpdate.get(i).getId()+""); | |
editor.commit(); | |
} | |
@Override | |
public void onNothingSelected(AdapterView<?> adapterView) { | |
} | |
}); | |
btnSaveUpdate.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
if(TextUtils.isEmpty(etUpdateName.getText().toString())){ | |
Toast.makeText(MainActivity.this, R.string.msg_cannot_allow_empty_field, Toast.LENGTH_SHORT).show(); | |
}else if(TextUtils.isEmpty(etUpdatePhone.getText().toString())){ | |
Toast.makeText(MainActivity.this, R.string.msg_cannot_allow_empty_field, Toast.LENGTH_SHORT).show(); | |
}else{ | |
String newName = etUpdateName.getText().toString(); | |
String newPhone = etUpdatePhone.getText().toString(); | |
String idProfile = sharedPreferences.getString("id-for-update",null); | |
//update data | |
handler.updatedetails(Integer.parseInt(idProfile), newName, newPhone); | |
displayAllData(handler,profile); | |
dialog.dismiss(); | |
} | |
} | |
}); | |
dialog.show(); | |
} | |
} |
EmoticonEmoticon