placeSpinner.getSelectedItem().toString();
我正在使用这行代码从我的spinner
中获取文本,并将其传递给另一个Java文件。但是每当我这样做时,应用程序就会强制关闭。
我该如何解决这个错误?
非常感谢。
placeSpinner.getSelectedItem().toString();
我正在使用这行代码从我的spinner
中获取文本,并将其传递给另一个Java文件。但是每当我这样做时,应用程序就会强制关闭。
我该如何解决这个错误?
非常感谢。
final String text = ((YourAdapter) spinner.getAdapter()).getItem(spinner.getSelectedItemPosition());
希望对你有所帮助。
试试这段代码...
在这里,我将所选下拉菜单的索引和值存储到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", "");
尝试这段代码:将适配器设置为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
}
});
抱歉,我发布错了。请尝试这个。
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("");
}
});
这应该可以工作:
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long id) {
String value = (TextView)view.getText().toString();
}
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()