简体   繁体   English

从SQLite填充Android Spinner,但有两个字段?

[英]Populating an Android Spinner from an SQLite but with two fields?

I've finally managed to populate a spinner from an sqlite db after much messing about with this code however it's only using one field and I want First and Last names on the same spinner item? 经过这段代码的混乱之后,我终于设法从sqlite db中填充了一个微调器,但是它仅使用一个字段,并且我希望同一微调器项上的名字和姓氏?

The code is as follows: 代码如下:

    private void fillSpinner() {
    Cursor c = myDbHelper.FetchDrivers();
    startManagingCursor(c);

    // create an array to specify which fields we want to display
    String[] from = new String[]{"FirstName"};
    // create an array of the display item we want to bind our data to
    int[] to = new int[]{android.R.id.text1};
    // create simple cursor adapter
    SimpleCursorAdapter adapter =
      new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to );
    adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
    // get reference to our spinner
    Spinner s = (Spinner) findViewById( R.id.spr_Driver1);
    s.setAdapter(adapter);
}

Now it displays the FirstName and I've tried added "FirstName", "LastName" but it doesn't do anything different, I ideally want it to display the name in full on each spinner item. 现在,它显示了FirstName,并且我尝试添加“ FirstName”,“ LastName”,但没有任何不同,我理想地希望它在每个微调器项目上完整显示名称。 Is this even possible? 这有可能吗?

Any help would be great! 任何帮助将是巨大的!

Thanks, Chris 谢谢克里斯

For this, you need to change the android.R.layout.simple_spinner_item and android.R.layout.simple_spinner_dropdown_item , as these layout items are able to display only one item in the dropdown list and on the spinner. 为此,您需要更改android.R.layout.simple_spinner_itemandroid.R.layout.simple_spinner_dropdown_item ,因为这些布局项目只能在下拉列表和微调器上显示一项。 For your purpose, the best way is to create your own layout that has two items. 出于您的目的,最好的方法是创建具有两个项目的自己的布局。 Here is a link about how to do it. 是有关操作方法的链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM