Tengo el siguiente proyecto el cual busco hacer llamado al spinner en el SesionFragment.java y al hacer click en el botón "iniciar sesión" en el texto del spinner seleccionado me lleve a otro activity, por ejemplo al seleccionar Secretario(a) me lleve al Activity3Main.java y al seleccionar Tesorero(a) me lleve al Activity4Main.java etc.
BD usuarios, contraseñas y roles
fragment_sesion.xml
<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.josue.login.SesionFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="@string/hello_blank_fragment"
android:textAlignment="center"
android:textColor="@color/colorPrimary"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="@string/user_fragment"
android:textColor="@color/colorPrimary"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.105"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/textView"
app:layout_constraintVertical_bias="0.189" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="@string/pwd_fragment"
android:textColor="@color/colorPrimary"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2"
app:layout_constraintVertical_bias="0.177" />
<EditText
android:id="@+id/txtuser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:ems="10"
android:hint="Digite su Usuario"
android:inputType="textPersonName"
android:textColor="@android:color/holo_blue_bright"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.287"
app:layout_constraintStart_toEndOf="@+id/textView2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.178" />
<EditText
android:id="@+id/txtpwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:ems="10"
android:hint="Digite su Contraseña"
android:inputType="textPassword"
android:textColor="@android:color/holo_blue_bright"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.741"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txtuser"
app:layout_constraintVertical_bias="0.127" />
<Button
android:id="@+id/btnsesion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="172dp"
android:text="@string/sesion_fragment"
android:textColor="@android:color/background_dark"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/textView"
app:layout_constraintEnd_toEndOf="@+id/textView"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/btnregistrar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="@string/registrar_fragment"
android:textColor="@android:color/background_dark"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnsesion"
app:layout_constraintVertical_bias="0.048" />
<Spinner
android:id="@+id/simpleSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/Cargo"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:textColor="@android:color/holo_blue_bright"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txtpwd"
app:layout_constraintVertical_bias="0.061" />
</android.support.constraint.ConstraintLayout>
SesionFragment.java
package com.example.josue.login;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.JsonRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class SesionFragment extends Fragment implements Response.Listener<JSONObject>, Response.ErrorListener {
RequestQueue rq;
JsonRequest jrq;
EditText txtUser, txtPwd;
Button btnSesion, btnRegistrar;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View vista = inflater.inflate(R.layout.fragment_sesion, container, false);
txtUser = (EditText) vista.findViewById(R.id.txtuser);
txtPwd = (EditText) vista.findViewById(R.id.txtpwd);
btnSesion = (Button) vista.findViewById(R.id.btnsesion);
btnRegistrar = (Button) vista.findViewById(R.id.btnregistrar);
rq = Volley.newRequestQueue(getContext());
btnSesion.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
iniciar_sesion();
}
});
btnRegistrar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
registrar_usuario();
}
});
// Inflate the layout for this fragment
return vista;
}
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(), "Usuario y/o Contraseña Incorrecto " + txtUser.getText().toString(), Toast.LENGTH_LONG).show();
}
@Override
public void onResponse(JSONObject response) {
Toast.makeText(getContext(), "Inicio de Sesión Exitoso " + txtUser.getText().toString(), Toast.LENGTH_SHORT).show();
User usuario = new User();
JSONArray jsonArray = response.optJSONArray("datos");
JSONObject jsonObject = null;
try {
jsonObject = jsonArray.getJSONObject(0);
usuario.setUser(jsonObject.optString("user"));
usuario.setPwd(jsonObject.optString("pwd"));
usuario.setNames(jsonObject.optString("names"));
} catch (JSONException e) {
e.printStackTrace();
}
Intent intencion = new Intent(getContext(), Main2Activity.class);
intencion.putExtra(Main2Activity.nombres, usuario.getNames());
startActivity(intencion);
}
void iniciar_sesion() {
String url = "https://xxxxxxxxxx.xxx.xxx/inicio.php?user=" + txtUser.getText().toString() +
"&pwd=" + txtPwd.getText().toString();
jrq = new JsonObjectRequest(Request.Method.GET, url, null, this, this);
rq.add(jrq);
}
void registrar_usuario(){
RegistrarFragment fr=new RegistrarFragment();
//fr.setArguments(fr);
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.escenario,fr)
.addToBackStack(null)
.commit();
}
}
strings.xml
<resources>
<string name="app_name">Ipuc Sesion</string>
<!-- TODO: Remove or change this placeholder text -->
<string name="hello_blank_fragment">Hermano(a), por favor digite sus datos</string>
<string name="user_fragment">Usuario:</string>
<string name="pwd_fragment">Contraseña:</string>
<string name="sesion_fragment">Iniciar Sesion</string>
<string name="registrar_fragment">Registrar usuario</string>
<string name="names_fragment">Nombres: </string>
<string-array name="Cargo">
<item>Seleccione su Cargo</item>
<item>Pastor</item>
<item>Tesorero(a)</item>
<item>Secretario(a)</item>
</string-array>
</resources>