Android - 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 会丢失数据吗?
I have an Android activity which displays a spinner populated with data from a database. The onCreate() is listed here:
@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();
}
}
});}
It works fine but has a minor problem - when the user fills in the form and clicks the fuelEntryOkButton, a dialog is display as intended but the spinner is empited of its data. If I removed the cc.close(); line, the problem goes away. Obviously I'd like to close the cursor when I've finished with it. I can't understand this - it is vehicleDataCursor which is providing data for the cursor, not cc.
Any ideas why my Spinner loses its data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我了解,当您执行此操作时,您只是获得对与微调器关联的光标的引用。换句话说,对
vehicleDataCursor
的引用。当您关闭
cc
时,您实际上就关闭了vehicleDataCursor
。这有道理吗?
As far as I understand it, when you do this you are simply getting a reference to the Cursor associated with the Spinner. In other words, a reference to
vehicleDataCursor
.When you close
cc
, you're effectively closingvehicleDataCursor
.Does that make sense?