database - Why not set the value of Spinner Android. i use Cursor? -
i have table in database called primarydata includes fields primarydataid , primarydatacode .my problem is,when cursor returns data database ,spinner not set values of cursor.
xml spinner file
<?xml version="1.0" encoding="utf-8"?> <textview xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:textsize="14sp" android:textcolor= "#000000" android:spinnermode="dialog" />
primarydata class in put primarydataid , primarydatacode. class equivalent of database table.
package com.example.proposaldashborad; public class primerydata { private int primerydataid; private string primerydatacode; @override public string tostring() { return this.primerydatacode; // display in spinner list. } public int getprimarydataid() { return primerydataid; } public void setprimarydataid(int value) { this.primerydataid = value; } public int getprimarydatacode() { return primerydataid; } public void setprimarydatacode(string value) { this.primerydatacode = value; } }
this code fill spinner
private void getprimarydataforspinner(string primarydataname, cursor cr) { cr.movetofirst(); //array_spinner = new string[cr.getcount()]; arraylist<primerydata> pds = new arraylist<primerydata>() ; { //array_spinner[i] = cr.getstring(cr.getcolumnindex("primerydatacode")); primerydata pd = new primerydata() ; pd.setprimarydataid(cr.getint(cr.getcolumnindex("primerydataid"))); pd.setprimarydatacode(cr.getstring(cr.getcolumnindex("primerydatacode"))); pds.add(pd) ; } while (cr.movetonext()); cr.close(); if (primarydataname == "admin") { adapteradmin = new arrayadapter<primerydata>(this,r.layout.spinner, pds); sadmin = (spinner) findviewbyid(r.id.spinneradmin); sadmin.setadapter(adapteradmin); } }
and code when data returned database.
private spinner sadmin; private arrayadapter<primerydata> adapteradmin; db = new dbadapter(getapplicationcontext()); db.open(); cr = db.getviewmt26(entityid); cr.movetofirst(); if(cr.getcount()>0) { primerydata pd = new primerydata(); pd.setprimarydataid(cr.getint(cr.getcolumnindex("adminid"))); pd.setprimarydatacode(cr.getstring(cr.getcolumnindex("admin"))); sadmin.setselection(adapteradmin.getposition(pd)); } cr.close(); db.close();
my main question why
adapteradmin.getposition (pd));
returns -1?
you have initialize adapteradmin, , set spinner setadapter method. furthermore, if using list of primerydata, text displayed on each spinner item primerydata (instance) tostring().
Comments
Post a Comment