DEV Community

mushokuuuu
mushokuuuu

Posted on

Database and Fragments 101 for Android Dev

//activity_ticket_printing.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/ticket_details"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp" />

    <Button
        android:id="@+id/back_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Back" />
</LinearLayout>

Enter fullscreen mode Exit fullscreen mode

//TicketPrintingActivity.java

public class TicketPrintingActivity extends AppCompatActivity {

    private TextView ticketDetailsTextView;
    private Button backButton;

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

        ticketDetailsTextView = findViewById(R.id.ticket_details);
        backButton = findViewById(R.id.back_button);

        // Retrieve ticket details from intent
        Intent intent = getIntent();
        String name = intent.getStringExtra("name");
        String age = intent.getStringExtra("age");
        String date = intent.getStringExtra("date");
        String time = intent.getStringExtra("time");
        String seatType = intent.getStringExtra("seatType");

        // Display ticket details
        String ticketDetails = "Name: " + name + "\nAge: " + age + "\nDate: " + date + "\nTime: " + time + "\nSeat Type: " + seatType;
        ticketDetailsTextView.setText(ticketDetails);

        backButton.setOnClickListener(v -> finish());
    }
}

Enter fullscreen mode Exit fullscreen mode

//fragment_past_bookings.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">

    <ListView
        android:id="@+id/past_bookings_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

Enter fullscreen mode Exit fullscreen mode

//PastBookingsFragment.java

public class PastBookingsFragment extends Fragment {

    private ListView pastBookingsListView;
    private ArrayAdapter<String> adapter;
    private ArrayList<String> pastBookings;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_past_bookings, container, false);

        pastBookingsListView = view.findViewById(R.id.past_bookings_list);
        pastBookings = new ArrayList<>();

        // Retrieve past bookings from SQLite database
        SQLiteDatabase db = getActivity().openOrCreateDatabase("TravelBookingDB", Context.MODE_PRIVATE, null);
        Cursor cursor = db.rawQuery("SELECT * FROM bookings", null);
        while (cursor.moveToNext()) {
            String bookingDetails = "Name: " + cursor.getString(0) + "\nAge: " + cursor.getString(1) + "\nDate: " + cursor.getString(2) + "\nTime: " + cursor.getString(3) + "\nSeat Type: " + cursor.getString(4);
            pastBookings.add(bookingDetails);
        }
        cursor.close();

        adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, pastBookings);
        pastBookingsListView.setAdapter(adapter);

        return view;
    }
}

Enter fullscreen mode Exit fullscreen mode

//fragment_profile.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/username_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp" />

    <Button
        android:id="@+id/logout_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Logout" />
</LinearLayout>

Enter fullscreen mode Exit fullscreen mode

//ProfileFragment.java

public class ProfileFragment extends Fragment {

    private usernameTextView;
    private Button logoutButton;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_profile, container, false);

        usernameTextView = view.findViewById(R.id.username_text);
        logoutButton = view.findViewById(R.id.logout_button);

        // Retrieve username from shared preferences or intent
        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("UserPrefs", Context.MODE_PRIVATE);
        String username = sharedPreferences.getString("username", "Guest");
        usernameTextView.setText("Username: " + username);

        logoutButton.setOnClickListener(v -> {
            // Handle logout logic here
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.clear();
            editor.apply();

            // Navigate back to login/signup activity
            Intent intent = new Intent(getActivity(), LoginSignupActivity.class);
            startActivity(intent);
            getActivity().finish();
        });

        return view;
    }
}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)