1

我有一个微调器和一个数据库,所以当我单击微调器时,我想在其中显示联系人的值(名称),但使用简单的代码。所以它们是分离的 javas 和 xml 布局,微调器位于(Novamensagem.java novamensagem.xml)中,而 contacs 数据库位于(Adicionarcontato.java adicionarcontato.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);        
    }`

有。

4

2 回答 2

1

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

  1. 从数据库中读取联系人并将其保存到列表中(例如)
  2. 为微调器创建一个适配器

方法如下所示:

 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/

于 2012-09-12T17:41:10.447 回答
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;
    }


}   

这可以帮助您获取项目的arrayList。

接下来做这个

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

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

于 2012-09-12T18:08:47.210 回答