Create a CRUD operation in Android Studio
We will perform create, read, update, delete operations using SQLite database. Adding data and using SQLite database will show the data in ListView.
In this blog post, a simple CRUD operation example will perform in a ListView with an SQLite database.
We will create, insert, display, edit, and delete SQLite databases in an Android listview using the SQLite OpenHelper class.
In last, the source code is also given.
First, we will create the database file named
DBmain.java
package com.pd.listviewsqlite; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import androidx.annotation.Nullable; public class DBmain extends SQLiteOpenHelper { public DBmain(@Nullable Context context) { super(context, "namedb", null, 1); } @Override public void onCreate(SQLiteDatabase db) { String sql="create table thing(id integer primary key,name text)"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { String sql="drop table if exists thing"; db.execSQL(sql); onCreate(db); } }
Now we will create a single XML view file to display data
singledata.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/txt_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="50dp" android:layout_marginTop="10dp" android:text="@string/app_name" /> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:id="@+id/edit_data" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_marginTop="10dp" android:src="@drawable/ic_baseline_edit_24" /> <ImageView android:id="@+id/delete_data" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:src="@drawable/ic_baseline_delete_24" /> </RelativeLayout> </RelativeLayout>
In the main activity, we will add data into the SQLite database
MainActivity.java
package com.pd.listviewsqlite; import androidx.appcompat.app.AppCompatActivity; import android.content.ContentValues; import android.content.Intent; import android.database.sqlite.SQLiteDatabase; 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 static DBmain dBmain; private static EditText edtname; private static Button submit, edit, display; private static SQLiteDatabase sqLiteDatabase; private static int id = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dBmain = new DBmain(MainActivity.this); //create method findid(); getData(); clear(); editData(); } private void editData() { if (getIntent().getBundleExtra("userdata")!=null){ Bundle bundle=getIntent().getBundleExtra("userdata"); id=bundle.getInt("id"); edtname.setText(bundle.getString("name")); edit.setVisibility(View.VISIBLE); submit.setVisibility(View.GONE); } } private void clear() { edtname.setText(""); } private void getData() { submit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ContentValues contentValues=new ContentValues(); contentValues.put("name",edtname.getText().toString()); sqLiteDatabase=dBmain.getWritableDatabase(); Long recid=sqLiteDatabase.insert("thing",null,contentValues); if (recid!=null){ Toast.makeText(MainActivity.this, "Data inserted successfully", Toast.LENGTH_SHORT).show(); clear(); }else{ Toast.makeText(MainActivity.this, "something wrong try again", Toast.LENGTH_SHORT).show(); } } }); // now display data display.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(MainActivity.this,DisplayData.class)); } }); //now edit the data edit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ContentValues contentValues=new ContentValues(); contentValues.put("name",edtname.getText().toString()); sqLiteDatabase=dBmain.getWritableDatabase(); long recid=sqLiteDatabase.update("thing",contentValues,"id="+id,null); if (recid!=-1){ Toast.makeText(MainActivity.this, "Data update successfully", Toast.LENGTH_SHORT).show(); // when successfully edit data submit button visible and edit button disapear submit.setVisibility(View.VISIBLE); edit.setVisibility(View.GONE); clear(); }else{ Toast.makeText(MainActivity.this, "something wrong try again", Toast.LENGTH_SHORT).show(); } } }); } private void findid() { edtname=findViewById(R.id.edtname); submit=findViewById(R.id.btn_submit); edit=findViewById(R.id.btn_edit); display=findViewById(R.id.btn_display); } }
we can add data through this file.
we will create the edit box and some buttons.
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" tools:context=".MainActivity"> <EditText android:id="@+id/edtname" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:hint="Name" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:orientation="horizontal"> <Button android:id="@+id/btn_submit" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="submit" /> <Button android:id="@+id/btn_edit" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="submit" android:visibility="gone" /> <Button android:id="@+id/btn_display" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="display" /> </LinearLayout> </LinearLayout>
Create new activity named DispalyData.
After inserting data into SQLite,
Now we will fetch each data.
DisplayData.java
package com.pd.listviewsqlite; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class DisplayData extends AppCompatActivity { DBmain dBmain; SQLiteDatabase sqLiteDatabase; ListView listView; String[] name; int[] id; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_display_data); dBmain = new DBmain(DisplayData.this); //create method findid(); dis(); } //for display data private void dis() { sqLiteDatabase = dBmain.getReadableDatabase(); Cursor cursor = sqLiteDatabase.rawQuery("select *from thing", null); if (cursor.getCount() > 0) { id = new int[cursor.getCount()]; name = new String[cursor.getCount()]; int i = 0; while (cursor.moveToNext()) { id[i] = cursor.getInt(0); name[i] = cursor.getString(1); i++; } Custom adapter = new Custom(); listView.setAdapter(adapter); } } private void findid() { listView = findViewById(R.id.listview); } private class Custom extends BaseAdapter { @Override public int getCount() { return name.length; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { TextView textView; ImageView edit, delete; convertView = LayoutInflater.from(DisplayData.this).inflate(R.layout.singledata, parent, false); textView = convertView.findViewById(R.id.txt_name); edit = convertView.findViewById(R.id.edit_data); delete = convertView.findViewById(R.id.delete_data); textView.setText(name[position]); edit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Bundle bundle = new Bundle(); bundle.putInt("id", id[position]); bundle.putString("name", name[position]); Intent intent = new Intent(DisplayData.this, MainActivity.class); intent.putExtra("userdata", bundle); startActivity(intent); } }); delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sqLiteDatabase = dBmain.getReadableDatabase(); long recd = sqLiteDatabase.delete("thing", "id=" + id[position], null); if (recd != -1) { Toast.makeText(DisplayData.this, "Record deleted successfully", Toast.LENGTH_SHORT).show(); dis(); } } }); return convertView; } } }
We will create a list file with a list view to display data
activity_display.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=".DisplayData"> <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/listview" android:layout_weight="1"/> </LinearLayout>
https://youtu.be/ejLEPMS3wds
SUBSCRIBE to downloadDownload Source code