Android - Spinner的数据消失了

时间:2011-04-09 20:20:04

标签: android sqlite cursor spinner

我有一个Android活动,显示一个填充了数据库中数据的微调器。 onCreate()列在这里:

@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时,会按预期显示一个对话框,但是微调器会占用其数据。如果我删除了cc.close();线,问题就消失了。显然,当我完成它时,我想关闭光标。我无法理解这一点 - 它是为光标提供数据的vehicleDataCursor,而不是cc。

为什么我的Spinner失去了数据?

1 个答案:

答案 0 :(得分:1)

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

据我所知,当你这样做时,你只需要引用与Spinner关联的Cursor。换句话说,引用vehicleDataCursor

当您关闭cc时,您实际上正在关闭vehicleDataCursor

这有意义吗?