❓ Help Android studio java

Ekko 12

Fanatic
package com.example.fixedit;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;


import androidx.appcompat.app.AppCompatActivity;


import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class SignUp extends AppCompatActivity {

EditText signupEmail, signupPassword, signupUsername, signupPhoneNumber;
RadioButton owner, customer;
Button signupButton;
TextView signupText;

FirebaseDatabase database;
DatabaseReference reference;

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

signupEmail = findViewById(R.id.signupEmail);
signupPassword = findViewById(R.id.signupPassword);
signupUsername = findViewById(R.id.signupUsername);
signupPhoneNumber = findViewById(R.id.signupPhoneNumber);
signupButton = findViewById(R.id.signupButton);
signupText = findViewById(R.id.signupText);
owner = findViewById(R.id.owner);
customer = findViewById(R.id.costumer);


signupButton.setOnClickListener(new View.OnClickListener() {
Override
public void onClick(View v) {

database = FirebaseDatabase.getInstance();

String email = signupEmail.getText().toString();
String password = signupPassword.getText().toString();
String username = signupUsername.getText().toString();
String phoneNumber = signupPhoneNumber.getText().toString();
String user = "";

if (owner.isChecked()) {
user = "Owner";
reference = database.getReference("Owner");
} else if (customer.isChecked()) {
user = "Customer";
reference = database.getReference("Costumer");
} else {
Toast.makeText(SignUp.this, "Please select a user type", Toast.LENGTH_SHORT).show();
return;
}

if (email.isEmpty() || password.isEmpty() || username.isEmpty() || phoneNumber.isEmpty()) {
Toast.makeText(SignUp.this, "Please fill in all fields", Toast.LENGTH_SHORT).show();
return;
}


HelperClass helperClass = new HelperClass(email, password, username, phoneNumber, user);
reference.child(username).setValue(helperClass);

Toast.makeText(SignUp.this, "Account Created", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(SignUp.this, LogIn.class);
startActivity(intent);

}
});


Hi sino kaya dito nakakaalam meron kasi notif yung isang variable ko po reassign varaible paano po ito ayusin nag erro po yung akin pumapasok yung data sa firebase then after na ma sign tapos nung ilolog in kona is ayaw ma recognize or madetect yung costumer reference

signupText.setOnClickListener(new View.OnClickListener() {
Override
public void onClick(View v) {
Intent intent = new Intent(SignUp.this, LogIn.class);
startActivity(intent);
}
});
}
}
 
It looks like you are encountering an issue with reassigning variables in your Android Studio project. Specifically, you mentioned that after signing up, the data is being sent to Firebase correctly but when you try to log in, the "Costumer" reference is not being recognized.

In your code snippet, I noticed a couple of potential issues that might be causing the problem:

1. Typo in DatabaseReference Names:
- You have a typo in the database references. You are using "Costumer" instead of "Customer" for the reference.
- Update the reference assignment for the customer to reference = database.getReference("Customer"); instead of reference = database.getReference("Costumer");.

2. Variable Scope Issue:
- The issue might be related to the scope of the reference variable. It seems that you are reassigning the reference variable inside the click listener based on the user type (Owner or Customer).
- Ensure that the reference variable is declared at the class level, so it can be accessed and updated within the click listener.

To address the issue, you can make the following modifications to your code:

Java:
public class SignUp extends AppCompatActivity {

    EditText signupEmail, signupPassword, signupUsername, signupPhoneNumber;
    RadioButton owner, customer;
    Button signupButton;
    TextView signupText;

    FirebaseDatabase database;
    DatabaseReference reference;

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

        signupEmail = findViewById(R.id.signupEmail);
        signupPassword = findViewById(R.id.signupPassword);
        signupUsername = findViewById(R.id.signupUsername);
        signupPhoneNumber = findViewById(R.id.signupPhoneNumber);
        signupButton = findViewById(R.id.signupButton);
        signupText = findViewById(R.id.signupText);
        owner = findViewById(R.id.owner);
        customer = findViewById(R.id.costumer);

        database = FirebaseDatabase.getInstance();
        reference = database.getReference(); // Initialize reference

        signupButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email = signupEmail.getText().toString();
                String password = signupPassword.getText().toString();
                String username = signupUsername.getText().toString();
                String phoneNumber = signupPhoneNumber.getText().toString();
                String user = "";

                if (owner.isChecked()) {
                    user = "Owner";
                    reference = database.getReference("Owner");
                } else if (customer.isChecked()) {
                    user = "Customer";
                    reference = database.getReference("Customer");
                } else {
                    Toast.makeText(SignUp.this, "Please select a user type", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (email.isEmpty() || password.isEmpty() || username.isEmpty() || phoneNumber.isEmpty()) {
                    Toast.makeText(SignUp.this, "Please fill in all fields", Toast.LENGTH_SHORT).show();
                    return;
                }

                HelperClass helperClass = new HelperClass(email, password, username, phoneNumber, user);
                reference.child(username).setValue(helperClass);

                Toast.makeText(SignUp.this, "Account Created", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(SignUp.this, LogIn.class);
                startActivity(intent);
            }
        });

        signupText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SignUp.this, LogIn.class);
                startActivity(intent);
            }
        });
    }
}

By making these changes, you should be able to address the issues with reassigning the reference variable and ensure that the correct reference is used when storing the data for Customers in Firebase.
 

About this Thread

  • 1
    Replies
  • 302
    Views
  • 1
    Participants
Last reply from:
Unknown user

Trending Topics

Online now

Members online
1,045
Guests online
1,038
Total visitors
2,083

Forum statistics

Threads
2,273,638
Posts
28,950,643
Members
1,234,900
Latest member
Somnus1416
Back
Top