android - how to display data from sqlite in spiner -


i want display data present in database.but not able display data in ui.i use spinner bt not work..please frnds me ort this..where mistake

spinner spr;     @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         spr=(spinner)findviewbyid(r.id.spinner1);          databasehandler db = new databasehandler(this);          /**          * crud operations          * */         // inserting contacts         log.d("insert: ", "inserting ..");         db.addcontact(new contact("ravi", "9100000000"));         db.addcontact(new contact("srinivas", "9199999999"));         db.addcontact(new contact("tommy", "9522222222"));         db.addcontact(new contact("karthik", "9533333333"));          // reading contacts         log.d("reading: ", "reading contacts..");         list<contact> contacts = db.getallcontacts();                 (contact cn : contacts) {             string log = "id: "+cn.getid()+" ,name: " + cn.getname() + " ,phone: " + cn.getphonenumber();                 // writing contacts log         log.d("name: ", log);        // arrayadapter<contact>adapter=new arrayadapter<contact>(this, android.r.layout.simple_spinner_item,cn.getname());         }          loaddata();     }     private void loaddata() {         // todo auto-generated method stub          databasehandler db=new databasehandler(getapplicationcontext());         list<contact> label=db.getallcontacts();           (contact cn : label) {              string log = "id: "+cn.getid()+" ,name: " + cn.getname() + " ,phone: " + cn.getphonenumber();                arrayadapter<contact>adapter=new arrayadapter<contact>(this, android.r.layout.simple_spinner_item,label);           spr.setadapter(adapter);          }        } }  //enter code here   //package com.example.newdemo;  public class contact {      //private variables     int _id;     string _name;     string _phone_number;      // empty constructor     public contact(){      }     // constructor     public contact(int id, string name, string _phone_number){         this._id = id;         this._name = name;         this._phone_number = _phone_number;     }      // constructor     public contact(string name, string _phone_number){         this._name = name;         this._phone_number = _phone_number;     }     // getting id     public int getid(){         return this._id;     }      // setting id     public void setid(int id){         this._id = id;     }      // getting name     public string getname(){         return this._name;     }      // setting name     public void setname(string name){         this._name = name;     }      // getting phone number     public string getphonenumber(){         return this._phone_number;     }      // setting phone number     public void setphonenumber(string phone_number){         this._phone_number = phone_number;     } } 

try code,

    spinner spr; @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      spr=(spinner)findviewbyid(r.id.spinner1);      databasehandler db = new databasehandler(this);      /**      * crud operations      * */     // inserting contacts     log.d("insert: ", "inserting ..");     db.addcontact(new contact("ravi", "9100000000"));     db.addcontact(new contact("srinivas", "9199999999"));     db.addcontact(new contact("tommy", "9522222222"));     db.addcontact(new contact("karthik", "9533333333"));      // reading contacts     log.d("reading: ", "reading contacts..");     list<contact> contacts = db.getallcontacts();             loaddata(); } private void loaddata() {     // todo auto-generated method stub      databasehandler db = new databasehandler(getapplicationcontext());     list<contact> label=db.getallcontacts();         list<string> namelist = new arraylist<string>();      (contact cn : label) {          string log = "id: "+cn.getid()+" ,name: " + cn.getname() + " ,phone: " + cn.getphonenumber();          namelist.add(cn.getname());          }      arrayadapter<string>adapter=new arrayadapter<string>(this, android.r.layout.simple_spinner_item, namelist );      spr.setadapter(adapter);   } } 

if want show name in spinner.


Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -