在我的应用程序中,我有一个微调器显示了一些消费期(每周,每两周,每月等),我用SimpleCursorAdapter
填充它,因为这些期间保存在数据库中。以下是我实施它的方法:
Cursor spendingPeriodCursor = dataSource.getSpendingPeriods();
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, spendingPeriodCursor, fromColumns, toColumns);
mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpendingSpinner.setAdapter(mAdapter);
微调器填充,在活动结束时,我尝试获取所选项目:
// Get spending period description:
String spendingDesc = mSpendingSpinner.getSelectedItem().toString();
但它不会返回实际的字符串值。它看起来像是获取光标部分的字符串表示:
android.database.sqlite.SQLiteCursor@410dfae8
如何获取所选项目的实际文本?
答案 0 :(得分:3)
Cursor cursor = (Cursor) mSpendingSpinner.getSelectedItem();
String text = cursor.getString(cursor.getColumnIndex("my_column_name"));
答案 1 :(得分:0)
String spendingDesc = mSpendingSpinner.getSelectedItem().toString();
mSpendingSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View arg1,
int pos, long arg3) {
String spendingDesc= parent.getSelectedItem().toString();
}
}
Cursor cursor = (Cursor) mSpendingSpinner.getSelectedItem();
String text = cursor.getString(cursor.getColumnIndex("COLUN_NAME"));