I am currently working on an android application where i have to register new users into sqlite db and users enter their details into editText and we need to fectch entered data and store it in db
the thing is when i try it with hard coded data it works fine.
DBHelper.java
package com.example.db;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper {
public static final String DBNAME = "login.db";
public DBHelper(Context context) {
super(context, DBNAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create TABLE users(unsername TEXT primary key,password TEXT,fullName TEXT,email TEXT,phone TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop Table if exists users");
}
public void insertData(String username, String password, String fullName, String email,String phone) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("unsername",username);
contentValues.put("password",password);
contentValues.put("fullName",fullName);
contentValues.put("email",email);
contentValues.put("phone",phone);
}
}
here is signup.java activity
package com.example.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.os.UserHandle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityOptionsCompat;
import androidx.core.graphics.Insets;
import androidx.core.util.Pair;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import com.google.android.material.textfield.TextInputLayout;
public class SignUpPage extends AppCompatActivity {
// Varaible
Button loginbtn, signupbtn;
ImageView logo;
TextInputLayout uname, pass, mail, phoneNo, fname;
FirebaseDatabase rootNode;
DatabaseReference reference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_sign_up_page);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
loginbtn = findViewById(R.id.loginBtn);
signupbtn = findViewById(R.id.signUpBtn);
logo = findViewById(R.id.logo);
uname = findViewById(R.id.username);
pass = findViewById(R.id.pwd);
phoneNo = findViewById(R.id.phone);
mail = findViewById(R.id.email);
fname = findViewById(R.id.name);
String username = uname.getEditText().getText().toString();
String password = pass.getEditText().getText().toString();
String fullNmae = fname.getEditText().getText().toString();
String email = mail.getEditText().getText().toString();
String phone = phoneNo.getEditText().getText().toString();
DBHelper dbHelper = new DBHelper(this);
loginbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(SignUpPage.this, LoginPage.class);
Pair[] pair = new Pair[3];
pair[0] = new Pair<View, String>(logo, "logo_img");
pair[1] = new Pair<View, String>(loginbtn, "sign_up_in");
pair[2] = new Pair<View, String>(signupbtn, "back_sign_up_in");
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(SignUpPage.this, pair);
startActivity(intent, options.toBundle());
}
});
signupbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean insert = dbHelper.insertData(username,password,fullNmae,email,phone);
if (insert)
{
Toast.makeText(SignUpPage.this,"Inserted",Toast.LENGTH_LONG).show();
}else {
Toast.makeText(SignUpPage.this,"not inserted",Toast.LENGTH_LONG).show();
}
}
});
}
}
here all id like uname,fname,pass are TextInputLayout in which TextInputEditText is present
as mentioned above when i try passing "username" as chard coded strings the function work fine
String[] usernames = {"john_doe", "jane_smith", "bob_jackson"};
String[] passwords = {"StrongPassword123", "SecurePass456", "Pa$$w0rd789"};
String[] fullNames = {"John Doe", "Jane Smith", "Bob Jackson"};
String[] emails = {"john.doe@example.com", "jane.smith@example.com", "bob.jackson@example.com"};
String[] phones = {"123-456-7890", "987-654-3210", "555-123-4567"};
DBHelper dbHelper = new DBHelper(this);
// Insert data into the database
for (int i = 0; i < usernames.length; i++) {
dbHelper.insertData(usernames[i], passwords[i], fullNames[i], emails[i], phones[i]);
}
what i want is that when users enter their detail in TextInputEditText when signup btn is clicked all the data should be stored in String variable and dbinsert function should be called inserting all data into db
You need to read values from inputs at the button press time, not in onCreate()
. So move input.getEditText().getText().toString()
code under onClick()
like:
signupbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = uname.getEditText().getText().toString();
String password = pass.getEditText().getText().toString();
String fullNmae = fname.getEditText().getText().toString();
String email = mail.getEditText().getText().toString();
String phone = phoneNo.getEditText().getText().toString();
// ...
boolean insert = dbHelper.insertData(username,password,fullNmae,email,phone);
// ...
}
});