<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.razormist.simplecrudapplication"> <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" android:configChanges="orientation" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".Registration" android:configChanges="orientation" android:screenOrientation="portrait"> <intent-filter> <action android:name="com.razormist.simplecrudapplication.Registration" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".EmployeeDetail" /> <activity android:name=".UpdateEmployee"></activity> </application> </manifest>
<?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" tools:context="com.razormist.simplecrudapplication.MainActivity" android:orientation="vertical"> android:id="@+id/lv_list" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> android:id="@+id/btn_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ADD" android:ems="10" android:layout_centerHorizontal="true" /> </RelativeLayout> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" tools:context="com.razormist.simplecrudapplication.Registration"> <TextView android:id="@+id/tv_firstname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Firstname" android:textSize="25sp" android:layout_marginLeft="20dp" android:layout_marginTop="64dp" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <EditText android:id="@+id/et_firstname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:inputType="text" android:layout_toRightOf="@+id/tv_firstname" android:layout_marginTop="55dp" android:layout_alignParentTop="true" android:layout_alignParentRight="true" /> <TextView android:id="@+id/tv_lastname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Lastname" android:textSize="25sp" android:layout_marginLeft="20dp" android:layout_marginTop="30dp" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_below="@id/tv_firstname"/> <EditText android:id="@+id/et_lastname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" android:layout_toRightOf="@+id/tv_lastname" android:layout_marginTop="15dp" android:layout_alignParentRight="true" android:layout_below="@+id/et_firstname" /> <TextView android:id="@+id/tv_address" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Address" android:textSize="25sp" android:layout_marginLeft="40dp" android:layout_marginTop="30dp" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_below="@id/tv_lastname"/> <EditText android:id="@+id/et_address" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" android:layout_toRightOf="@+id/tv_address" android:layout_marginTop="15dp" android:layout_alignParentRight="true" android:layout_below="@+id/et_lastname" /> android:id="@+id/btn_regsave" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginRight="20dp" android:layout_alignParentRight="true" android:layout_below="@+id/tv_address" android:text="SAVE" android:ems="10" android:layout_marginTop="30dp"/> android:id="@+id/btn_regback" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginLeft="20dp" android:layout_alignParentLeft="true" android:layout_below="@+id/tv_address" android:text="BACK" android:ems="10" android:layout_marginTop="30dp"/> </RelativeLayout>
<?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" tools:context="com.razormist.simplecrudapplication.EmployeeDetail" android:orientation="vertical"> <TextView android:id="@+id/tv_name2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Name" android:textStyle="bold" android:textSize="18sp"/> <TextView android:id="@+id/tv_dname" android:layout_width="match_parent" android:layout_height="wrap_content"/> <TextView android:id="@+id/tv_address2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Address" android:textStyle="bold" android:textSize="18sp"/> <TextView android:id="@+id/tv_daddress" android:layout_width="match_parent" android:layout_height="wrap_content" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> android:id="@+id/btn_uback" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Back" /> android:id="@+id/btn_delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Delete" /> android:id="@+id/btn_edit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Edit" /> </LinearLayout> </LinearLayout>
<?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" tools:context="com.razormist.simplecrudapplication.UpdateEmployee" android:orientation="vertical"> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Firstname" android:textStyle="bold" android:textSize="18sp"/> <EditText android:id="@+id/et_ufirstname" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:ellipsize="end" android:inputType="text" /> <TextView android:id="@+id/textView2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Lastname" android:textStyle="bold" android:textSize="18sp"/> <EditText android:id="@+id/et_ulastname" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:ellipsize="end" android:inputType="text" /> <TextView android:id="@+id/textView3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Address" android:textStyle="bold" android:textSize="18sp"/> <EditText android:id="@+id/et_uaddress" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:ellipsize="end" android:inputType="text" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> android:id="@+id/btn_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Back" /> android:id="@+id/btn_update" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Update" /> </LinearLayout> </LinearLayout>
<?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/tv_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" android:padding="5dp"/> <TextView android:id="@+id/tv_address" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp"/> </LinearLayout>
package com.razormist.simplecrudapplication.adapters; import android.content.Context; 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 com.razormist.simplecrudapplication.Employee; import com.razormist.simplecrudapplication.R; import org.w3c.dom.Text; import java.util.Comparator; import java.util.List; public class EmployeeListAdapter extends ArrayAdapter<Employee> { private List<Employee>employees; super(context, R.layout.employee_list, employees); this.context = context; this.employees = employees; } @NonNull @Override LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); TextView tv_name = (TextView)view.findViewById(R.id.tv_name); TextView tv_address = (TextView)view.findViewById(R.id.tv_address); tv_name.setText("Name: " + employees.get(position).getFirstname() + " " + employees.get(position).getLastname()); tv_address.setText("Address: " + employees.get(position).getAddress()); return view; } }
package com.razormist.simplecrudapplication; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.provider.ContactsContract; import java.util.ArrayList; import java.util.List; public class Database extends SQLiteOpenHelper{ super(context, DATABASE_NAME, null, 1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, firstname TEXT, lastname TEXT, address TEXT)"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXIST "+TABLE_NAME); onCreate(db); } public boolean InsertData(Employee employee){ try { SQLiteDatabase db = getWritableDatabase(); ContentValues contentValues = new ContentValues(); contentValues.put("firstname", employee.getFirstname()); contentValues.put("lastname", employee.getLastname()); contentValues.put("address", employee.getAddress()); long result = db.insert(TABLE_NAME, null, contentValues); db.close(); if (result == -1) { return false; } else { return true; } return false; } } public List<Employee> DisplayAll(){ try{ List<Employee> employees = new ArrayList<Employee>(); SQLiteDatabase sqLiteDatabase = getWritableDatabase(); if(cursor.moveToFirst()){ do{ Employee employee = new Employee(); employee.setId(cursor.getInt(0)); employee.setFirstname(cursor.getString(1)); employee.setLastname(cursor.getString(2)); employee.setAddress(cursor.getString(3)); employees.add(employee); }while (cursor.moveToNext()); } sqLiteDatabase.close(); return employees; return null; } } public boolean Delete(int id){ try{ SQLiteDatabase db = getWritableDatabase(); db.close(); return row > 0; return false; } } public boolean Update(Employee employee){ try { SQLiteDatabase db = getWritableDatabase(); ContentValues contentValues = new ContentValues(); contentValues.put("firstname", employee.getFirstname()); contentValues.put("lastname", employee.getLastname()); contentValues.put("address", employee.getAddress()); db.close(); return row > 0; return false; } } }
package com.razormist.simplecrudapplication; import java.io.Serializable; private int id; public int getId() { return id; } public void setId(int id) { this.id = id; } return firstname; } this.firstname = firstname; } return lastname; } this.lastname = lastname; } return address; } this.address = address; } this.id = id; this.firstname = firstname; this.lastname = lastname; this.address = address; } public Employee() { } }
package com.razormist.simplecrudapplication; import android.content.Intent; import android.database.Cursor; import android.provider.ContactsContract; import android.support.v4.media.RatingCompat; import android.support.v4.widget.SimpleCursorAdapter; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.Toast; import com.razormist.simplecrudapplication.adapters.EmployeeListAdapter; import java.util.Comparator; public class MainActivity extends AppCompatActivity { Database database; EditText et_firstname, et_lastname, et_address; Button btn_add, btn_view; ListView lv_list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); database = new Database(this); EmployeeListAdapter employeeListAdapter = new EmployeeListAdapter(this, database.DisplayAll()); employeeListAdapter.sort(new Comparator<Employee>() { @Override public int compare(Employee lhs, Employee rhs) { return lhs.getLastname().compareTo(rhs.getLastname()); } }); lv_list.setAdapter(employeeListAdapter); lv_list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override Employee employee = database.DisplayAll().get(position); Intent intent = new Intent(MainActivity.this, EmployeeDetail.class); intent.putExtra("employee", employee); startActivity(intent); } }); @Override Intent intent = new Intent(MainActivity.this, Registration.class); startActivity(intent); } }); } }
package com.razormist.simplecrudapplication; import android.content.Intent; 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 Registration extends AppCompatActivity { Database database; EditText et_regfirstname, et_reglastname, et_regaddress; Button btn_regsave, btn_regback; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_registration); database = new Database(this); et_regfirstname = (EditText)findViewById(R.id.et_firstname); et_reglastname = (EditText)findViewById(R.id.et_lastname); et_regaddress = (EditText)findViewById(R.id.et_address); @Override if(!firstname.equals("") || !lastname.equals("") || !address.equals("")){ Employee employee = new Employee(); employee.setFirstname(firstname); employee.setLastname(lastname); employee.setAddress(address); if (database.InsertData(employee)) { Toast.makeText(Registration.this, "Successfully Inserted Data", Toast.LENGTH_SHORT).show(); et_regfirstname.setText(""); et_reglastname.setText(""); et_regaddress.setText(""); } }else{ Toast.makeText(Registration.this, "Please complete the required field!", Toast.LENGTH_SHORT).show(); } } }); @Override Intent intent = new Intent(Registration.this, MainActivity.class); startActivity(intent); } }); } }
package com.razormist.simplecrudapplication; import android.content.DialogInterface; import android.content.Intent; import android.preference.DialogPreference; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class EmployeeDetail extends AppCompatActivity { private TextView tv_dname, tv_daddress; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_employee_detail); final Employee employee = (Employee) getIntent().getSerializableExtra("employee"); tv_dname = (TextView)findViewById(R.id.tv_dname); tv_daddress = (TextView)findViewById(R.id.tv_daddress); tv_dname.setText(employee.getFirstname() + " " + employee.getLastname()); tv_daddress.setText(employee.getAddress()); @Override Intent intent = new Intent(EmployeeDetail.this, MainActivity.class); startActivity(intent); } }); @Override Intent intent = new Intent(EmployeeDetail.this, UpdateEmployee.class); intent.putExtra("employee", employee); startActivity(intent); } }); @Override final AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext()); builder.setTitle("Confirmation"); builder.setMessage("Are you sure you want to delete this record?"); builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Database database = new Database(getBaseContext()); if(database.Delete(employee.getId())){ Intent intent = new Intent(EmployeeDetail.this, MainActivity.class); startActivity(intent); }else{ AlertDialog.Builder builder1 = new AlertDialog.Builder(getBaseContext()); builder1.setMessage("Fail"); builder1.setCancelable(false); builder1.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); builder1.create().show(); } } }); builder.setNegativeButton("No", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); builder.create().show(); } }); } }
package com.razormist.simplecrudapplication; import android.content.DialogInterface; import android.content.Intent; import android.support.v7.app.AlertDialog; 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 UpdateEmployee extends AppCompatActivity { private EditText et_ufirstname, et_ulastname, et_uaddress; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_update_employee); final Database db = new Database(getBaseContext()); final Employee employee = (Employee)getIntent().getSerializableExtra("employee"); et_ufirstname = (EditText)findViewById(R.id.et_ufirstname); et_ulastname = (EditText)findViewById(R.id.et_ulastname); et_uaddress = (EditText)findViewById(R.id.et_uaddress); et_ufirstname.setText(employee.getFirstname()); et_ulastname.setText(employee.getLastname()); et_uaddress.setText(employee.getAddress()); et_ufirstname.setSelection(et_ufirstname.getText().length()); et_ulastname.setSelection(et_ulastname.getText().length()); et_uaddress.setSelection(et_uaddress.getText().length()); @Override Intent intent = new Intent(UpdateEmployee.this, EmployeeDetail.class); intent.putExtra("employee", employee); startActivity(intent); } }); @Override employee.setFirstname(firstname); employee.setLastname(lastname); employee.setAddress(address); if(db.Update(employee)){ //Toast.makeText(UpdateEmployee.this, "SUCCESS!", Toast.LENGTH_SHORT).show(); AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext()); builder.setTitle("System"); builder.setMessage("SUCCESSFULLY UPDATED"); builder.setCancelable(false); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent intent = new Intent(UpdateEmployee.this, MainActivity.class); startActivity(intent); } }); builder.create().show(); }else{ Toast.makeText(UpdateEmployee.this, "FAILED!", Toast.LENGTH_SHORT).show(); } } }); } }
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
Hello razormist,
First of all, a really great example, it helped me a lot.
But I find there is one thing not working properly for me at least.
As your code sorts the list based on the "Last Name", when I click on a row, the selection seems being done by the "Id", rather than by the "position" it is shown on the screen.
For example, if I first save a record with a "Last Name" = Smith and then a second one with "Last Name" = Johnson, the list will show Johnson as first and the Smith, because of the sorting by Last Name.
But then, if I select on Johnson to Edit/Delete, what I see the app is selecting is the one with Smith.
Any solution to this?
Thank you.