【发布时间】:2021-09-27 22:27:39
【问题描述】:
我有一个浮点列表,可以捕获用户触摸的轨迹,并且必须将该列表存储在 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上存储列表吗?如果没有,我该如何继续这样做?
【问题讨论】:
-
首先,停止忽略错误。使用
Log.d(TAG, error.getMessage());。您是否在 logcat 中打印了一些内容? -
我正在尝试这样做,但我看到了消息
Tag Manager is not found and thus will not be used -
试试
Log.d("YOUR_TAG", error.getMessage());。 -
Firebase 的
onCancelled方法中没有打印任何内容,这就是您希望我将日志消息放在哪里? -
将 Float 类型的对象列表添加到 Firebase 实时数据库绝对是可能的。尝试将
List<Float> xTrajectory重命名为List<Float> trajectoryX并将 setter 重命名为public void setTrajectoryX(List<Float> trajectoryX) { this.trajectoryX = trajectoryX; }并告诉我它是否有效。
标签: java android firebase firebase-realtime-database arraylist