select 如何在 Android 中从 ActionBar 上的 Navigation Spinner 发送文本?

How to select text from Navigation Spinner on ActionBar in Android?

我 Activity 的 ActionBar 上有 Navigation Spinner。它由来自 SQLite Database 使用 SimpleCursorAdapter 的数据填充。 我使用以下代码完成了此操作

String query="select rowid _id, vehicleName from vehiclesTable";
    try{
        Cursor c1=db.rawQuery(query,null);
        if (c1.getCount()>0){
         while (c1.moveToNext()){
             adapter=new SimpleCursorAdapter(this,android.R.layout.simple_list_item_1, c1,new String[]{"vehicleName"},new int[]{android.R.id.text1});
         }
        }
    }catch (SQLiteException e){
        Toast.makeText(MainPage.this,e.toString(),Toast.LENGTH_SHORT).show();
    }

    actionBar=getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    actionBar.setListNavigationCallbacks(adapter,this);

现在我必须获取所选项目的文本并将其显示在 Toast 中。我不知道该怎么做。有知道的帮帮我吧

您可以像这样获取所选项目的文本。

String selectedItemText = ((TextView) view.findViewById(R.id.text1)).getText().toString();

然后在 toast 中显示 selectedItemText。

希望对您有所帮助!

为了回答你的问题,而不是在评论中胡言乱语;您应该从当前设置为 Spinner 的相同位置读取 Cursor 中的数据。

类似

public boolean onNavigationItemSelected(int position, int id) {
    Cursor c = adapter.getCursor();
    c.moveToPosition(position);
    String value = c.getString(c.getColumnIndex("vehicleName"));
    // show toast
    return true;
}