Advertisement
Guest User

Untitled

a guest
May 22nd, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.77 KB | None | 0 0
  1. package com.example.marco.mylibreria;
  2.  
  3. import android.content.Intent;
  4. import android.content.SharedPreferences;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.Toast;
  11.  
  12. import WebRequest.WebLoginClass;
  13.  
  14. public class LoginActivity extends AppCompatActivity {
  15.  
  16.     private Button bottoneLogin;
  17.     private EditText editTextUsername;
  18.     private EditText editTextPassword;
  19.  
  20.     @Override
  21.     protected void onCreate(Bundle savedInstanceState) {
  22.         super.onCreate(savedInstanceState);
  23.         setContentView(R.layout.activity_login);
  24.  
  25.         SharedPreferences preferences = getPreferences(MODE_PRIVATE);
  26.         int id = preferences.getInt("user-id", -1);
  27.         if (id > 0) {
  28.             Intent intent = new Intent(getApplicationContext(), CatalogoActivity.class);
  29.             startActivity(intent);
  30.  
  31.             editTextUsername = (EditText) findViewById(R.id.editTextUsername);
  32.             editTextPassword = (EditText) findViewById(R.id.editTextPassword);
  33.             bottoneLogin = (Button) findViewById(R.id.buttonLogin);
  34.  
  35.             bottoneLogin.setOnClickListener(new View.OnClickListener() {
  36.                 @Override
  37.                 public void onClick(View v) {
  38.                     WebLoginClass loginClass = new WebLoginClass();
  39.                     String username = editTextUsername.getText().toString();
  40.                     String password = editTextUsername.getText().toString();
  41.  
  42.                     loginClass.setUsername(username);
  43.                     loginClass.setPassword(password);
  44.  
  45.                     try {
  46.                         if (loginClass.effettuaLogin() > 0) {
  47.  
  48.                             // scrivilo nelle preferenze
  49.                             SharedPreferences my_preference = getPreferences(MODE_PRIVATE);
  50.                             SharedPreferences.Editor editor = my_preference.edit();
  51.  
  52.                             // ricorda il commit
  53.                             //editor.putInt("user-id",loginClass.getId());
  54.                             //editor.commit();
  55.  
  56.                             Intent intent = new Intent(getApplicationContext(), CatalogoActivity.class);
  57.                             startActivity(intent);
  58.  
  59.                         } else {
  60.                             Toast.makeText(getApplicationContext(),
  61.                                     "Hai inserito i dati errati,riprova!",
  62.                                     Toast.LENGTH_LONG)
  63.                                     .show();
  64.                         }
  65.                     } catch (Exception e) {
  66.                         e.printStackTrace();
  67.                     }
  68.                 }
  69.             });
  70.  
  71.  
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement