简体   繁体   English

如何从游标填充的游标中获取值

[英]How to get values from a spinner populated from a cursor

I've populated my spinner as this: 我将旋转器填充为:

DatabaseConnector dbConnector = new DatabaseConnector(this);
    dbConnector.open();
    Cursor c = dbConnector.raw("SELECT _id, nombrecasa FROM casa");
    startManagingCursor(c);
    String[] from = new String[]{"nombrecasa"};
    int[] to = new int[]{android.R.id.text1};
    SimpleCursorAdapter adapter =
    new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to );
    adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
    casaSpinner = (Spinner) findViewById( R.id.casaspinner);
    casaSpinner.setAdapter(adapter);
    dbConnector.close();
    }

And when I try to get the value as: 当我尝试获得的价​​值为:

String idcasa   = casaSpinner.getSelectedItem().toString();

it only returns this: 它仅返回以下内容:

android.database.sqlite.SQLiteCursor@40539880

Your spinner has taken a SimpleCursorAdapter as an adapter, of which its dataset is a Cursor . 您的微调器采用了SimpleCursorAdapter作为适配器,其数据集为Cursor When you select an item on the spinner, you've only moved the cursor to a specific row and when you call getSelectedItem() you're still requesting for the dataset object - which is still a cursor object. 当您在微调器上选择一个项目时,您仅将光标移到了特定行,并且在调用getSelectedItem()您仍在请求数据集对象-仍然是光标对象。 to do what you're trying you can simply call: 做您想做的事情,您可以简单地调用:

c.getString(1);

once your selection has been made. 一旦做出选择。 For something a little more fundamentary illustrative of what i said. 对于我所说的更多基本说明。

((Cursor) casaSpinner.getSelectedItem()).getString(1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM