1

I am populating Spinner using SimplecursorAdapter. How can I add "Select station..." as a first item ?

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);
user1143989
  • 163
  • 1
  • 3
  • 18

2 Answers2

2

Maybe you can override the getCount, getItem and getItemId method from the CursorAdapter to make it thing you have one more element... Something like this (not tested thought) :

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

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

}
XGouchet
  • 10,002
  • 10
  • 48
  • 83
0

1) Create arrayList and and cursor result into it, then create array adapter and set that list to adapter:

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();
Perception
  • 79,279
  • 19
  • 185
  • 195
Pranita Patil
  • 791
  • 1
  • 9
  • 16