Boa Tarde Pessoal
Na minha aplicação tenho um login de usuário e senha. Preciso pegar o valor do campo de usuário e levar pra outra classe.
Eu armazeno o usuário na variável “userid”. Qual seria a melhor solução para o meu caso?
CLASSE QUE PRECISO PEGAR O VALOR
public class MainActivity extends Activity {
ConnectionClass connectionClass;
EditText edtuserid,edtpass;
Button btnlogin;
ProgressBar pbbar;
EditText ed1,ed2,ed3;
Button b1;
Intent in;
public static final String MyPREFERENCES = "MyPrefs" ;
public static final String Name = "usuarioKey";
public static final String Phone = "senhaKey";
SharedPreferences sharedpreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Instancia da classe ConnectionClass
connectionClass = new ConnectionClass();
//EditViews, TextViews, PrograssBars,Buttons
edtuserid = (EditText) findViewById(R.id.edtuserid);
edtpass = (EditText) findViewById(R.id.edtpass);
btnlogin = (Button) findViewById(R.id.btnEnviarRec);
pbbar = (ProgressBar) findViewById(R.id.pbbar);
pbbar.setVisibility(View.GONE);
//OnClickListener para o Login
btnlogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DoLogin doLogin = new DoLogin();
doLogin.execute("");
}
});
}
public class DoLogin extends AsyncTask<String,String,String>
{
String z = "";
Boolean isSuccess = false;
public String userid = edtuserid.getText().toString();// VARIAVEL QUE ARMAZENO O VALOR DO USUARIO
String password = edtpass.getText().toString();
@Override
protected void onPreExecute() {
pbbar.setVisibility(View.VISIBLE);
}
//@Override
protected void onPostExecute(String r) {
pbbar.setVisibility(View.GONE);
Toast.makeText(MainActivity.this,r,Toast.LENGTH_SHORT).show();
if(isSuccess) {
Intent i = new Intent(MainActivity.this, MenuInicio_U.class);
startActivity(i);
finish();
}
}
@Override
protected String doInBackground(String... params) {
/* MenuInicio_U men = new MenuInicio_U();
int ty = 201771;
men.Recebe(ty); */
if(userid.trim().equals("")|| password.trim().equals(""))
z = "Por Favor, entre com o Usuário e Senha";
else
{
try {
Connection con = connectionClass.CONN();
if (con == null) {
z = "Error in connection with SQL server";
} else {
//String query = "select * from RBUsers where FirstName='" + userid + "' and LastName='" + password + "'";
String query = "select * from RBUsers where IDUser='" + userid + "' and LastName='" + password + "'";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
if(rs.next())
{
z = "Logado com sucesso";
isSuccess=true;
}
else
{
z = "Credenciais Inválidas";
isSuccess = false;
}
}
}
catch (Exception ex)
{
isSuccess = false;
z = "Exceptions";
}
}
return z;
}
}
}