2011-04-09 54 views
1

我有一個Android活動,顯示一個填充了數據庫數據的微調器。該的onCreate()是列在這裏:Android - Spinner的數據消失

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
super.onCreate(savedInstanceState); 
setContentView(R.layout.fuel_entry); 

    vehicleDataCursor = VehicleDataProvider.getVehicles(); 
    startManagingCursor(vehicleDataCursor); 

    String[] from = new String[]{REGISTRATION_NO_COLUMN, DESCRIPTION_COLUMN}; 
    int[] to = new int[]{R.id.vehicle_db_row_registration_number, R.id.vehicle_db_row_description}; 

    vehicleAdapter = 
     new SimpleCursorAdapter(this, R.layout.vehicle_spinner_row, vehicleDataCursor, from, to); 

    vehicleSpinner = (Spinner) findViewById(R.id.spinner); 
    vehicleSpinner.setAdapter(vehicleAdapter); 

mileageEntry = (EditText) findViewById(R.id.fuel_entry_mileage_edit_text); 

fuelAmountEntry = (EditText) findViewById(R.id.fuel_entry_fuel_edit_text); 

fuelEntryOkButton = (Button) findViewById(R.id.fuel_entry_ok_button); 
fuelEntryOkButton.setOnClickListener(new OnClickListener() 
{ 
@Override 
public void onClick(View v) 
{ 
    String registrationNumber = ""; 
    Cursor cc = (Cursor)(vehicleSpinner.getSelectedItem()); 
    if (cc != null) 
    { 
    registrationNumber = cc.getString(cc.getColumnIndexOrThrow(REGISTRATION_NO_COLUMN)); 
     if(FuelUseDataProvider.addFuelUp(registrationNumber, Integer.parseInt(mileageEntry.getText().toString()), Integer.parseInt(fuelAmountEntry.getText().toString()), System.currentTimeMillis()) != DATABASE_INSERT_ERROR_CODE) 
      { 
      showMPGDialog(registrationNumber); 
      } 
      else 
      { 
      //do error handling 
      } 
      cc.close(); 
     } 
} 
});} 

它工作正常,但有一個小問題 - 當用戶在填寫表單和點擊fuelEntryOkButton,對話是如預期顯示,但微調是empited其數據。如果我刪除了cc.close();行,問題消失。很顯然,當我完成它時,我想關閉光標。我無法理解這一點 - 它是爲遊標提供數據的vehicleDataCursor,而不是cc。

任何想法爲什麼我的Spinner丟失其數據?

回答

1
Cursor cc = (Cursor)(vehicleSpinner.getSelectedItem()); 

據我瞭解,當你這樣做時,你只是得到一個引用與光標相關聯的光標。換句話說,對vehicleDataCursor的引用。

當您關閉cc時,您正在關閉vehicleDataCursor

這有道理嗎?

+0

完美的感覺。它的工作原理 - 我可以使用vehicleDataSpinner獲取我需要的數據。 cc不是必需的。巴里 – barry 2011-04-10 07:17:39