Android微调控制器实现不显示选定的项目

问题描述:

我有3个Spinners,我从他们每个人中选择值。但是,当我在下一个按钮的onClickListener()之外声明setOnItemSelected()方法时,选定的值不会显示在Toast中。当我在按钮的onClickListener中声明setOnItemSelected()方法时,它可以工作,但当我从上一个微调器中选择“Set Limit”时,我无法隐藏编辑文本。 请帮忙。 以下是我的.java文件。Android微调控制器实现不显示选定的项目

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); 
     } 
    }); 
} 
} 

以下是我的布局文件。

<?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> 

你编码的方式是这个问题。

你必须改变的第一件事是你设置监听器的地方。您应该避免在其他侦听器中设置侦听器。其次,代码将执行这些侦听器中的所有代码,然后在这些侦听器中执行代码。

第三,如果您想要在Spinner内显示Toast的消息,请将其放入微调器的侦听器中。

尝试重新组织代码思考这些提示,看看你是否会再次遇到问题。

+0

好的。将尝试重新排列它。感谢您的回复 –

+0

我试着把所有setOnItemSelectListener()放在onClickListener()之外,其他一切正常。但正如你在我的代码中看到的那样,我需要把这个值放入这个包中并传递给下一个活动。现在我不能把bundle放在setOnItemSelectListener()中,因为每次用户选择一个值时,它都会被输入到bundle中。而且我无法访问按钮的onClickListener()内的选定值。 –

+0

@ParagAgrawal你可以根据你的商业规则做很多事情。您可以将来自'setOnItemSelectListener()'的数据存储在一个变量中,并在您调用该活动时获取该数据,并且此时可以将数据传递给该包,甚至可以使用该包本身。您可以直接从'setOnItemSelectListener()'中将数据放入包中,并进行检查以避免重复值。我有一个问题,如果你只使用数组的第一个位置,为什么要使用String []? –