是否有一种简单的方法可以从微调器中获取用户的选择(字符串值)?我试图避免在其上设置一个监听器并编写onItemSelected()方法,因为当用户进行选择时我不需要发生任何事情。相反,我正在尝试设置按钮单击以捕获选择并将其以捆绑形式发送到另一个活动。我的微调器被光标填充。这是我的代码,它崩溃了:
String choice = this.adapter.getItem(index).toString();
代码:
public class MainActivity extends BaseActivity
{
static final String TAG = "MainActivity";
static final String[] FROM = {CreateDB.C_CATEGORY};
static final int[] TO = {android.R.id.text1};
CreateDB dbBuilder;
SQLiteDatabase database;
Cursor cursor;
SimpleCursorAdapter adapter;
Spinner spinnerLocal;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dbBuilder = new CreateDB(this);
database = dbBuilder.getReadableDatabase();
cursor = database.query(CreateDB.TABLE, null, null, null, null, null, CreateDB.C_CATEGORY + " DESC");
startManagingCursor(cursor);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, cursor, FROM, TO);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner spinnerRemote = (Spinner) findViewById(R.id.spinner2);
spinnerRemote.setAdapter(adapter);
}
public void createReview(View view)
{
//createReview runs when a button is clicked
Spinner spinner = (Spinner)findViewById(R.id.spinner2);
int index = spinner.getSelectedItemPosition();
String choice = this.adapter.getItem(index).toString();
Intent postReview = new Intent(this,Post.class);
Bundle bundle = new Bundle();
bundle.putString("CATEGORY", choice);
postReview.putExtras(bundle);
startActivity(postReview);
}
答案 0 :(得分:1)
是否有一种简单的方法可以从微调器中获取用户的选择(字符串值)?
您没有“字符串值”。
您使用了SimpleCursorAdapter
。因此,您的“价值”是Cursor
。
您可以在getSelectedItem()
上致电Spinner
,CursorAdapter
支持的Spinner
}应该返回位于所选行上的Cursor
。从那里,通过getString()
和亲属