I am building an Android app with a login screen that stores the username and password in an SQLite database. After a successful login, the user is taken to a Movie Ticket Booking screen where they can choose a movie, theatre, and showtime using spinners. I want to save the selected movie booking information in the database as well, tied to the logged-in user.
Currently, the login part works, but I’m not sure how to:
Save the selected spinner values (movie, theatre, time) into the database.
Retrieve and display the previously booked selection when the user logs in again.
How should I structure the database and code to support this?
I implemented the login and registration functionality using SQLiteOpenHelper, and it works — users can register and log in. After login, I created another activity with spinners for movie, theatre, and time selection.
I tried saving the selected spinner values using an updateBooking() method in my database helper, similar to how I saved the grades in a previous app. However, it doesn’t seem to update or retrieve correctly — the booking info is not saved or shown when the user logs in again.
I expected that after logging in and selecting options from the spinners, the data would be saved in the database and shown the next time the user logs in.
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical" android:padding="16dp">
<EditText android:id="@+id/etUsername"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:hint="Username" />
<EditText android:id="@+id/etPassword"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:hint="Password" android:inputType="textPassword" />
<Button android:id="@+id/btnLogin"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:text="Login" />
<Button android:id="@+id/btnRegister"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:text="Register" />
</LinearLayout>
LoginActivity.java
package com.example.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
public class LoginActivity extends AppCompatActivity {
EditText etUsername, etPassword;
Button btnLogin, btnRegister;
DBHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etUsername = findViewById(R.id.etUsername);
etPassword = findViewById(R.id.etPassword);
btnLogin = findViewById(R.id.btnLogin);
btnRegister = findViewById(R.id.btnRegister);
dbHelper = new DBHelper(this);
btnRegister.setOnClickListener(v -> {
String user = etUsername.getText().toString();
String pass = etPassword.getText().toString();
if (dbHelper.registerUser(user, pass)) {
Toast.makeText(this, "User Registered!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "User Exists!", Toast.LENGTH_SHORT).show();
}
});
btnLogin.setOnClickListener(v -> {
String user = etUsername.getText().toString();
String pass = etPassword.getText().toString();
if (dbHelper.loginUser(user, pass)) {
Intent i = new Intent(this, MainActivity.class);
i.putExtra("username", user);
startActivity(i);
finish();
} else {
Toast.makeText(this, "Invalid login", Toast.LENGTH_SHORT).show();
}
});
}
}
activity_booking.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical" android:padding="16dp">
<TextView android:id="@+id/tvWelcome"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="18sp" />
<Spinner android:id="@+id/spMovie"
android:layout_width="match_parent" android:layout_height="wrap_content" />
<Spinner android:id="@+id/spTime"
android:layout_width="match_parent" android:layout_height="wrap_content" />
<Button android:id="@+id/btnBook"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:text="Book Ticket" />
</LinearLayout>
android manifest
<activity
android:name=".LoginActivity"
android:exported="true">
main.java
package com.example.myapplication;
import android.os.Bundle;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
Spinner spMovie, spTime;
Button btnBook;
TextView tvWelcome;
DBHelper dbHelper;
String username;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_booking);
spMovie = findViewById(R.id.spMovie);
spTime = findViewById(R.id.spTime);
btnBook = findViewById(R.id.btnBook);
tvWelcome = findViewById(R.id.tvWelcome);
dbHelper = new DBHelper(this);
username = getIntent().getStringExtra("username");
tvWelcome.setText("Welcome, " + username);
String[] movies = {"Inception", "Interstellar", "Oppenheimer"};
String[] times = {"10:00 AM", "1:00 PM", "4:00 PM", "7:00 PM"};
spMovie.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, movies));
spTime.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, times));
btnBook.setOnClickListener(v -> {
String movie = spMovie.getSelectedItem().toString();
String time = spTime.getSelectedItem().toString();
if (dbHelper.saveBooking(username, movie, time)) {
Toast.makeText(this, "Ticket Booked!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Booking Failed!", Toast.LENGTH_SHORT).show();
}
});
}
}
DBhelpder.java
package com.example.myapplication;
import android.content.*;
import android.database.Cursor;
import android.database.sqlite.*;
public class DBHelper extends SQLiteOpenHelper {
public static final String DB_NAME = "MovieApp.db";
public DBHelper(Context context) {
super(context, DB_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE users(username TEXT PRIMARY KEY, password TEXT)");
db.execSQL("CREATE TABLE bookings(username TEXT, movie TEXT, time TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
db.execSQL("DROP TABLE IF EXISTS users");
db.execSQL("DROP TABLE IF EXISTS bookings");
onCreate(db);
}
public boolean registerUser(String username, String password) {
SQLiteDatabase db = getWritableDatabase();
Cursor c = db.rawQuery("SELECT * FROM users WHERE username=?", new String[]{username});
if (c.getCount() > 0) return false;
ContentValues cv = new ContentValues();
cv.put("username", username);
cv.put("password", password);
return db.insert("users", null, cv) != -1;
}
public boolean loginUser(String username, String password) {
SQLiteDatabase db = getReadableDatabase();
Cursor c = db.rawQuery("SELECT * FROM users WHERE username=? AND password=?", new String[]{username, password});
return c.getCount() > 0;
}
public boolean saveBooking(String username, String movie, String time) {
SQLiteDatabase db = getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("username", username);
cv.put("movie", movie);
cv.put("time", time);
return db.insert("bookings", null, cv) != -1;
}
}