如何在spinner中显示sqlite中的数据

时间:2015-04-25 11:57:37

标签: android sqlite

我想显示数据库中存在的数据。但是我无法在ui中显示这些数据。我使用微调器,但它不起作用..如何排序这个......我的错误在哪里?

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);
         }
      }
}


//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;
    }
}

2 个答案:

答案 0 :(得分:0)

试试这段代码,

angular
  .module('dashboard', [
    'authentication'
  ])
  .run(run);

如果你想在微调器中显示名字。

答案 1 :(得分:0)

就您所知,它也可以在不使用String类型的ArrayList的情况下工作。

为此,您只需要在 Contact 类中重写toString方法并在那里返回联系人姓名

例如,在Contact类中添加以下方法

@Override
    public String toString() {
        return _name;
    }

loadData ()方法中没有任何更改。