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



我有一个Float列表,它捕捉用户触摸的轨迹,并且必须将该列表存储在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类:

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上存储列表?如果没有,我该如何处理?

当您使用类型为"的对象向Firebase实时数据库写入数据时;触摸数据";,类中的字段应该根据JavaBeans标准进行命名。

这意味着当使用以下设置器时:

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

Firebase正在寻找一个名为XTrajectory而非xTrajectory的属性。看到大写的X了吗?因此,为了解决这个问题,将属性命名为更有意义

private List<Float> trajectoryX;

使用相应的设置器:

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

和吸气剂:

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

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

最新更新