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

Android理发师APP:跨Activity数据传递与预约信息展示需求

解决理发师应用的跨Activity数据传递与预约展示问题

嘿,正在开发理发师预约应用的你好呀!我来帮你搞定Landing页面收集预约信息,ContactUs页面展示已有预约的核心需求,同时解决跨Activity读写数据的问题~

核心思路

跨Activity传递数据有几种常用方案,根据你的场景推荐:

  • 临时传递单条/多条预约:用Intent + Parcelable(比Serializable效率更高)
  • 持久化保存历史预约:用SharedPreferences(简单场景)或Room数据库(复杂场景)

第一步:封装预约数据(Parcelable实体类)

先创建一个Appointment类来封装预约信息,这样不管是传递单条还是多条数据都更清晰:

package ie.app.barbershop;

import android.os.Parcel;
import android.os.Parcelable;

public class Appointment implements Parcelable {
    private String userName;
    private String userPhone;

    // 构造方法
    public Appointment(String userName, String userPhone) {
        this.userName = userName;
        this.userPhone = userPhone;
    }

    // Parcelable必备方法(可通过Android Studio自动生成)
    protected Appointment(Parcel in) {
        userName = in.readString();
        userPhone = in.readString();
    }

    public static final Creator<Appointment> CREATOR = new Creator<Appointment>() {
        @Override
        public Appointment createFromParcel(Parcel in) {
            return new Appointment(in);
        }

        @Override
        public Appointment[] newArray(int size) {
            return new Appointment[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(userName);
        dest.writeString(userPhone);
    }

    // Getter方法
    public String getUserName() {
        return userName;
    }

    public String getUserPhone() {
        return userPhone;
    }
}

第二步:Landing页面收集数据并跳转

在你的Landing.java里,给弹窗的确认按钮添加点击事件,收集用户输入的姓名/联系方式,然后传递给ContactUs:

package ie.app.barbershop;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.ArrayList;
import android.content.SharedPreferences;

public class Landing extends AppCompatActivity {
    // 假设弹窗里的输入框和按钮
    private EditText etName, etPhone;
    private Button btnConfirm, btnViewAppointments;
    // 存储预约列表(如果需要保留历史,结合SharedPreferences持久化)
    private ArrayList<Appointment> appointmentList = new ArrayList<>();

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

        // 初始化控件
        etName = findViewById(R.id.et_name);
        etPhone = findViewById(R.id.et_phone);
        btnConfirm = findViewById(R.id.btn_confirm_popup);
        btnViewAppointments = findViewById(R.id.btn_view_appointments);

        // 弹窗确认按钮:收集数据并添加到列表
        btnConfirm.setOnClickListener(v -> {
            String name = etName.getText().toString().trim();
            String phone = etPhone.getText().toString().trim();

            // 简单输入验证
            if (!name.isEmpty() && !phone.isEmpty()) {
                appointmentList.add(new Appointment(name, phone));
                // 持久化到SharedPreferences,防止重启App数据丢失
                saveAppointmentsToSP();
                // 关闭弹窗(根据你弹窗的实现处理,比如dialog.dismiss();)
            }
        });

        // "查看预约"按钮:跳转到ContactUs并传递数据
        btnViewAppointments.setOnClickListener(v -> {
            Intent intent = new Intent(Landing.this, ContactUs.class);
            // 传递预约列表
            intent.putParcelableArrayListExtra("KEY_APPOINTMENTS", appointmentList);
            startActivity(intent);
        });
    }

    // 持久化预约列表到SharedPreferences
    private void saveAppointmentsToSP() {
        // 需要引入Gson库,在build.gradle(Module)添加:
        // implementation 'com.google.code.gson:gson:2.10.1'
        SharedPreferences sp = getSharedPreferences("BarberShopPrefs", MODE_PRIVATE);
        String json = new com.google.gson.Gson().toJson(appointmentList);
        sp.edit().putString("SAVED_APPOINTMENTS", json).apply();
    }
}

第三步:ContactUs页面读取并展示预约信息

在ContactUs.java里,接收传递过来的预约数据,并用RecyclerView(推荐)展示:

package ie.app.barbershop;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
import android.content.SharedPreferences;

public class ContactUs extends AppCompatActivity {
    private RecyclerView rvAppointments;
    private AppointmentAdapter adapter;

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

        rvAppointments = findViewById(R.id.rv_appointments);
        rvAppointments.setLayoutManager(new LinearLayoutManager(this));

        // 1. 接收从Landing传递过来的预约列表
        ArrayList<Appointment> receivedList = getIntent().getParcelableArrayListExtra("KEY_APPOINTMENTS");

        // 2. 读取SharedPreferences里的历史预约(防止重启后数据丢失)
        ArrayList<Appointment> savedList = loadAppointmentsFromSP();
        if (savedList != null && !savedList.isEmpty()) {
            receivedList = savedList;
        }

        // 3. 设置Adapter展示数据
        adapter = new AppointmentAdapter(receivedList != null ? receivedList : new ArrayList<>());
        rvAppointments.setAdapter(adapter);
    }

    // 从SharedPreferences读取预约列表
    private ArrayList<Appointment> loadAppointmentsFromSP() {
        SharedPreferences sp = getSharedPreferences("BarberShopPrefs", MODE_PRIVATE);
        String json = sp.getString("SAVED_APPOINTMENTS", "");
        if (!json.isEmpty()) {
            return new com.google.gson.Gson().fromJson(json,
                    new com.google.gson.reflect.TypeToken<ArrayList<Appointment>>() {}.getType());
        }
        return null;
    }

    // 自定义RecyclerView Adapter
    private class AppointmentAdapter extends RecyclerView.Adapter<AppointmentAdapter.ViewHolder> {
        private ArrayList<Appointment> appointmentList;

        public AppointmentAdapter(ArrayList<Appointment> appointmentList) {
            this.appointmentList = appointmentList;
        }

        @Override
        public ViewHolder onCreateViewHolder(android.view.ViewGroup parent, int viewType) {
            android.view.View view = getLayoutInflater().inflate(R.layout.item_appointment, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            Appointment appointment = appointmentList.get(position);
            holder.tvName.setText(appointment.getUserName());
            holder.tvPhone.setText(appointment.getUserPhone());
        }

        @Override
        public int getItemCount() {
            return appointmentList.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            android.widget.TextView tvName, tvPhone;

            public ViewHolder(android.view.View itemView) {
                super(itemView);
                tvName = itemView.findViewById(R.id.tv_item_name);
                tvPhone = itemView.findViewById(R.id.tv_item_phone);
            }
        }
    }
}

补充说明

  1. 输入验证:记得在收集数据时判断输入是否为空,避免空指针异常
  2. 持久化选择:如果你的应用需要长期保存大量预约数据,推荐使用Room数据库(比SharedPreferences更适合结构化数据)
  3. 弹窗实现:如果你还没完成弹窗,可以用AlertDialog或者自定义DialogFragment,核心逻辑都是收集输入后传递数据

内容的提问来源于stack exchange,提问作者Aaron Brat Smith

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 08:33:11