Populating an Android Spinner from an SQLite but with two fields?

ε祈祈猫儿з 提交于 2020-01-05 05:30:07

问题


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?

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. Is this even possible?

Any help would be great!

Thanks, Chris


回答1:


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. 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.



来源:https://stackoverflow.com/questions/10639306/populating-an-android-spinner-from-an-sqlite-but-with-two-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!