spinner in android studio
in this blog post, we will use the spinner widget and display its listview using the Array adapter.
also, we used the SQLite database to store user data in the database.
How to get value from the spinner and display it in listview with SQLite Database in android
we will make a simple spinner example.
This blog post shows you a spinner android example.
We will set value in spinner dynamically using SQLite Database.
Spinner value display in a listview using an adapter.
DBmain.java
package com.pd.spinnersqlitelistview; 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="computer.db"; public static final int VER=1; String query; public DBmain(@Nullable Context context) { super(context, DB, null, VER); } @Override public void onCreate(SQLiteDatabase db) { query="create table course(id integer primary key,category text)"; db.execSQL(query); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { query="drop table if exists course"; db.execSQL(query); onCreate(db); } }
we will use the default activity_main file,
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:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="select option" /> <Spinner android:id="@+id/spinner" android:layout_width="match_parent" android:layout_height="wrap_content" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"> <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>
MainAcitivity.java
package com.pd.spinnersqlitelistview; 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.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.Spinner; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener { DBmain dBmain; SQLiteDatabase sqLiteDatabase; Spinner spinner; String[]name={"C","C++","Java","Php","Python"}; Button button,button2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dBmain=new DBmain(MainActivity.this); findid(); insertData(); } private void insertData() { //when click on submit button insert data into database button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ContentValues contentValues=new ContentValues(); contentValues.put("category",spinner.getItemAtPosition(spinner.getSelectedItemPosition()).toString()); sqLiteDatabase=dBmain.getWritableDatabase(); 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(); } } }); //now 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); spinner=(Spinner)findViewById(R.id.spinner); spinner.setOnItemSelectedListener(this); ArrayAdapter arrayAdapter=new ArrayAdapter(this, android.R.layout.simple_spinner_item,name); arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(arrayAdapter); } @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { } @Override public void onNothingSelected(AdapterView<?> parent) { } }
after creating a new activity, we will use the listview widget in a second activity XML file.
activity_display_data.xml
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" /> </LinearLayout>
so we will use an adapter in java class file.
DisplayData.java
package com.pd.spinnersqlitelistview; 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; String[]item; int[]id; ListView listView; @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()]; item=new String[cursor.getCount()]; int i=0; while (cursor.moveToNext()){ id[i]=cursor.getInt(0); item[i]=cursor.getString(1); i++; } Custom custom=new Custom(); listView.setAdapter(custom); } } private void findid() { listView=(ListView)findViewById(R.id.lv); } private class Custom extends BaseAdapter { @Override public int getCount() { return item.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; convertView=LayoutInflater.from(DisplayData.this).inflate(R.layout.singledata,parent,false); textView=convertView.findViewById(R.id.viewtext); textView.setText(item[position]); return convertView; } } }
singledata.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/viewtext" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" android:textSize="25sp" /> </LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.pd.spinnersqlitelistview"> <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.SpinnerSQLiteListView"> <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/gKSn6X05BkI
SUBSCRIBE to download
Download Source code