2012-03-14 36 views
1

我使用SimplecursorAdapter填充Spinner。如何添加「選擇站...」作爲第一項?Android將「Select station ...」添加爲Spinner的第一項

SimpleCursorAdapter adapter = new SimpleCursorAdapter (this, R.layout.stnacspinner_item, cspinner, new String[] {"_id"}, new int[] {R.id.stnacspinnertext}); 
     adapter.setDropDownViewResource(R.layout.stnacspinner_item); 
     sp.setAdapter(adapter); 

回答

2

也許你可以從CursorAdapter的覆蓋getCount將,和的getItem方法getItemId使它的事情你有一個更多元......像這樣的東西(未測試的思想):

int getCount(){ 
    return super.getCount() + 1; 
} 

Object getItem(int position) { 
    if (position == 0) { 
     return "Select Station"; 
    } else { 
     return super.getItem(position - 1); 
    } 

} 
+0

我正在嘗試。 – user1143989 2012-03-14 16:51:10

0

1)創建ArrayList和和光標導致了進去,然後創建陣列適配器,並設置該列表適配器:

as.add(0, "select insurance"); 

Cursor cursorInsurance = this.dbH1.getReadableDatabase().query(
    "tablename", 
    new String[]{"column field..."}, 
    null, 
    null, 
    null, 
    null, 
    null); 

if(cursorInsurance!=null) { 
    if(cursorInsurance.moveToFirst()) { 
     do { 
     as.add(cursorInsurance.getString(2)); 
     } while(cursorInsurance.moveToNext()); 
    } 
} 

ArrayAdapter<String> adpInsurance=new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item,as); 

adpInsurance.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
spInsurance.setAdapter(adpInsurance); 
spInsurance.setSelection(0); 

adpInsurance.notifyDataSetChanged(); 
相關問題