placeSpinner.getSelectedItem().toString();
所以我正在使用这些编码行从我的spinner
获取文本并将其传递给另一个java文件。但是每当我这样做时,应用程序一直给我一个力量。
如何解决此错误?
非常感谢你。
答案 0 :(得分:0)
我希望它对你有所帮助。
试试此代码......
这里我将所选微调器的索引和值存储到SharedPreferences
中private SharedPreferences prefs;
private String prefName = "spinn";
final List<String> list=new ArrayList<String>();
list.add("Item 1");
list.add("Item 2");
list.add("Item 3");
list.add("Item 4");
list.add("Item 5");
final Spinner sp1= (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adp1=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,list);
adp1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(adp1);
sp1.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long id) {
// TODO Auto-generated method stub
int pos1=position;
String str1=sp1.getSelectedItem().toString();
prefs = getSharedPreferences(prefName, MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
//---save the values in the EditText view to preferences---
editor.putInt("index", pos1);
editor.putString("value", str1);
//---saves the values---
editor.commit();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
要将该值转换为另一个java文件,在该java文件中,在需要它的地方编写代码。
prefs = getSharedPreferences(prefName, MODE_PRIVATE);
int pos=prefs.getInt("index", (Integer) null);
String value=prefs.getString("value", "");
答案 1 :(得分:0)
试用此代码:将适配器设置为spinnerName
:
spinnerName=(Spinner)findViewById(R.id.spinnerName);
String []data={"item1","item2","item3","item4","item5"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, data);
spinnerName.setAdapter(adapter);
现在将setOnItemSelectedListener
设置为spinnerName
。
spinnerName.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), spinnerName.getSelectedItem().toString(), Toast.LENGTH_LONG).show();
Intent intent=new Intent(FirstActivity.this,SecondActivity.class);
// pass value of selected spinnerItem to other Activity.
intent.putExtra("spinnerItem", spinnerName.getSelectedItem().toString());
startActivity(intent);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
答案 2 :(得分:0)
抱歉,我发错了。 请试试这个。
String []data={"item1","item2","item3","item4","item5"};
spinner.setAdapter(adaptador);
spinner.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent,
android.view.View v, int position, long id) {
mData.setText("Selected: " + data[position]);
}
public void onNothingSelected(AdapterView<?> parent) {
mData.setText("");
}
});
答案 3 :(得分:0)
使用此:
final String text = ((YourAdapter) spinner.getAdapter()).getItem(spinner.getSelectedItemPosition());
答案 4 :(得分:0)
这应该有效:
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long id) {
String value = (TextView)view.getText().toString();
}
答案 5 :(得分:-1)
final Equipamento[] eqptos = eqptoDao.list();
List<String> labels = new ArrayList<String>();
for(Equipamento eqpto : eqptos)
{
labels.add(eqpto.getDescricao());
}
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,labels);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
//to get value of spinner
eqptos[Integer.parseInt("" + spinner.getSelectedItemId())].getEquipamento()