2010-01-16 109 views
5

我正在從spinner.getSelectedItem()。toString()調用返回文本'[email protected]'。我不知道爲什麼。微調器綁定到SimpleCursorAdapter。[email protected]

下面是代碼

cCategories = (Cursor) myAdapter.getAllCategories(); 
    this.startManagingCursor(cCategories); 

    SimpleCursorAdapter scaCategories = new SimpleCursorAdapter(this, R.layout.track_category_item,cCategories,new String[] {DBAdapter.KEY_CATEGORIES_NAME},new int[]{R.id.text1}); 
    scaCategories.setDropDownViewResource(R.layout.track_category_dropdown_item); 
    mCatSpinner = (Spinner) findViewById(R.id.thecategory); 
    mCatSpinner.setAdapter(scaCategories); 

    if(mCatSpinner.isSelected() != true) { 
     mCatSpinner.setSelection(0); 
    } 

和XML track_category_item.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/text1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@android:id/text1" 
    style="?android:attr/spinnerItemStyle" 
    android:ellipsize="marquee" 
    android:singleLine="true"> 
</TextView> 

track_category_dropdown_item.xml

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"   
    android:id="@+id/text1" 
    style="?android:attr/spinnerDropDownItemStyle" 
    android:singleLine="true" 
    android:layout_width="fill_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:ellipsize="marquee" /> 

的微調XML看起來像這樣

<Spinner 
    android:id="@+id/thecategory" 
    android:prompt="@string/SELECT_CATEGORY" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_x="15px" 
    android:layout_y="133px" > 
</Spinner> 

並恢復光標

public Cursor getAllCategories() 
{ 
    return db.query(DATABASE_CATEGORIES_TABLE, new String[] { 
      KEY_CATEGORIES_ROWID, 
      KEY_CATEGORIES_NAME, 
      KEY_CATEGORIES_DEFAULT}, 
      null, 
      null, 
      null, 
      null, 
      null); 
} 

的微調似乎正常工作。當我嘗試保存時,這是使用spinner.getSelectedItem()。toString()作爲選定項目的值傳遞的內容。

任何人都可以在這裏看到任何錯誤的東西。不知道該怎麼辦。

感謝 帕特里克

+0

這對於ArrayAdapter來說是正確的。然後傳遞的值是類別的文本(Business或Personal)。我使用SimpleCursorAdapter的事實是否改變了值的傳遞方式? – bugzy 2010-01-16 01:40:18

回答

6

你的代碼工作,你寫的。 SpinnerAdapterView。您連接到的適配器是SimpleCursorAdapter。這意味着所選項目是Cursor(位於遊標結果集中與用戶選擇對應的項目中)。 Cursor的默認實現爲toString(),它返回類似[email protected]的內容。

由於您沒有告訴我們您要做什麼,因此不可能爲您進一步準確地提供建議。但是,無論您想要保存的是什麼,都需要從中取出,您從getSelectedItem()中獲得。

+0

謝謝commonsware,我想你可能已經回答了我的問題。我只是試圖獲取所選項目的值,並將其保存到數據庫表中。最初我使用ArrayAdapter來填充微調器。該數組是一個具有幾個類別(「業務」,「個人」)的單元素數組。這工作作爲一個字符串已通過。 所以你說我需要做一些額外的代碼來根據傳入的spinner.getSelectedItemPosition()來從遊標中找到實際的類別名? 謝謝 patrick – bugzy 2010-01-16 02:19:07

+0

是的。鑑於你的query(),你需要在'Cursor'上調用'getString(1)'來取回第二列......假設'KEY_CATEGORIES_NAME'是你所尋求的值。 – CommonsWare 2010-01-16 12:38:06

1

我可能會困擾讀你的背景,但只是想簡單的幫助。 我有一個以DbHelper.KEY_COL命名的列,我正在檢索特定行的DbHelper.KEY_COL值。 也許我的一些代碼會有所幫助:

Cursor colCur=(Cursor)spCols.getSelectedItem(); 
String col=colCur.getString(colCur.getColumnIndex(DbHelper.KEY_COL)); 
相關問題