I’m creating sign_up and sign_in page and data that type into editview will be store in database.
So for database I create one class named DBAdapter.java and my anotherfile is signup.java and login.java.
So I want to do that when I click on submit button data will be store in database.
But issue is that I cannot use DBAdapter.java’s instance db into onclick event of submit button and same issue with login button in login.java file.
thanks in advance…
public class DBAdapter
{
public static final String KEY_ROWID = "_id";
public static final String KEY_FIRSTNAME = "FirstName";
public static final String KEY_LASTNAME = "LastName";
public static final String KEY_USERNAME = "UserName";
public static final String KEY_PASSWORD = "Password";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "master";
private static final String DATABASE_TABLE = "register";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE =
"create table titles (_id integer primary key autoincrement, "
+ "FirstName text not null, LastName text not null, "
+ "UserName text not null, Password text not null);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion
+ " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS titles");
onCreate(db);
}
}
and signup.java
public class SignUp extends Activity {
private EditText firstname;
private EditText lastname;
private EditText username;
private EditText password;
public String first_name;
public String last_name;
public String user_name;
public String pass_word;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DBAdapter db = new DBAdapter(this);
firstname = (EditText) findViewById(R.id.ev_fname);
lastname = (EditText) findViewById(R.id.ev_lname);
username = (EditText) findViewById(R.id.ev_uname);
password = (EditText) findViewById(R.id.ev_password);
final Button button = (Button) findViewById(R.id.btn_submit);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
first_name = firstname.getText().toString();
last_name = lastname.getText().toString();
user_name = username.getText().toString();
pass_word = password.getText().toString();
Intent goToNextActivity = new Intent(getApplicationContext(), link.class);
startActivity(goToNextActivity);
}
});
It’s more of a hack, I think, but what stops you from having this?
I’d like to find a more elegant solution myself, as there are multiple needs for this in my apps too.