无法从微调器中获取选定的项目文本

时间:2014-11-27 19:33:07

标签: android sqlite cursor spinner simplecursoradapter

在我的应用程序中,我有一个微调器显示了一些消费期(每周,每两周,每月等),我用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

如何获取所选项目的实际文本?

2 个答案:

答案 0 :(得分:3)

Cursor cursor = (Cursor) mSpendingSpinner.getSelectedItem();    
String text = cursor.getString(cursor.getColumnIndex("my_column_name"));

答案 1 :(得分:0)

1)嗯,用的就足够了:

String spendingDesc = mSpendingSpinner.getSelectedItem().toString();

2)但你也可以尝试:

mSpendingSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> parent, View arg1,
                int pos, long arg3) {
                String spendingDesc= parent.getSelectedItem().toString();
        }
 }

3)或

Cursor cursor = (Cursor) mSpendingSpinner.getSelectedItem();
String text = cursor.getString(cursor.getColumnIndex("COLUN_NAME"));