You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

开发裁缝APP:能否在Android Studio中创建带用户参数的类存储Firebase实时DB数据?

Creating Custom Garment Classes for Firebase Realtime Database in Android Studio

Absolutely! You can absolutely create custom classes tailored to your garment types (shirts, pants, jackets) in Android Studio, and store their user-provided parameter data in Firebase Realtime Database. This is actually a standard, recommended approach for organizing structured order data—perfect for your tailor app.

Here's how to implement this step by step:

1. Create Garment-Specific Custom Classes

First, define classes for each garment type that mirror the parameters your users will input. Firebase requires a few key things for these classes to work:

  • A no-argument constructor (for deserializing data from Firebase)
  • Getter methods for all fields (or make fields public, though getters are preferred for encapsulation)
  • A parameterized constructor to create instances from user input

Example: Shirt Class

public class Shirt {
    // Required no-arg constructor for Firebase
    public Shirt() {}

    // Fields matching user input parameters
    private String customerName;
    private int height;
    private int chestCircumference;
    private int sleeveLength;

    // Parameterized constructor to create shirt instances
    public Shirt(String customerName, int height, int chestCircumference, int sleeveLength) {
        this.customerName = customerName;
        this.height = height;
        this.chestCircumference = chestCircumference;
        this.sleeveLength = sleeveLength;
    }

    // Getters (required for Firebase to read/write fields)
    public String getCustomerName() { return customerName; }
    public void setCustomerName(String customerName) { this.customerName = customerName; }
    public int getHeight() { return height; }
    public void setHeight(int height) { this.height = height; }
    public int getChestCircumference() { return chestCircumference; }
    public void setChestCircumference(int chestCircumference) { this.chestCircumference = chestCircumference; }
    public int getSleeveLength() { return sleeveLength; }
    public void setSleeveLength(int sleeveLength) { this.sleeveLength = sleeveLength; }
}

Example: Pants Class

public class Pants {
    public Pants() {}

    private String customerName;
    private int height;
    private int waistCircumference;
    private int thighCircumference;
    private int inseamLength;

    public Pants(String customerName, int height, int waistCircumference, int thighCircumference, int inseamLength) {
        this.customerName = customerName;
        this.height = height;
        this.waistCircumference = waistCircumference;
        this.thighCircumference = thighCircumference;
        this.inseamLength = inseamLength;
    }

    // Corresponding getters and setters
    public String getCustomerName() { return customerName; }
    public void setCustomerName(String customerName) { this.customerName = customerName; }
    public int getHeight() { return height; }
    public void setHeight(int height) { this.height = height; }
    public int getWaistCircumference() { return waistCircumference; }
    public void setWaistCircumference(int waistCircumference) { this.waistCircumference = waistCircumference; }
    public int getThighCircumference() { return thighCircumference; }
    public void setThighCircumference(int thighCircumference) { this.thighCircumference = thighCircumference; }
    public int getInseamLength() { return inseamLength; }
    public void setInseamLength(int inseamLength) { this.inseamLength = inseamLength; }
}

2. Store Custom Garment Objects in Firebase

Modify your existing Firebase code to use these custom classes instead of a generic Object. A key improvement is using push() to generate unique IDs for each order—this prevents overwriting existing orders and keeps your data organized.

Example: Saving a Shirt Order

// Initialize Firebase reference (matches your existing code)
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();

// Get user input values (replace with your actual input handling)
String customerName = "Jane Smith";
int userHeight = 165;
int chestSize = 88;
int sleeveLength = 58;

// Create a Shirt instance from user input
Shirt customShirt = new Shirt(customerName, userHeight, chestSize, sleeveLength);

// Store the shirt under "orders/shirts" with a unique ID
rootRef.child("orders").child("shirts").push().setValue(customShirt)
        .addOnSuccessListener(v -> {
            // Handle successful save (e.g., show a toast to the user)
            Toast.makeText(getApplicationContext(), "Shirt order saved!", Toast.LENGTH_SHORT).show();
        })
        .addOnFailureListener(e -> {
            // Handle save failure
            Toast.makeText(getApplicationContext(), "Save failed: " + e.getMessage(), Toast.LENGTH_SHORT).show();
        });

3. Retrieve and Display Stored Orders

To show past orders to users, you can read the data from Firebase and deserialize it back into your custom garment objects:

Example: Loading Shirt Orders

rootRef.child("orders").child("shirts").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        List<Shirt> shirtOrders = new ArrayList<>();
        for (DataSnapshot orderSnapshot : snapshot.getChildren()) {
            // Convert Firebase data back to a Shirt object
            Shirt shirt = orderSnapshot.getValue(Shirt.class);
            if (shirt != null) {
                shirtOrders.add(shirt);
            }
        }
        // Bind shirtOrders to a RecyclerView/ListView to display to the user
        // (e.g., set up an adapter and attach it to your UI component)
    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {
        // Handle failed data retrieval
        Log.e("FirebaseError", "Failed to load orders: " + error.getMessage());
    }
});

Key Notes for Firebase Compatibility

  • No-arg constructor is mandatory: Firebase needs this to create instances when reading data from the database.
  • Match data types: Ensure the fields in your classes match the data types users input (e.g., use float instead of int if you need decimal measurements).
  • Organize your data structure: Group orders by garment type (like orders/shirts or orders/pants) or customer ID to make querying and managing data easier.

To directly answer your core question: Yes, this approach works perfectly in Android Studio. Your custom classes with user-provided parameters will serialize and deserialize seamlessly with Firebase Realtime Database.

内容的提问来源于stack exchange,提问作者Adarsh Shete

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 09:17:46