SQ Lite CRUD Performance with RecyclerView in Android Studio - AndroidCode

STEP 1 : (XML) Create Main Activity


<?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=".MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">

<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:hint="Enter Name" />

<EditText
android:id="@+id/et_surname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:hint="Enter Surname" />

<Button
android:id="@+id/btn_insert"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="32dp"
android:text="insert" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">

<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#696969" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>

</LinearLayout>





STEP 2 : (XML) Create Item




<?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="wrap_content"
android:orientation="vertical"
android:padding="0dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">


<TextView
android:id="@+id/tv_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="2.5"
android:gravity="center"
android:text="1"
android:textSize="16dp" />

<TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="2"
android:text="@string/app_name"
android:textSize="16dp" />

<TextView
android:id="@+id/tv_surname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="2"
android:text="@string/app_name"
android:textSize="16dp" />

<ImageView
android:id="@+id/iv_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:clickable="true"
android:foreground="?selectableItemBackground"
android:src="@drawable/ic_edit" />

<ImageView
android:id="@+id/iv_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:clickable="true"
android:foreground="?selectableItemBackground"
android:src="@drawable/ic_delete" />

</LinearLayout>

<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#696969" />

</LinearLayout>





STEP 3 : (XML) Create Edit Dialog




<?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="wrap_content"
android:background="#ffffff"
android:orientation="vertical">

<EditText
android:id="@+id/update_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:enabled="false"
android:text="id" />

<EditText
android:id="@+id/update_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="Name" />

<EditText
android:id="@+id/update_surname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="Surname" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
android:id="@+id/update_Button_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:layout_weight="1"
android:backgroundTint="#696969"
android:text="CANCEL" />

<Button
android:id="@+id/update_Button_update"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:layout_weight="1"
android:backgroundTint="#008000"
android:text="UPDATE" />

</LinearLayout>

</LinearLayout>










STEP 4 : (XML) Create Delete Dialog




<?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="wrap_content"
android:background="#ffffff"
android:orientation="vertical">

<EditText
android:id="@+id/delete_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:enabled="false"
android:text="id" />

<EditText
android:id="@+id/delete_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:enabled="false"
android:text="Name" />

<EditText
android:id="@+id/delete_surname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:enabled="false"
android:text="Surname" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
android:id="@+id/delete_Button_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:layout_weight="1"
android:backgroundTint="#696969"
android:text="CANCEL" />

<Button
android:id="@+id/delete_Button_delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:layout_weight="1"
android:backgroundTint="#FF0000"
android:text="DELETE" />

</LinearLayout>

</LinearLayout>





STEP 5 : Create SQLite Database Class




public class MyDataBase extends SQLiteOpenHelper {

public MyDataBase(@Nullable Context context) {
super(context, "mydb.db", null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
String query = "create table student(id integer primary key autoincrement, name text, surname text)";
db.execSQL(query);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String query = "drop table if exists student";
onCreate(db);
db.execSQL(query);
}

public void insertData(String name, String surname) {
SQLiteDatabase database = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name", name);
contentValues.put("surname", surname);
database.insert("student", null, contentValues);
}

public void updateData(String id, String name, String surname) {
SQLiteDatabase database = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("id", id);
contentValues.put("name", name);
contentValues.put("surname", surname);
database.update("student", contentValues, "id=?", new String[]{id});
}

public void deleteData(String id) {
SQLiteDatabase database = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("id", id);
database.delete("student", "id=?", new String[]{id});
}

public Cursor getData() {
SQLiteDatabase database = getReadableDatabase();
String query = "select * from student";
Cursor cursor = database.rawQuery(query, null);
return cursor;
}
}





STEP 6 : Create Model Class (for Need by You)




public class ModelData {

String id, name, surname;

public ModelData(String id, String name, String surname) {
this.id = id;
this.name = name;
this.surname = surname;
}

}





STEP 7 : Create Adapter




public class MyAdapter extends RecyclerView.Adapter<MyAdapter.DataHolder> {

private Context context;
private LayoutInflater inflater;
private List<ModelData> dataList;

private MyDataBase myDataBase;
private RecyclerView recyclerView;
private MyAdapter myAdapter;


public MyAdapter(MainActivity mainActivity, List<ModelData> dataList, MyDataBase myDataBase) {
this.context = mainActivity;
this.dataList = dataList;
inflater = LayoutInflater.from(context);

this.myDataBase = myDataBase;
}

@NonNull
@Override
public DataHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.item, parent, false);
return new DataHolder(view);
}

public void clearRecyclerView() {
int size = this.dataList.size();
if (size > 0) {
for (int i = 0; i < size; i++) {
dataList.remove(0);
}
this.notifyItemRangeRemoved(0, size);
}
}

@Override
public void onBindViewHolder(@NonNull DataHolder holder, int position) {


holder.tv_id.setText(dataList.get(position).id);
holder.tv_name.setText(dataList.get(position).name);
holder.tv_surname.setText(dataList.get(position).surname);

holder.iv_edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(v.getContext());
LayoutInflater inflater = (LayoutInflater) v.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dialogView = inflater.inflate(R.layout.dialog_edit, null);
dialogBuilder.setView(dialogView);
dialogBuilder.setCancelable(false);
final AlertDialog dialog = dialogBuilder.create();
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();

EditText update_id = dialog.findViewById(R.id.update_id);
EditText update_name = dialog.findViewById(R.id.update_name);
EditText update_surname = dialog.findViewById(R.id.update_surname);
Button update_Button_cancel = dialog.findViewById(R.id.update_Button_cancel);
Button update_Button_update = dialog.findViewById(R.id.update_Button_update);

//TODO: Dialog Click To Show Data...
update_id.setText(holder.tv_id.getText().toString());
update_name.setText(holder.tv_name.getText().toString());
update_surname.setText(holder.tv_surname.getText().toString());

update_Button_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});

update_Button_update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String id = update_id.getText().toString().trim();
String new_name = update_name.getText().toString().trim();
String new_surname = update_surname.getText().toString().trim();

myDataBase.updateData(id, new_name, new_surname);
dialog.dismiss();

//TODO: Re-fetching Data...
//MainActivity.imageRefresh.callOnClick();

((MainActivity) context).refreshData();

}
});
}
});

holder.iv_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(v.getContext());
LayoutInflater inflater = (LayoutInflater) v.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dialogView = inflater.inflate(R.layout.dialog_delete, null);
dialogBuilder.setView(dialogView);
dialogBuilder.setCancelable(false);
final AlertDialog dialog = dialogBuilder.create();
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();

EditText delete_id = dialog.findViewById(R.id.delete_id);
EditText delete_name = dialog.findViewById(R.id.delete_name);
EditText delete_surname = dialog.findViewById(R.id.delete_surname);
Button delete_Button_cancel = dialog.findViewById(R.id.delete_Button_cancel);
Button delete_Button_delete = dialog.findViewById(R.id.delete_Button_delete);

//TODO: Dialog Click To Show Data...
delete_id.setText(holder.tv_id.getText().toString());
delete_name.setText(holder.tv_name.getText().toString());
delete_surname.setText(holder.tv_surname.getText().toString());

delete_Button_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});

delete_Button_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String id = delete_id.getText().toString().trim();

myDataBase.deleteData(id);
dialog.dismiss();

//TODO: Re-fetching Data...
//MainActivity.imageRefresh.callOnClick();
((MainActivity) context).refreshData();

}
});
}
});
}


@Override
public int getItemCount() {
return dataList.size();
}

public class DataHolder extends RecyclerView.ViewHolder {

private TextView tv_id, tv_name, tv_surname;
private ImageView iv_edit, iv_delete;

public DataHolder(@NonNull View itemView) {
super(itemView);

tv_id = itemView.findViewById(R.id.tv_id);
tv_name = itemView.findViewById(R.id.tv_name);
tv_surname = itemView.findViewById(R.id.tv_surname);
iv_edit = itemView.findViewById(R.id.iv_edit);
iv_delete = itemView.findViewById(R.id.iv_delete);
}
}
}





STEP 8 : Create Main Activity




public class MainActivity extends AppCompatActivity {

private EditText et_name, et_surname;
private Button btn_insert;
private RecyclerView recyclerView;
private List<ModelData> dataList = new ArrayList<>();

private MyAdapter myAdapter;
private MyDataBase myDataBase;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

et_name = findViewById(R.id.et_name);
et_surname = findViewById(R.id.et_surname);
btn_insert = findViewById(R.id.btn_insert);
recyclerView = findViewById(R.id.recyclerView);

myDataBase = new MyDataBase(this);
myAdapter = new MyAdapter(this, dataList, myDataBase);
refreshData();

btn_insert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

String name = et_name.getText().toString();
String surname = et_surname.getText().toString();

if (name.isEmpty()) {
et_name.setError("Enter name");
} else if (surname.isEmpty()) {
et_surname.setError("Enter surname");
} else {

clearEditText();

myDataBase.insertData(name, surname);
refreshData();
}

}
});
}

private void clearEditText() {
et_name.setText(null);
et_surname.setText(null);
}

public void refreshData() {

myAdapter.clearRecyclerView();

Cursor cursor = myDataBase.getData();
cursor.moveToFirst();

for (int i = 0; i < cursor.getCount(); i++) {
String id = cursor.getString(0);
String name = cursor.getString(1);
String surname = cursor.getString(2);

ModelData modelData = new ModelData(id, name, surname);
dataList.add(modelData);

cursor.moveToNext();
}

//set Adapter...
Log.e("student", "adapter called: ");
RecyclerView.LayoutManager manager = new LinearLayoutManager(MainActivity.this);
recyclerView.setLayoutManager(manager);
recyclerView.setAdapter(myAdapter);
}
}