Android - Spinner 的数据消失

发布于 2024-10-31 18:41:37 字数 1877 浏览 6 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

御弟哥哥 2024-11-07 18:41:37
Cursor cc = (Cursor)(vehicleSpinner.getSelectedItem());

据我了解,当您执行此操作时,您只是获得对与微调器关联的光标的引用。换句话说,对 vehicleDataCursor 的引用。

当您关闭cc时,您实际上就关闭了vehicleDataCursor

这有道理吗?

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

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 closing vehicleDataCursor.

Does that make sense?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文