我是Android应用开发的新手。我的问题是,当我点击提交按钮时,那些选择的微调器项目不会显示在单独的editbox上。在编辑框的编辑框中只显示nullnullnull值。当我点击btnSubmit1时,仅在edirbox上显示nullnullnull值
btnSubmit1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
/*Intent intent = new Intent(context,MainActivity.class);
startActivity(intent);*/
display.setText("Facility-:"+Spinner1Value+"\n"+"Trip-:"+Spinner2Value+"\n"+"ShiftType-:"+Spinner3Value);
//display.setVisibility(View.VISIBLE);
}
});}
public void addItemsOnSpinner(){
spinner1=(Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this, R.array.facility_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
try
{
Spinner1Value= spinner1.getSelectedItem().toString();
//Toast.makeText(getApplicationContext(), Spinner1Value,Toast.LENGTH_SHORT).show();
// Spinner1Value = parent.getItemAtPosition(position).toString();
Spinner1Value=String.valueOf(spinner1.getSelectedItem().toString());
}
catch(Exception e)
{
Toast.makeText(getBaseContext(), "Error::"+e.toString(), Toast.LENGTH_LONG).show();
}
//EditText1.setText(Spinner1Value);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
try
{
// Spinner2Value=String.valueOf(spinner2.getSelectedItem().toString());
Spinner2Value= parent.getSelectedItem().toString();
//Spinner2Value = parent.getItemAtPosition(position).toString();
}
catch(Exception e)
{
Toast.makeText(getBaseContext(), "Error::"+e.toString(), Toast.LENGTH_LONG).show();
}
//EditText1.setText(Spinner1Value);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
spinner3.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
try
{
Spinner3Value= spinner3.getSelectedItem().toString();
//Spinner3Value = parent.getItemAtPosition(position).toString();
// Spinner3Value=String.valueOf(spinner3.getSelectedItem().toString());
}
catch(Exception e)
{
Toast.makeText(getBaseContext(), "Error::"+e.toString(), Toast.LENGTH_LONG).show();
}
//EditText1.setText(Spinner1Value);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
答案 0 :(得分:0)
而不是分配微调器onItemSelected的值,你尝试在btnSubmit1 onClick事件中重新分配Spinner的值吗?
public void onClick(View arg0) {
Spinner1Value = spinner1.getSelectedItem().toString();
Spinner2Value = spinner2.getSelectedItem().toString();
Spinner3Value = spinner3.getSelectedItem().toString();
display.setText("Facility-:"+Spinner1Value+"\n"+"Trip-:"+Spinner2Value+"\n"+"ShiftType-:"+Spinner3Value);
}
您可能需要一个函数来确保每个Spinner都已被选中,或者您可以事先在每个Spinner上设置默认的selectedItem。