英文:
Insert into firebase real-time database with getuid instead of getkey
问题
这是我的当前代码: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) {
}
});
}
英文:
I am inserting data into the firebase real-time database and then getting the data and displaying it on a graph but when i insert into the database now i am using the unique getkey()
method to insert the values but i would like to use the getuid()
method as the user is already authenticated in the app. I'd like to know how I can change the getkey()
to use the getuid().
This is my current code: The getkey()
method is in the setListneners()
method
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) {
}
});
}
答案1
得分: 0
@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) {
}
});
}
Then you can get Uid
mAuth.getUid();
英文:
@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) {
}
});
}
Then you can get Uid
mAuth.getUid()
答案2
得分: 0
如果您想将数据写入用户的 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 下获得一个由自动生成的(push)ID 组成的列表。
英文:
If you want to write the data under the user's UID instead of the key generated by push()
, you'd do something like this:
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);
If you want to store a list of points for each user, you'd change that last line to:
reference.child(id).push().setValue(pointValue);
So this way you get a list of auto-generated (push) IDs under each UID.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论