如何在微调器内查看数据库值?

时间:2012-09-12 17:13:40

标签: android database spinner

我有一个微调器和一个数据库,所以当我点击微调器我想显示其中的联系人的值(名称),但是在一个简单的代码中。因此,如果您可以指定并且只是代码更好,那么它们是spinner在(Novamensagem.java novamensagem.xml)和contacs数据库(adicionarcontato.java adicionarcontato.xml)中的sevasaretated javas和xml布局,谢谢

    final TextView spinnerContato = (TextView) findViewById(R.id.spinner);
    String[] campos = new String[] {"nome", "telefone"};

    Cursor c = db.query("contatos", campos, null, null, null, null, null);
    c.moveToFirst();
    String lista = "";
    if(c.getCount() > 0) {
        while(true) {
            lista = lista + c.getString(c.getColumnIndex("nome")).toString() + "";
            if(!c.moveToNext()) break;
        }

        spinnerContato.setText(lista);
    }

这就是代码,但它给出了错误(在评论中有更多解释) //

整个代码:

ArrayList<String>() list = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.novamensagem);

    db = openOrCreateDatabase("banco.db", Context.MODE_WORLD_WRITEABLE, null);

     SalvaMensagem();

    //Data e Hora
    setCurrentDateOnView();
    addListenerOnButton();
    setCurrentTimeOnView();
    addListenerOnButton2();

    //Spinner
    DadosSpinner();





}





private void DadosSpinner() {
    // TODO Auto-generated method stub

    final TextView spinnerContato = (TextView) findViewById(R.id.spinner);
    String[] campos = new String[] {"nome", "telefone"};

    list = new ArrayList<String>();
    Cursor c = db.query("contatos", campos, null, null, null, null, null);
    c.moveToFirst();
    String lista = "";
    if(c.getCount() > 0) {
        while(true) {
           list.add(c.getString(c.getColumnIndex("nome")).toString());
            if(!c.moveToNext()) break;
        }
    }

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, list);


    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);


    spinner.setAdapter(adapter);        
    }`

有。

2 个答案:

答案 0 :(得分:1)

要从SQLite数据库加载微调器数据,您必须:

  1. 从数据库中读取联系人并将其保存到列表中(例如)
  2. 为微调器创建适配器
  3. 方法如下所示:

     private void loadSpinnerData() 
     {
        // database handler
        DatabaseHandler db = new DatabaseHandler(getApplicationContext());
    
        // Spinner Drop down elements
        List<String> contacts = db.getAllContacts();
    
        // Creating adapter for spinner
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, contacts);
    
        // Drop down layout style - list view with radio button
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    
        // attaching data adapter to spinner
        spinner.setAdapter(dataAdapter);
     }
    

    getAllContacts()方法将返回所有联系人:

    public List<String> getAllConatcts(){
        List<String> contacts = new ArrayList<String>();
    
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS;
    
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
    
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                contacts.add(cursor.getString(1));
            } while (cursor.moveToNext());
        }
    
        // closing connection
        cursor.close();
        db.close();
    
        // returning contatcs
        return contacts;
    }
    

    有关详细信息,请查看本教程:http://www.androidhive.info/2012/06/android-populating-spinner-data-from-sqlite-database/

答案 1 :(得分:1)

检查

list = new ArrayList<String>();
Cursor c = db.query("contatos", campos, null, null, null, null, null);
c.moveToFirst();
String lista = "";
if(c.getCount() > 0) {
    while(true) {
       list.add(c.getString(c.getColumnIndex("nome")).toString());
        if(!c.moveToNext()) break;
    }


}   

这有助于您获取项目的数组列表。

接下来做这个

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_spinner_item, list);


adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);


spinner.setAdapter(adapter);

如果您有任何问题,请回来