Breaking News

How to show SQLite data on ListView

In this post, I will show you how to show SQLite data on ListView.





Firstly we create a Database Helper class to perform Database operations like Insert, Delete, Update, Show Data.

To follow Database operations, Follow the links for
Creation of Database
Insertion in SQLite Database
Deletion in Database
Updations in Database and
Get data from Database

In this we Insert data and then we get data and show it on ListView

For that, we create a ListView inside XML file.

activitymain.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:layout_width="match_parent"  
   android:layout_height="wrap_content"  
   android:id="@+id/name"  
   android:hint="name"  
   />  
   <EditText  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:id="@+id/marks"  
     android:hint="marks"  
     />  
   <EditText  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:id="@+id/ids"  
     android:hint="id"  
     />  
   <Button  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="Insert"  
     android:id="@+id/Button"/>  
   <Button  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="Show"  
     android:id="@+id/show"/>  
   <Button  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="Update"  
     android:id="@+id/Update"/>  
   <Button  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="Delete"  
     android:id="@+id/delete"/>  
   <ListView  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:id="@+id/Listview"/>  
 </LinearLayout>  

Here, we create a ListView in our XML file.

Then we create Listview instance, an ArrayList and Array Adapter

MainActivity.java
 ListView lv;  
   ArrayList list = new ArrayList();  
   ArrayAdapter adapter;  

Then we create a method of show list where we perform functionality to show items on ListView

MainActivity.java
 lv = findViewById(R.id.Listview);  
      
     showlist(); 
 
     lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {  
       @Override  
       public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {  
         Toast.makeText(MainActivity.this, String.valueOf(lv.getAdapter().getItem(i)), Toast.LENGTH_SHORT).show();  
       }  
     });  
   }  

   public void showlist()  
   {  
     showbtn.setOnClickListener(new View.OnClickListener() {  
       @Override  
       public void onClick(View view) {  
         list.clear();  
         Cursor cursor = mydb.Showdata();  
         if(cursor.getCount() == 0)  
         {  
           Toast.makeText(MainActivity.this, "No Data", Toast.LENGTH_SHORT).show();  
         }  
         while(cursor.moveToNext())  
         {  
           list.add(cursor.getString(0));  
           list.add(cursor.getString(1));  
           list.add(cursor.getString(2));  
         }  
         adapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,list);  
         lv.setAdapter(adapter);  
       }  
     });  
   }  
 }  

Here We find the id of our Listview then create a Showlist method in which we add values to Array List then set ArrayList to the adapter and then set the adapter to ListView.

also, we add list.clear to clean the list before adding new items.

then we create onitemclicklistener to perform clicking on listview items.

then call the show list method in oncreate.

Follow us for more posts like this,
Subscribe Harpreet studio on Youtube
Like Harpreet Studio on Facebook
Follow me on Instagram

No comments