java - SQLiteDatabase: Get column and store into an array -
is there way grab column database created, , store values found in column array?
my main goal, values in column, , store them spinner.
look below code!
patientdbhelper.java
package tanav.sharma; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import android.provider.basecolumns; import android.util.log; import java.util.arraylist; import java.util.list; /** * created tanav on 11/4/2016. */ public class patientdbhelper extends sqliteopenhelper { private static final string database_name = "patientinfo.db"; private static final int database_version = 1; private static final string create_query = "create table " + patientinfo.newpatientinfo.table_name + " (" + patientinfo.newpatientinfo.patient_id + " integer primary key autoincrement, " + patientinfo.newpatientinfo.patient_fname+ " text," + patientinfo.newpatientinfo.patient_lname+" text," + patientinfo.newpatientinfo.patient_department+" text);"; private static final string create_query_test = "create table " + testinfo.newtestinfo.table_test_name + " (" + testinfo.newtestinfo.test_blood_pressure + " text," + testinfo.newtestinfo.test_blood_oxygen_level + " text," + testinfo.newtestinfo.test_respitory_rate +" text," + testinfo.newtestinfo.test_heart_rate +" text);"; public patientdbhelper(context context){ super(context,database_name,null,database_version); log.e("database operations","database created / opened ..."); } @override public void oncreate(sqlitedatabase db) { db.execsql(create_query); db.execsql(create_query_test); log.e("database operations","table created..."); } public void addinformations(int id, string fname, string lname, string department, sqlitedatabase db){ contentvalues contentvalues = new contentvalues(); if ( id != 0 ){ contentvalues.put(patientinfo.newpatientinfo.patient_id, id); } contentvalues.put(patientinfo.newpatientinfo.patient_fname,fname); contentvalues.put(patientinfo.newpatientinfo.patient_lname,lname); contentvalues.put(patientinfo.newpatientinfo.patient_department,department); db.insert(patientinfo.newpatientinfo.table_name, null,contentvalues); log.e("database operations","one in row inserted..."); } public void addtestinformation(/*string bp*/ int bl, int rr, int hr, sqlitedatabase db){ contentvalues contenttestvalues = new contentvalues(); //contenttestvalues.put(testinfo.newtestinfo.test_blood_pressure, bp); contenttestvalues.put(testinfo.newtestinfo.test_blood_oxygen_level,bl); contenttestvalues.put(testinfo.newtestinfo.test_respitory_rate, rr); contenttestvalues.put(testinfo.newtestinfo.test_heart_rate, hr); db.insert(testinfo.newtestinfo.table_test_name, null, contenttestvalues); log.e("database operations", "one in row inserted..."); } /* public cursor getpatientdepartment(string departments, sqlitedatabase sqlitedatabase){ string[] projections = {patientinfo.newpatientinfo.patient_fname, patientinfo.newpatientinfo.patient_lname, patientinfo.newpatientinfo.patient_department}; string selection = patientinfo.newpatientinfo.patient_department+ " ?"; string[] selection_args = {departments}; cursor cursor = sqlitedatabase.query(patientinfo.newpatientinfo.table_name,projections,selection,selection_args,null,null,null); return cursor; }*/ public cursor getpatientsid(int patient_id,sqlitedatabase sqlitedatabase){ string[] projections = {patientinfo.newpatientinfo.patient_id, patientinfo.newpatientinfo.patient_fname, patientinfo.newpatientinfo.patient_department}; string selection = patientinfo.newpatientinfo.patient_id+ " ?"; string convert = string.valueof(patient_id); string[] selection_args = {convert}; cursor cursor = sqlitedatabase.query(patientinfo.newpatientinfo.table_name,projections,selection,selection_args,null,null,null); return cursor; } public cursor getinformations(sqlitedatabase db){ cursor cursor; string[] projections = {patientinfo.newpatientinfo.patient_fname, patientinfo.newpatientinfo.patient_lname, patientinfo.newpatientinfo.patient_department}; cursor = db.query(patientinfo.newpatientinfo.table_name,projections,null,null,null,null,null); return cursor; } public cursor gettestinformation(sqlitedatabase db){ cursor cursortest; string[] testprojections = {testinfo.newtestinfo.test_heart_rate, testinfo.newtestinfo.test_respitory_rate, testinfo.newtestinfo.test_blood_oxygen_level, testinfo.newtestinfo.test_blood_pressure}; cursortest = db.query(testinfo.newtestinfo.table_test_name,testprojections,null,null,null,null,null); return cursortest; } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { } }
above database handler class, want grab column patient_fname
, store values array. later on, in addtest.java file, want display these values spinner.
below addtest.java
package tanav.sharma; import android.app.alertdialog; import android.content.context; import android.content.intent; import android.database.sqlite.sqlitedatabase; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.util.log; import android.view.view; import android.widget.arrayadapter; import android.widget.button; import android.widget.edittext; import android.widget.radiobutton; import android.widget.radiogroup; import android.widget.spinner; import android.widget.toast; import java.util.arraylist; import java.util.list; public class addtest extends appcompatactivity { private radiogroup radiogroup; private radiobutton radiobutton; edittext etbl, etrr, ethbr; spinner patients; context context; patientdbhelper patientdbhelper; sqlitedatabase sqlitedatabase; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_add_test); getsupportactionbar().settitle(r.string.add_test); getsupportactionbar().setdisplayhomeasupenabled(true); etbl = (edittext) findviewbyid(r.id.etbol); etrr = (edittext) findviewbyid(r.id.etrr); ethbr = (edittext) findviewbyid(r.id.ethbr); patients = (spinner) findviewbyid(r.id.spinner); } public void addtest(view view){ radiogroup = (radiogroup) findviewbyid(r.id.radio); button addtest = (button) findviewbyid(r.id.addtest); addtest.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { int selectedid = radiogroup.getcheckedradiobuttonid(); radiobutton radiobutton = (radiobutton) findviewbyid(selectedid); } }); //string bp = radiobutton.gettext().tostring(); int bl = integer.parseint(etbl.gettext().tostring()); int rr = integer.parseint(etrr.gettext().tostring()); int hr = integer.parseint(ethbr.gettext().tostring()); patientdbhelper = new patientdbhelper(this); sqlitedatabase = patientdbhelper.getwritabledatabase(); patientdbhelper.addtestinformation(bl,rr,hr,sqlitedatabase); toast.maketext(getbasecontext(),"data saved",toast.length_long).show(); patientdbhelper.close(); } }
you can like
list<string> names = new arraylist<>(); cursor cursor = patientdbhelper.getinformations(sqlitedatabase); if (cursor != null){ while(cursor.movetonext()){ names.add(cursor.getstring(cursor.getcolumnindex(patientinfo.newpatientinfo.patient_fname))); } cursor.close(); }
Comments
Post a Comment