简体   繁体   中英

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

I have a list of Floats that capture the trajectory of the user's touch and have to store that list in Firebase Realtime Database. The variable is declared and stored as follows:

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);
    }

Here's the 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;
    }
}

What happens in this implementation is that all the data except the lists get stored. Is storing lists on firebase not allowed? If not, how can I proceed to do that?

When you are writing data to the Firebase Realtime Database using an object of type "TouchData", the fields in your class should be named according to JavaBeans standards.

This means that when using the following setter:

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

Firebase is looking after a property called XTrajectory and not xTrajectory . See the capital X? So to solve this, it makes more sense to name the property:

private List<Float> trajectoryX;

With the corresponding setter:

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

And getter:

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

To be able to use all the other fields, you should also change their names accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM