我的表格中有一个微调器。现在我需要传递选定的值并将其包含在我的表单请求中。填充Spinner已经有效了,我只需要获取所选值。我对Android很新,所以我非常感谢您提供的任何帮助或提示。
以下是Spinner值:
//this method will execute when we pick an item from the spinner
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//Setting the values to textviews for a selected item
tvID.setText(getName(position));
}
这是我的提交按钮功能:
btnSignup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String firstname = etFname.getText().toString();
final String lastname = etLname.getText().toString();
final String email = etEmail.getText().toString();
final String phone = etContact.getText().toString();
final String username = etUsername.getText().toString();
final String password = etPassword.getText().toString();
final String department = spinner.getSelectedItem().toString();
//GET the value of spinner and include it here
我需要微调器值,以便我可以将它包含在我的请求中:
RegisterRequest registerRequest = new RegisterRequest(firstname, lastname, email, phone, username, password, responseListener);
RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
queue.add(registerRequest);
编辑:我尝试了以下解决方案,但我收到此错误:
答案 0 :(得分:0)
试试这个,
Spinner spinner = (Spinner)findViewById(R.id.spinner);
String text = spinner.getSelectedItem().toString();
这可能有助于你
答案 1 :(得分:0)
获取微调器值使用以下代码
String text = mySpinner.getSelectedItem().toString();
答案 2 :(得分:0)
您可以将所选值保存在全局字符串中,然后将其传递给您的请求。
像这样的东西
String sel_spinner;
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//Setting the values to textviews for a selected item
tvID.setText(getName(position));
sel_spinner=getName(position);
}
然后在这里使用此字符串
btnSignup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String firstname = etFname.getText().toString();
final String lastname = etLname.getText().toString();
final String email = etEmail.getText().toString();
final String phone = etContact.getText().toString();
final String username = etUsername.getText().toString();
final String password = etPassword.getText().toString();
final String department = sel_spinner;
希望这会有所帮助:)
答案 3 :(得分:0)
如果上面的代码正如您所提到的那样工作,并且此方法正在调用
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//Setting the values to textviews for a selected item
tvID.setText(getName(position));
}
然后你也可以得到这样的价值。
btnSignup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String firstname = etFname.getText().toString();
final String lastname = etLname.getText().toString();
final String email = etEmail.getText().toString();
final String phone = etContact.getText().toString();
final String username = etUsername.getText().toString();
final String password = etPassword.getText().toString();
final String department = tvID.getText().toString();
}
}
答案 4 :(得分:0)
问题1: - 如何获取微调器的选定值
答案: - 对于获取微调器的选定项目,您必须在您的微调器项目上使用getSelectedItem()方法,您可以通过以下方式执行这些操作: -
public String getName(int position)
{
spinnerreferece.setSelection(position);
String selVal = (String) spinnerreference.getSelectedItem();
return selVal
}
问题2: - 如何传递所选项目的值: 答案2: - 我假设你想把价值传递给其他活动: -
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
String selValue = (String) getName(position);
tvID.setText(selValue);
//it se
cho=Integer.parseInt(selValue);
changescreen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (cho)
{
case 1:
Intent i=new Intent(getApplicationContext(),Secondactivity.class);
Log.d("xml", String.valueOf(i));
i.putExtra("key",selValue);
startActivity(i);
break;
}
}
通过做这两件事你可以完成你想要的任务。我希望它能帮到你。