Create an application to make Insert , update , Delete and retrieve operation on the database.
MainActivity.java
MainActivity.java
package com.example.assignment18;
import androidx.appcompat.app.AppCompatActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.assignment18.Database.AppDatabase;
import com.example.assignment18.Database.Person;
import java.util.List;
public class MainActivity extends AppCompatActivity {
TextView tvMessage;
EditText etName;
EditText etAge;
EditText etPhoneNumber;
Button btnInsert;
Button btnUpdate;
Button btnDelete;
ListView listView;
Person personToUpdate;
PersonAdapter personAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
initListViewAdapter();
loadDataFromDatabase();
setClickListeners();
clearAllFields();
}
private void initViews() {
tvMessage = findViewById(R.id.tvMessage);
etName = findViewById(R.id.etName);
etAge = findViewById(R.id.etAge);
etPhoneNumber = findViewById(R.id.etPhoneNumber);
btnInsert = findViewById(R.id.btnInsert);
btnUpdate = findViewById(R.id.btnUpdate);
btnDelete = findViewById(R.id.btnDelete);
listView = findViewById(R.id.listView);
}
private boolean isValid() {
String name = etName.getText().toString().trim();
String age = etAge.getText().toString().trim();
String phoneNumber = etPhoneNumber.getText().toString().trim();
if (TextUtils.isEmpty(name) || TextUtils.isEmpty(age) || TextUtils.isEmpty(phoneNumber)) {
Toast.makeText(MainActivity.this, "All Fields are mandatory", Toast.LENGTH_SHORT).show();
return false;
} else {
return true;
}
}
private void initListViewAdapter() {
personAdapter = new PersonAdapter(this);
listView.setAdapter(personAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
personToUpdate = personAdapter.getItem(position);
etName.setText(personToUpdate.name);
etAge.setText(personToUpdate.age);
etPhoneNumber.setText(personToUpdate.phoneNumber);
tvMessage.setText("Click to Update or Delete or reset");
btnInsert.setEnabled(false);
btnUpdate.setEnabled(true);
btnDelete.setEnabled(true);
}
});
}
private void loadDataFromDatabase() {
new AsyncTask<Void, Void, List<Person>>() {
@Override
protected List<Person> doInBackground(Void... voids) {
return AppDatabase.getInstance(MainActivity.this.getApplicationContext()).personDao().getAllPerson();
}
@Override
protected void onPostExecute(List<Person> people) {
super.onPostExecute(people);
personAdapter.clearAndAddItemAndNotify(people);
}
}.execute();
}
private void setClickListeners() {
btnInsert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isValid()) {
final Person person = new Person();
person.name = etName.getText().toString().trim();
person.age = etAge.getText().toString().trim();
person.phoneNumber = etPhoneNumber.getText().toString().trim();
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
AppDatabase.getInstance(MainActivity.this.getApplicationContext()).personDao().insert(person);
return null;
}
@Override
protected void onPostExecute(Void returnValue) {
Toast.makeText(MainActivity.this, "Insert Successfully", Toast.LENGTH_SHORT).show();
loadDataFromDatabase();
clearAllFields();
}
}.execute();
}
}
});
btnUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isValid()) {
if (personToUpdate != null) {
personToUpdate.name = etName.getText().toString().trim();
personToUpdate.age = etAge.getText().toString().trim();
personToUpdate.phoneNumber = etPhoneNumber.getText().toString().trim();
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
AppDatabase.getInstance(MainActivity.this.getApplicationContext()).personDao().update(personToUpdate);
return null;
}
@Override
protected void onPostExecute(Void returnValue) {
Toast.makeText(MainActivity.this, "Update Successfully", Toast.LENGTH_SHORT).show();
loadDataFromDatabase();
clearAllFields();
}
}.execute();
}
}
}
});
btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (personToUpdate != null) {
if (personToUpdate != null) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
AppDatabase.getInstance(MainActivity.this.getApplicationContext()).personDao().delete(personToUpdate);
return null;
}
@Override
protected void onPostExecute(Void returnValue) {
Toast.makeText(MainActivity.this, "Delete Successfully", Toast.LENGTH_SHORT).show();
loadDataFromDatabase();
clearAllFields();
}
}.execute();
}
}
}
});
tvMessage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clearAllFields();
}
});
}
private void clearAllFields() {
personToUpdate = null;
etName.setText("");
etAge.setText("");
etPhoneNumber.setText("");
tvMessage.setText("Insert New Data");
btnInsert.setEnabled(true);
btnUpdate.setEnabled(false);
btnDelete.setEnabled(false);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/tvMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="18dp"
android:text="Insert New Data"
android:textColor="#000000"
android:textSize="24dp" />
<EditText
android:id="@+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_x="0dp"
android:layout_y="77dp"
android:hint="Name"
android:textSize="18sp" />
<EditText
android:id="@+id/etAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_x="0dp"
android:layout_y="133dp"
android:hint="Age"
android:inputType="number"
android:textSize="18sp" />
<EditText
android:id="@+id/etPhoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_x="2dp"
android:layout_y="196dp"
android:hint="Phone Number"
android:inputType="phone"
android:textSize="18sp" />
<Button
android:id="@+id/btnInsert"
android:layout_width="111dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_x="5dp"
android:layout_y="240dp"
android:text="Insert" />
<Button
android:id="@+id/btnUpdate"
android:layout_width="111dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_x="142dp"
android:layout_y="241dp"
android:text="Update" />
<Button
android:id="@+id/btnDelete"
android:layout_width="111dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_x="273dp"
android:layout_y="241dp"
android:text="Delete" />
<ListView
android:id="@+id/listView"
android:layout_width="389dp"
android:layout_height="424dp"
android:layout_x="5dp"
android:layout_y="304dp" />
</AbsoluteLayout>
PersonAdapter.java
package com.example.assignment18;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.example.assignment18.Database.Person;
import java.util.ArrayList;
import java.util.List;
public class PersonAdapter extends BaseAdapter {
private ArrayList<Person> list;
Context _context;
public PersonAdapter(Context context) {
_context = context;
list = new ArrayList<Person>();
}
@Override
public int getCount() {
return list.size();
}
@Override
public Person getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = LayoutInflater.from(_context).inflate(R.layout.item_person, parent, false);
TextView tvName = view.findViewById(R.id.tvName);
TextView tvAge = view.findViewById(R.id.tvAge);
TextView tvPhoneNumber = view.findViewById(R.id.tvPhoneNumber);
Person person = getItem(position);
tvName.setText(person.name);
tvAge.setText(person.age);
tvPhoneNumber.setText(person.phoneNumber);
return view;
}
public void clearAndAddItemAndNotify(List<Person> peopleList) {
list.clear();
list.addAll(peopleList);
notifyDataSetChanged();
}
}
item_person.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvName"
android:layout_width="405dp"
android:layout_height="wrap_content"
android:layout_x="4dp"
android:layout_y="5dp"
android:text="NAME"
android:textColor="#000000"
android:textSize="18sp" />
<TextView
android:id="@+id/tvAge"
android:layout_width="406dp"
android:layout_height="wrap_content"
android:layout_x="2dp"
android:layout_y="32dp"
android:text="Age"
android:textColor="#000000"
android:textSize="18sp" />
<TextView
android:id="@+id/tvPhoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_x="2dp"
android:layout_y="60dp"
android:text="Phone Number"
android:textColor="#000000"
android:textSize="18sp" />
</AbsoluteLayout>
AppDatabase.java
package com.example.assignment18.Database;
import android.content.Context;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
@Database(entities = {Person.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
public abstract PersonDao personDao();
private static AppDatabase appDatabase;
public static AppDatabase getInstance(Context context) {
if (appDatabase == null) {
appDatabase = Room.databaseBuilder(context, AppDatabase.class, "MyDatabaseName").build();
}
return appDatabase;
}
}
Person.java
package com.example.assignment18.Database;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey;
@Entity
public class Person {
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id")
public int id;
@ColumnInfo(name = "name")
public String name;
@ColumnInfo(name = "age")
public String age;
@ColumnInfo(name = "phoneNumber")
public String phoneNumber;
}
PersonDao.java
package com.example.assignment18.Database;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query;
import androidx.room.Update;
import java.util.ArrayList;
import java.util.List;
@Dao
public interface PersonDao {
@Query("SELECT * FROM Person")
List<Person> getAllPerson();
@Query("SELECT * FROM Person WHERE id = :id")
List<Person> findPersonWithId(String id);
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertAll(ArrayList<Person> person);
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(Person person);
@Update()
void update(Person person);
@Query("UPDATE Person SET name = :name WHERE id = :id")
void updateQuery(String id, String name);
@Delete()
void delete(Person person);
@Query("DELETE FROM Person WHERE id = :id")
void deleteQuery(String id);
}
No comments:
Post a Comment