简体   繁体   中英

Android Spinner selection disables selections in another spinner

I am making an android timetable application, and I have two spinners that allow the user to choose the start time and the duration of a lecture.

No lecture can finish later than 6PM, however the duration spinner allows for lectures to last up to 3 Hours. What I want to do, is when the user selects 4PM or 5PM, for the start time, the options for duration are limited based on this selection. For example, if the start time selected is 5PM, the duration can be no longer than 1 hour.

Thanks! Source code is from MainActivity.Java

public class MainActivity extends Activity
{
    //Data types, inputs and buttons
    private     EditText    codeInput;
    private     Spinner     dayInput;
    private     Spinner     timeInput;
    private     Spinner     durationInput;
    private     Spinner     sessionInput;
    private     EditText    roomInput;

    private     Button      addNewButton;
    private     Button      displayTimetableButton;

    //Creates the database handler
    DatabaseHandler DBHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Find spinners and EditText elements in layout
        codeInput       =   (EditText)  findViewById(R.id.CodeInput);
        dayInput        =   (Spinner)   findViewById(R.id.DayInput);
        timeInput       =   (Spinner)   findViewById(R.id.TimeInput);
        durationInput   =   (Spinner)   findViewById(R.id.DurationInput);
        sessionInput    =   (Spinner)   findViewById(R.id.SessionInput);
        roomInput       =   (EditText)  findViewById(R.id.RoomInput);

        //Find buttons in layout
        addNewButton            =   (Button)    findViewById(R.id.AddNewButton);
        displayTimetableButton  =   (Button)    findViewById(R.id.DisplayTimetableButton);

        addNewButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                //this method collects and adds all of the input data to the database
                String      code        =   codeInput.getText().toString();
                String      day         =   dayInput.getSelectedItem().toString();
                String      time        =   timeInput.getSelectedItem().toString();
                String      duration    =   durationInput.getSelectedItem().toString();
                String      session     =   sessionInput.getSelectedItem().toString();
                String      room        =   roomInput.getText().toString();

                DBHandler = new DatabaseHandler (getBaseContext());
                DBHandler.open();
                long DatabaseHandler = DBHandler.InsertData(code, day, time, duration, session, room);

                //This toast appears when the module information is successfully added
                Toast.makeText(getBaseContext(), "Module Added", Toast.LENGTH_LONG).show();

                //These set the EditText fields to blank, ready for the next input
                codeInput       .   setText("");
                roomInput       .   setText("");

                //Close the database connection
                DBHandler.close();
            }
        });

        displayTimetableButton.setOnClickListener(new View.OnClickListener()
        {
            //This method starts the GridView activity when the button is pressed
            @Override
            public void onClick(View v)
            {
                Intent i = new Intent(MainActivity.this, GridViewActivity.class);
                startActivity(i);
            }
        });
    }
}

Selections:

<string-array name="timeArray">
        <item>  9:00AM  </item>
        <item>  10:00AM </item>
        <item>  11:00AM </item>
        <item>  12:00AM </item>
        <item>  1:00PM  </item>
        <item>  2:00PM  </item>
        <item>  3:00PM  </item>
        <item>  4:00PM  </item>
        <item>  5:00PM  </item>
    </string-array>

<string-array name="durationArray">
        <item>1 Hour      </item>
        <item>2 Hours     </item>
        <item>3 Hours     </item>
    </string-array>

You could keep track of the selected item and use the event setOnItemSelectedListener . To change the second Spinner items accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM