如何在 Firebase 实时数据库中存储浮动列表?

How to store a list of floats in Firebase Realtime Database?

我有一个捕捉用户触摸轨迹的浮动列表,必须将该列表存储在 Firebase 实时数据库中。变量声明和存储如下:

public boolean dispatchTouchEvent(MotionEvent event) {
        TouchData touchData = new TouchData();
        int index = event.getActionIndex();
        int pointerId = event.getPointerId(index);
        //Lists which need to be stored
        List<Float> xTraj = new ArrayList<Float>();
        List<Float> yTraj = new ArrayList<Float>();


        final int X = (int) event.getRawX();
        final int Y = (int) event.getRawY();
        xTraj.add(event.getX());
        yTraj.add(event.getY());
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:

                xTraj.add(event.getX());
                yTraj.add(event.getY());
                if(mVelocityTracker == null) {
                    // Retrieve a new VelocityTracker object to watch the velocity of a motion.
                    mVelocityTracker = VelocityTracker.obtain();
                }
                else {
                    // Reset the velocity tracker back to its initial state.
                    mVelocityTracker.clear();
                }
                // Add a user's movement to the tracker.
                mVelocityTracker.addMovement(event);
                break;

            case MotionEvent.ACTION_UP:


                float centreX = button.getX() + button.getWidth()  / 2;
                float centreY = button.getY() + button.getHeight() / 2;

                long eventDuration = event.getEventTime() - event.getDownTime();

                DatabaseReference newRef = FirebaseDatabase.getInstance("https://pragya-datacollector.firebaseio.com").getReference();
                touchData.setUniqueID(currentUserID);
                touchData.setTaskName(TASK_NAME);
                touchData.setTouchDuration(eventDuration);
                touchData.setxTrajectory(xTraj);
                touchData.setyTrajectory(yTraj);
                touchData.setxVelocity(xVelocity);
                touchData.setyVelocity(yVelocity);
                touchData.setTargetX(centreX);
                touchData.setTargetY(centreY);
                newRef.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot snapshot) {
                        databaseReference.push().setValue(touchData);

                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError error) {

                    }
                });
                break;
            case MotionEvent.ACTION_MOVE:
                mVelocityTracker.addMovement(event);
                mVelocityTracker.computeCurrentVelocity(1);
                xVelocity = mVelocityTracker.getXVelocity();
                yVelocity = mVelocityTracker.getYVelocity();
                xTraj.add(event.getX());
                yTraj.add(event.getY());

                break;

            case MotionEvent.ACTION_CANCEL:
                mVelocityTracker.recycle();
                break;
        }
        return super.dispatchTouchEvent(event);
    }

这是 TouchData class:

public class TouchData {
    private String taskName;

    private long touchDuration;

    private List<Float> xTrajectory;

    private List<Float> yTrajectory;

    private float xVelocity;

    private float yVelocity;

    private String uniqueID;

    private float targetX;

    private float targetY;


    public TouchData() {

    }


    public String getTaskName() {
        return taskName;
    }

    public void setTaskName(String taskName) {
        this.taskName = taskName;
    }

    public List<Float> getxTrajectory() {
        return xTrajectory;
    }

    public void setxTrajectory(List<Float> touchTrajectory) {
        this.xTrajectory = xTrajectory;
    }

    public List<Float> getyTrajectory() {
        return yTrajectory;
    }

    public void setyTrajectory(List<Float> touchTrajectory) {
        this.yTrajectory = yTrajectory;
    }

    public long getTouchDuration() {
        return touchDuration;
    }

    public void setTouchDuration(long touchDuration) {
        this.touchDuration = touchDuration;
    }

    public float getxVelocity() {
        return xVelocity;
    }

    public void setxVelocity(float xVelocity) {
        this.xVelocity = xVelocity;
    }

    public float getyVelocity() {
        return yVelocity;
    }

    public void setyVelocity(float yVelocity) {
        this.yVelocity = yVelocity;
    }

    public String getUniqueID()
    {
        return uniqueID;
    }

    public void setUniqueID(String uniqueID) {
        this.uniqueID = uniqueID;
    }

    public float getTargetX() {
        return targetX;
    }

    public void setTargetX(float targetX) {
        this.targetX = targetX;
    }

    public float getTargetY() {
        return targetY;
    }

    public void setTargetY(float targetY) {
        this.targetY = targetY;
    }
}

此实现中发生的是存储除列表之外的所有数据。是否不允许在 firebase 上存储列表?如果没有,我该如何继续这样做?

当您使用“TouchData”类型的对象将数据写入 Firebase 实时数据库时,class 中的字段应根据 JavaBeans 标准命名。

这意味着当使用以下 setter:

public void setxTrajectory(List<Float> touchTrajectory) {
    this.xTrajectory = xTrajectory;
}

Firebase 正在管理名为 XTrajectory 而非 xTrajectory 的 属性。看到大写字母 X 了吗?所以要解决这个问题,命名 属性:

更有意义
private List<Float> trajectoryX;

对应setter:

public void setTrajectoryX(List<Float> trajectoryX) {
     this.trajectoryX = trajectoryX;
}

和getter:

public List<Float> getTrajectoryX() {
     return trajectoryX;
}

为了能够使用所有其他字段,您还应该相应地更改它们的名称。