jeudi 16 juin 2016

How can i use a spinner with a button to redirect the app for different activites

I am trying to make my first interactive app and its my first time ueing a spinner for a drop down menu. What i want is the user to select an item from the dropdown menu and for that to be stored ina vairble so that when the button is clicked they are redirected to the proper page this is what i have in the main file

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {



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

    final Spinner s = (Spinner) findViewById(R.id.positonSpinner);
    s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            //Toast.makeText(LoginActivity.this,parent.getSelectedItem().toString(),Toast.LENGTH_SHORT).show();

            String Text = s.getSelectedItem().toString();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
}

public void goOnClick (View v)
{
    Button go = (Button)v;
    if (Text == "Student" )
    {
        startActivity(new Intent(LoginActivity.this,StudentUser.class));
    }
    else
    {
        startActivity(new Intent(LoginActivity.this,AdminUser.class) );
    }

}
}

Aucun commentaire:

Enregistrer un commentaire