Android spinner实现未显示所选项目
我有3个微调器,我从每个微调器中选择值。但是,当我在next按钮的onClickListener()之外声明setOnItemSelected()方法时,所选值不会显示在Toast中。当我在按钮的onClickListener中声明setOnItemSelected()方法时,它可以工作,但是当我从最后一个微调器中选择“Set Limit”时,我无法隐藏我的edittext。 请帮忙。 下面是我的.java文件Android spinner实现未显示所选项目,android,spinner,Android,Spinner,我有3个微调器,我从每个微调器中选择值。但是,当我在next按钮的onClickListener()之外声明setOnItemSelected()方法时,所选值不会显示在Toast中。当我在按钮的onClickListener中声明setOnItemSelected()方法时,它可以工作,但是当我从最后一个微调器中选择“Set Limit”时,我无法隐藏我的edittext。 请帮忙。 下面是我的.java文件 public class AvailabilityActivity extends
public class AvailabilityActivity extends AppCompatActivity {
private Button next;
private Spinner advanceNotice, shortestTrip, longestDist;
private Bundle bundle;
private EditText setLimit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_availability);
bundle = getIntent().getExtras();
intializeSpinners();
setLimit = (EditText) findViewById(R.id.setlimit);
ArrayAdapter<CharSequence> adapteradvNotice = ArrayAdapter.createFromResource(this, R.array.advNoticeArray, R.layout.spinner_layout);
adapteradvNotice.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
advanceNotice.setAdapter(adapteradvNotice);
ArrayAdapter<CharSequence> adaptershortestTrip = ArrayAdapter.createFromResource(this, R.array.shortestTripArray, R.layout.spinner_layout);
adaptershortestTrip.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
shortestTrip.setAdapter(adaptershortestTrip);
ArrayAdapter<CharSequence> adapterlongestDist = ArrayAdapter.createFromResource(this, R.array.longestDistanceArray, R.layout.spinner_layout);
adapterlongestDist.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
longestDist.setAdapter(adapterlongestDist);
onNextPressed();
}
private void intializeSpinners() {
advanceNotice = (Spinner) findViewById(R.id.acceptAdvanceNotice);
shortestTrip = (Spinner) findViewById(R.id.acceptShortestTrip);
longestDist = (Spinner) findViewById(R.id.acceptLongestTrip);
}
private void onNextPressed() {
next = (Button) findViewById(R.id.nextButton1);
final String[] setLimitText = {""};
final String[] selectedlongestDist = new String[1];
longestDist.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedlongestDist[0] = parent.getItemAtPosition(position).toString();
if (selectedlongestDist[0].equals("Set Limit")){
setLimit.setVisibility(View.VISIBLE);
setLimitText[0] = setLimit.getText().toString();
}
if (selectedlongestDist[0].equals("No Limit")) {
setLimit.setVisibility(View.INVISIBLE);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
if (setLimitText[0] == "")
setLimitText[0] = selectedlongestDist[0];
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String[] selectedNotice = new String[1];
advanceNotice.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedNotice[0] = parent.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
final String[] selectedshortestTrip = new String[1];
shortestTrip.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedshortestTrip[0] = parent.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Toast.makeText(getApplicationContext(), setLimitText[0], Toast.LENGTH_LONG).show();
bundle.putString("advancenotice", selectedNotice[0]);
bundle.putString("shortesttrip", selectedshortestTrip[0]);
bundle.putString("longesttrip", setLimitText[0]);
Intent intent = new Intent(getBaseContext(),ImageActivity.class);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
}
公共类可用性活动扩展了AppCompativeActivity{
私人按钮下一步;
私人纺纱机先进,行程最短,距离最远;
私有包;
私人编辑文本设置限制;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u可用性);
bundle=getIntent().getExtras();
初始化espinners();
setLimit=(EditText)findViewById(R.id.setLimit);
ArrayAdapterAdvNotice=ArrayAdapter.createFromResource(this,R.array.advNoticeArray,R.layout.spinner\u layout);
adapteradvNotice.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
advanceNotice.setAdapter(适配器advanceNotice);
ArrayAdapter AdapterSortestTrip=ArrayAdapter.createFromResource(this,R.array.shortestTripArray,R.layout.spinner\u布局);
AdapterSortestTrip.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
最短行程。设置适配器(适配器最短行程);
ArrayAdapterLongestDist=ArrayAdapter.createFromResource(this,R.array.longestDistanceArray,R.layout.spinner\u布局);
adapterlongestDist.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
最长距离设置适配器(适配器最长距离);
onNextPressed();
}
私有void initializeSpinners(){
advanceNotice=(微调器)findViewById(R.id.acceptAdvanceNotice);
shortestTrip=(微调器)findViewById(R.id.acceptShortestTrip);
longestDist=(微调器)findViewById(R.id.acceptLongestTrip);
}
私有void onNextPressed(){
next=(按钮)findViewById(R.id.nextButton1);
最后一个字符串[]setLimitText={”“};
最终字符串[]selectedlongestDist=新字符串[1];
longestDist.setOnItemSelectedListener(新微调器.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
selectedlongestDist[0]=parent.getItemAtPosition(position.toString();
如果(selectedlongestDist[0]。等于(“设置限制”)){
setLimit.setVisibility(View.VISIBLE);
setLimitText[0]=setLimit.getText().toString();
}
如果(selectedlongestDist[0]。等于(“无限制”)){
setLimit.setVisibility(视图不可见);
}
}
@凌驾
未选择公共无效(AdapterView父级){
}
});
如果(setLimitText[0]=“”)
setLimitText[0]=selectedlongestDist[0];
next.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
最终字符串[]selectedNotice=新字符串[1];
advanceNotice.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
selectedNotice[0]=parent.getItemAtPosition(position.toString();
}
@凌驾
未选择公共无效(AdapterView父级){
}
});
最终字符串[]selectedshortestTrip=新字符串[1];
shortestTrip.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
selectedshortestTrip[0]=parent.getItemAtPosition(position.toString();
}
@凌驾
未选择公共无效(AdapterView父级){
}
});
Toast.makeText(getApplicationContext(),setLimitText[0],Toast.LENGTH_LONG.show();
bundle.putString(“advancenotice”,selectedNotice[0]);
bundle.putString(“shortesttrip”,selectedshortestTrip[0]);
bundle.putString(“longestrip”,setLimitText[0]);
Intent Intent=new Intent(getBaseContext(),ImageActivity.class);
意向。额外支出(捆绑);
星触觉(意向);
}
});
}
}
下面是我的布局文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.csci567.dailyrentals.AvailabilityActivity">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="How much advance notice do you need to confirm a trip request?"
android:textColor="#000000"
android:textSize="18dp"
android:layout_marginLeft="15dp"
android:id="@+id/advanceNoticeRequestText"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.035"
android:layout_marginStart="15dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Advance notice"
android:textSize="14dp"
android:id="@+id/advanceNoticeText"
android:layout_marginLeft="15dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.13"
android:layout_marginStart="15dp" />
<Spinner
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/acceptAdvanceNotice"
android:hint="Advance Notice"
style="@style/Widget.AppCompat.Spinner.Underlined"
android:layout_marginLeft="15dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.18"
android:layout_marginStart="15dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Block trips that don't give you enough advance notice."
android:textSize="16dp"
android:id="@+id/blockNoticeText"
android:layout_marginLeft="15dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.26"
android:layout_marginStart="15dp" />
<View android:background="#a8a8a6"
android:layout_width = "0dp"
android:layout_height="1dp"
android:id="@+id/separatorLine1"
android:layout_marginTop="25dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.3"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="How long would you like trips to last?"
android:textSize="18dp"
android:textColor="#000000"
android:id="@+id/tripDurationText"
android:layout_marginLeft="15dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.4"
android:layout_marginStart="15dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Shortest possible trip"
android:textSize="14dp"
android:id="@+id/shortestTripText"
android:layout_marginLeft="15dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.46"
android:layout_marginStart="15dp" />
<Spinner
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/acceptShortestTrip"
android:hint="Enter Shortest Trip"
style="@style/Widget.AppCompat.Spinner.Underlined"
android:layout_marginLeft="15dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.52"
android:layout_marginStart="15dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Longest Possible trip"
android:textSize="14dp"
android:id="@+id/longestTripText"
android:layout_marginLeft="15dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.6"
android:layout_marginStart="15dp" />
<Spinner
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/acceptLongestTrip"
android:hint="Enter Longest Trip"
style="@style/Widget.AppCompat.Spinner.Underlined"
android:layout_marginLeft="15dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.66"
android:layout_marginStart="15dp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/setlimit"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.76"
android:hint="Enter the value of longest possible trip"
android:layout_marginStart="15dp"
android:layout_marginLeft="15dp" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="35dp"
android:text="Next"
android:textSize="18dp"
android:textColor="#ffffff"
android:background="#8b36bc"
android:id="@+id/nextButton1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="1.0"/>
</android.support.constraint.ConstraintLayout>
问题在于您的编码方式 您必须更改的第一件事是设置侦听器的位置。应该避免在其他侦听器中设置侦听器 其次,代码将执行这些监听器中的所有代码,然后在这些监听器中执行代码 第三,如果你想用
微调器
中生成的消息来显示吐司
,你应该把它放在微调器的侦听器中
试着根据这些提示重新组织代码,看看是否还会出现问题。好的。我会尝试重新安排它。感谢您的回复我尝试将所有setOnItemSelectListener()放在onClickListener()之外,其他一切都很好。但正如您在我的代码中所看到的,我需要将该值放入捆绑包中并传递到下一个活动。现在我不能将bundle放入setOnItemSelectListener()中,因为每次用户选择一个值时,它都会被输入bundle中。我无法访问按钮onClickListener()中的所选值。@Paragrawal您可以根据业务规则执行许多操作。您可以将
setOnItemSelectListener()
中的数据存储在一个变量中,并在调用活动时获取此数据,此时,您可以将数据传递给捆绑包,甚至使用捆绑包本身。你可以把