1

I have a Spinner which is filled from a query using a SimpleCursorAdapter, this works fine... but now I need to put an option "Please Select" before all the items retrieved from the query, just for usability issues... but I'm not quite sure of how to do so... HELP please...


Here is my code...

private Spinner comboForm;
...
comboForm = (Spinner) findViewById(R.id.comboFormularios);
...
mDbH.abrir();
final Cursor cu = mDbH.consultaFormularios(idU);
if(cu.moveToFirst() == false){
    cu.close();
    mDbH.cerrar();
}else{
    SimpleCursorAdapter adapter2 = new SimpleCursorAdapter(getApplicationContext(),R.layout.spinner,cu,new String[] {"nombre"},new int[] {R.id.textoCombo});
    adapter2.setDropDownViewResource(R.layout.simple_spinner_dropdown_item);
    comboForm.setAdapter(adapter2);
}
mDbH.cerrar();
...
comboForm.setOnItemSelectedListener(new OnItemSelectedListener(){
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView,int position, long id) {
        idF = (int) id;
        obtenerDatosRutas(idU,idF);
        tabla.removeAllViews();
        llenarTabla();
    }

    public void onNothingSelected(AdapterView<?> arg0) {}
});

Where mDbH is an instance of the class I'm using to manipulate the Database... as you can see the Spinner is filled up from Cursor resulting of the query consultaFormularios(idU)

4

1 回答 1

1

创建游标时,一种可能的解决方案是使用 SQL UNION 并构造第二个 SELECT,其中仅包含您需要的标签(添加硬编码的虚拟字段以进行排序)。

或者,这很可能是最简单的解决方案。不要使用光标适配器,而是使用数组适配器,并首先使用所需的默认值填充数组,然后插入光标中的所有项目。就像是,

ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("Please select");

final Cursor cu = mDbH.consultaFormularios(idU);

while(cu.moveToNext()) { 
    arrayList.add(cu.getString(0)); // assuming you want a 
                                    //string from the first column
}

ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, arrayList);
comboForm.setAdapter(spinnerArrayAdapter);
于 2013-04-23T19:36:58.473 回答