如何让微调select项目值的string?

我有5个纺纱厂。 为了对此做出总结。

这是微软的微调

<Spinner android:id="@+id/text_interested" android:layout_span="2" android:layout_width="wrap_content" android:layout_height="60px" android:entries="@array/interestedarrays" android:prompt="@string/interestedprompt" /> 

这是Java中的Spinner

 submitbtn.setOnClickListener(new OnClickListener() { public void onClick(View v) { interested.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { public void onItemSelected( AdapterView<?> adapterView, View view, int i, long l) { interesting = interested.getItemAtPosition(i).toString(); } public void onNothingSelected( AdapterView<?> adapterView) { } }); } }); 

这里解释:

该页面有一个button。 按下此button时,将从微调器读取数据。 我用这个检查了输出

 System.out.println(interested.getItemAtPosition(i).toString()); 

它给了我什么都没有,甚至没有空。

如何检索值和string?

尝试这个:

 String text = mySpinner.getSelectedItem().toString(); 

像这样,你可以得到不同的纺纱厂的价值。

 String Text = mySpinner.getSelectedItem().toString(); 

要么

 mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { Object item = parent.getItemAtPosition(position); } public void onNothingSelected(AdapterView<?> parent) { } }); 

您可以使用,从微调获取选定的项目,

 interested.getSelectedItem().toString(); 

尝试这个

  final Spinner cardStatusSpinner1 = (Spinner) findViewById(R.id.text_interested); String cardStatusString; cardStatusSpinner1.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { cardStatusString = parent.getItemAtPosition(pos).toString(); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); final Button saveBtn = (Button) findViewById(R.id.save_button); saveBtn .setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub System.out.println("Selected cardStatusString : " + cardStatusString ); //this will print the result } }); 

如果您的Spinner被SQLite光标填充,那么解决scheme是:

 Spinner mySpin = (Spinner) findViewById(R.id.myspin); mySpin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { SQLiteCursor item = (SQLiteCursor) parent.getItemAtPosition(position); String value = String.valueOf(item.getString(0)); Toast.makeText(getApplicationContext(), "The option is:" + value , Toast.LENGTH_SHORT).show(); } 

PS:在item.getString(0) – > 0是你想要得到的游标的列索引。

除了build议之外,

 String Text = mySpinner.getSelectedItem().toString(); 

你可以做,

 String Text = String.valueOf(mySpinner.getSelectedItem()); 

当你从微调select任何值,然后你得到选定的价值,

 interested.getSelectedItem().toString(); 

我想你想单击button时,微调器的选定的项目..

尝试getSelectedItem() :

 spinner.getSelectedItem() 

通过为适配器对象实现SpinnerAdapter,我使用interested.getItem(i).toString()