我将数据插入firebase实时数据库,然后获取数据并将其显示在图形上,但当我现在插入数据库时,我使用唯一的getkey()
方法来插入值,但我希望使用getuid()
方法,因为用户已经在应用程序中进行了身份验证。我想知道如何将getkey()
更改为使用getuid().
这是我当前的代码:getkey()
方法在setListneners()
方法中
FirebaseDatabase database;
DatabaseReference reference;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/mm/yyyy");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_weight);
Btn_Display = findViewById(R.id.btn_display);
graphView = findViewById(R.id.weight_graph);
series = new LineGraphSeries();
graphView.addSeries(series);
database = FirebaseDatabase.getInstance();
reference = database.getReference("WeightGraph");
setListeners();
}
private void setListeners() {
Btn_Display.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String id = reference.push().getKey();
long x = new Date().getTime();
int y = Integer.parseInt(weight.getText().toString());
PointValue pointValue = new PointValue(x, y);
reference.child(id).setValue(pointValue);
}
});
graphView.getGridLabelRenderer().setNumHorizontalLabels(2);
graphView.getGridLabelRenderer().setNumVerticalLabels(2);
graphView.getGridLabelRenderer().setLabelFormatter(new DefaultLabelFormatter(){
@Override
public String formatLabel(double value, boolean isValueX) {
if(isValueX){
return simpleDateFormat.format(new Date((long) value));
}else{
return super.formatLabel(value, isValueX);
}
}
});
}
@Override
protected void onStart() {
super.onStart();
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
DataPoint[] dataPoints = new DataPoint[(int) snapshot.getChildrenCount()];
int index = 0;
for(DataSnapshot dataSnapshot : snapshot.getChildren()){
PointValue pointValue = dataSnapshot.getValue(PointValue.class);
dataPoints[index] = new DataPoint(pointValue.getxValue(), pointValue.getyValue());
series.resetData(dataPoints);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
@Override
protected void onStart() {
super.onStart();
FirebaseUser mAuth = FirebaseAuth.getInstance().getCurrentUser()
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
DataPoint[] dataPoints = new DataPoint[(int) snapshot.getChildrenCount()];
int index = 0;
for(DataSnapshot dataSnapshot : snapshot.getChildren()){
PointValue pointValue = dataSnapshot.getValue(PointValue.class);
dataPoints[index] = new DataPoint(pointValue.getxValue(), pointValue.getyValue());
series.resetData(dataPoints);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
然后你可以得到Uid
mAuth.getUid()
如果您想在用户的UID而不是push()
生成的密钥下写入数据,您可以执行以下操作:
String id = FirebaseAuth.getInstance().getCurrentUser().getUid();
long x = new Date().getTime();
int y = Integer.parseInt(weight.getText().toString());
PointValue pointValue = new PointValue(x, y);
reference.child(id).setValue(pointValue);
如果您想为每个用户存储一个点列表,您可以将最后一行更改为:
reference.child(id).push().setValue(pointValue);
因此,通过这种方式,您可以在每个UID下获得一个自动生成(推送(ID的列表。