Advertisement
Guest User

signUp

a guest
Apr 6th, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.21 KB | None | 0 0
  1. package com.example.tiketsaya;
  2.  
  3. import android.content.Intent;
  4. import android.content.SharedPreferences;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9. import android.widget.LinearLayout;
  10. import android.widget.Toast;
  11.  
  12. import com.google.firebase.database.DataSnapshot;
  13. import com.google.firebase.database.DatabaseError;
  14. import com.google.firebase.database.DatabaseReference;
  15. import com.google.firebase.database.FirebaseDatabase;
  16. import com.google.firebase.database.ValueEventListener;
  17.  
  18. import androidx.annotation.NonNull;
  19. import androidx.appcompat.app.AppCompatActivity;
  20.  
  21. public class RegisterOneActivity extends AppCompatActivity {
  22.  
  23.     LinearLayout btnBack;
  24.     Button btnContinue;
  25.     EditText username,password,email_address;
  26.     DatabaseReference reference;
  27.  
  28.     String USERNAME_KEY = "usernamekey";
  29.     String username_key = "";
  30.  
  31.     @Override
  32.     protected void onCreate(Bundle savedInstanceState) {
  33.         super.onCreate(savedInstanceState);
  34.         setContentView(R.layout.activity_register_one);
  35.  
  36.         username = findViewById(R.id.username);
  37.         password = findViewById(R.id.password);
  38.         email_address = findViewById(R.id.email_address);
  39.  
  40.         btnBack = findViewById(R.id.btnBack);
  41.         btnBack.setOnClickListener(new View.OnClickListener() {
  42.             @Override
  43.             public void onClick(View v) {
  44.                 onBackPressed();
  45.             }
  46.         });
  47.  
  48.         btnContinue = findViewById(R.id.btnContinue);
  49.         btnContinue.setOnClickListener(new View.OnClickListener() {
  50.             @Override
  51.             public void onClick(View v) {
  52.                 //save to local
  53.                 SharedPreferences sharedPreferences = getSharedPreferences(USERNAME_KEY, MODE_PRIVATE);
  54.                 SharedPreferences.Editor editor = sharedPreferences.edit();
  55.                 editor.putString(username_key, username.getText().toString());
  56.                 editor.apply();
  57.                 //Toast.makeText(getApplicationContext(), "Username: " + username.getText().toString(), Toast.LENGTH_SHORT).show();
  58.  
  59.                 //save to db
  60.                 reference = FirebaseDatabase.getInstance().getReference().child(username.getText().toString());
  61.                 reference.addListenerForSingleValueEvent(new ValueEventListener() {
  62.                     @Override
  63.                     public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  64.                         dataSnapshot.getRef().child("username").setValue(username.getText().toString());
  65.                         dataSnapshot.getRef().child("password").setValue(password.getText().toString());
  66.                         dataSnapshot.getRef().child("email_address").setValue(email_address.getText().toString());
  67.                         dataSnapshot.getRef().child("user_balance").setValue(800);
  68.                     }
  69.  
  70.                     @Override
  71.                     public void onCancelled(@NonNull DatabaseError databaseError) {
  72.  
  73.                     }
  74.                 });
  75.  
  76.                 Intent nextRegist = new Intent(RegisterOneActivity.this, RegisterTwoActivity.class);
  77.                 startActivity(nextRegist);
  78.             }
  79.         });
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement