display checkbox value in listview in the android with SQLite database
How to display a checkbox in android studio
how to get multiple checkbox values and display listview in an android SQLite database
The checkbox box is a more useful widget for the user, inserting values without typing a single word.
The checkbox is very useful when there are multiple choices.
In this blog post, I will show you how to insert multiple checkbox values? in an SQLite database and display it in a listview.
The checkbox allows the user checkbox to show multiple checkbox value stores in the SQLite database and also needs to fetch this value using the adapter.
First, we will create the SQLiteOpenHelper class file.DBmain.java
package com.pd.checkbox; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import androidx.annotation.Nullable; public class DBmain extends SQLiteOpenHelper { public static final String DB="student"; public static final int VER=1; public DBmain(@Nullable Context context) { super(context, DB, null, VER); } @Override public void onCreate(SQLiteDatabase db) { String query="create table course(id integer primary key,sub text,sub2 text)"; db.execSQL(query); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { String drop="drop table if exists course"; db.execSQL(drop); onCreate(db); } }
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" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="Select your favorite Subject" android:textSize="18dp" android:textStyle="bold" /> <CheckBox android:id="@+id/checkBox" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Algorithms" /> <CheckBox android:id="@+id/checkBox2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Data Structure" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="submit" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Display" /> </LinearLayout> </LinearLayout>
MainActivity.java
package com.pd.checkbox; 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.CheckBox; import android.widget.Toast; public class MainActivity extends AppCompatActivity { DBmain dBmain; SQLiteDatabase sqLiteDatabase; Button button,button2; CheckBox checkBox,checkBox2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dBmain=new DBmain(MainActivity.this); findid(); insertData(); } private void insertData() { button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sqLiteDatabase=dBmain.getWritableDatabase(); ContentValues contentValues=new ContentValues(); if (checkBox.isChecked()) contentValues.put("sub",checkBox.getText().toString()); if (checkBox2.isChecked()) contentValues.put("sub2",checkBox2.getText().toString()); Long rec=sqLiteDatabase.insert("course",null,contentValues); if (rec!=null){ Toast.makeText(MainActivity.this, "data inserted", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(MainActivity.this, "data not inserted", Toast.LENGTH_SHORT).show(); } } }); //display data button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(MainActivity.this,DisplayData.class)); } }); } private void findid() { button=(Button)findViewById(R.id.button); button2=(Button)findViewById(R.id.button2); checkBox2=(CheckBox)findViewById(R.id.checkBox2); checkBox=(CheckBox)findViewById(R.id.checkBox); } }
Now, we will create a new activity to show data into listview.
so first of we will create an XML file in a new activity.
activity_display_data.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:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent" android:divider="@color/purple_200" android:dividerHeight="1dp" android:listSelector="@color/teal_200" /> </LinearLayout>
DisplayData.java
After we will create a single XML file named
package com.pd.checkbox; import androidx.appcompat.app.AppCompatActivity; 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.ListView; import android.widget.TextView; public class DisplayData extends AppCompatActivity { DBmain dBmain; SQLiteDatabase sqLiteDatabase; ListView listView; String[]name,name2; int[]id; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_display_data); dBmain=new DBmain(DisplayData.this); findid(); displayData(); } private void displayData() { sqLiteDatabase=dBmain.getReadableDatabase(); Cursor cursor=sqLiteDatabase.rawQuery("select *from course",null); if (cursor.getCount()>0){ id=new int[cursor.getCount()]; name=new String[cursor.getCount()]; name2=new String[cursor.getCount()]; int i=0; while(cursor.moveToNext()){ id[i]=cursor.getInt(0); name[i]=cursor.getString(1); name2[i]=cursor.getString(2); i++; } Custom custom=new Custom(); listView.setAdapter(custom); } } private void findid() { listView=findViewById(R.id.lv); } 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,textView1; convertView= LayoutInflater.from(DisplayData.this).inflate(R.layout.singledata,parent,false); textView=(TextView)convertView.findViewById(R.id.textView); textView1=(TextView)convertView.findViewById(R.id.textView2); textView.setText(name[position]); textView1.setText(name2[position]); return convertView; } } }
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="match_parent"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" android:textSize="18dp" android:textStyle="bold" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:text="TextView" android:textSize="18dp" android:textStyle="bold" /> </RelativeLayout>
AndroidManifest.xml
AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.pd.checkbox"> <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/Theme.CheckBox"> <activity android:name=".DisplayData"></activity> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
https://youtu.be/NKKqJkaBW_8