samedi 25 avril 2015

how to display data from sqlite in spiner

I want to display my data which is present in database.but i could not able be display this data in ui.i use the spinner bt it does not work..please frnds help me to ort this..where is the my 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 all contacts
    Log.d("Reading: ", "Reading all contacts..");
    List<Contact> contacts = db.getAllContacts();       

    for (Contact cn : contacts) {
        String log = "Id: "+cn.getID()+" ,Name: " + cn.getName() + " ,Phone: " + cn.getPhoneNumber();
            // Writing Contacts to 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();

     for (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;
}

}

Aucun commentaire:

Enregistrer un commentaire