插入 Firebase 实时数据库时使用 getuid 而不是 getkey。

huangapple go评论92阅读模式
英文:

Insert into firebase real-time database with getuid instead of getkey

问题

这是我的当前代码:getkey() 方法在 setListneners() 方法中:

  1. FirebaseDatabase database;
  2. DatabaseReference reference;
  3. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/mm/yyyy");
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_weight);
  8. Btn_Display = findViewById(R.id.btn_display);
  9. graphView = findViewById(R.id.weight_graph);
  10. series = new LineGraphSeries();
  11. graphView.addSeries(series);
  12. database = FirebaseDatabase.getInstance();
  13. reference = database.getReference("WeightGraph");
  14. setListeners();
  15. }
  16. private void setListeners() {
  17. Btn_Display.setOnClickListener(new View.OnClickListener() {
  18. @Override
  19. public void onClick(View view) {
  20. String id = reference.push().getKey();
  21. long x = new Date().getTime();
  22. int y = Integer.parseInt(weight.getText().toString());
  23. PointValue pointValue = new PointValue(x, y);
  24. reference.child(id).setValue(pointValue);
  25. }
  26. });
  27. graphView.getGridLabelRenderer().setNumHorizontalLabels(2);
  28. graphView.getGridLabelRenderer().setNumVerticalLabels(2);
  29. graphView.getGridLabelRenderer().setLabelFormatter(new DefaultLabelFormatter(){
  30. @Override
  31. public String formatLabel(double value, boolean isValueX) {
  32. if(isValueX){
  33. return simpleDateFormat.format(new Date((long) value));
  34. }else{
  35. return super.formatLabel(value, isValueX);
  36. }
  37. }
  38. });
  39. }
  40. @Override
  41. protected void onStart() {
  42. super.onStart();
  43. reference.addValueEventListener(new ValueEventListener() {
  44. @Override
  45. public void onDataChange(@NonNull DataSnapshot snapshot) {
  46. DataPoint[] dataPoints = new DataPoint[(int) snapshot.getChildrenCount()];
  47. int index = 0;
  48. for(DataSnapshot dataSnapshot : snapshot.getChildren()){
  49. PointValue pointValue = dataSnapshot.getValue(PointValue.class);
  50. dataPoints[index] = new DataPoint(pointValue.getxValue(), pointValue.getyValue());
  51. series.resetData(dataPoints);
  52. }
  53. }
  54. @Override
  55. public void onCancelled(@NonNull DatabaseError error) {
  56. }
  57. });
  58. }
英文:

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

  1. FirebaseDatabase database;
  2. DatabaseReference reference;
  3. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/mm/yyyy");
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_weight);
  8. Btn_Display = findViewById(R.id.btn_display);
  9. graphView = findViewById(R.id.weight_graph);
  10. series = new LineGraphSeries();
  11. graphView.addSeries(series);
  12. database = FirebaseDatabase.getInstance();
  13. reference = database.getReference("WeightGraph");
  14. setListeners();
  15. }
  16. private void setListeners() {
  17. Btn_Display.setOnClickListener(new View.OnClickListener() {
  18. @Override
  19. public void onClick(View view) {
  20. String id = reference.push().getKey();
  21. long x = new Date().getTime();
  22. int y = Integer.parseInt(weight.getText().toString());
  23. PointValue pointValue = new PointValue(x, y);
  24. reference.child(id).setValue(pointValue);
  25. }
  26. });
  27. graphView.getGridLabelRenderer().setNumHorizontalLabels(2);
  28. graphView.getGridLabelRenderer().setNumVerticalLabels(2);
  29. graphView.getGridLabelRenderer().setLabelFormatter(new DefaultLabelFormatter(){
  30. @Override
  31. public String formatLabel(double value, boolean isValueX) {
  32. if(isValueX){
  33. return simpleDateFormat.format(new Date((long) value));
  34. }else{
  35. return super.formatLabel(value, isValueX);
  36. }
  37. }
  38. });
  39. }
  40. @Override
  41. protected void onStart() {
  42. super.onStart();
  43. reference.addValueEventListener(new ValueEventListener() {
  44. @Override
  45. public void onDataChange(@NonNull DataSnapshot snapshot) {
  46. DataPoint[] dataPoints = new DataPoint[(int) snapshot.getChildrenCount()];
  47. int index = 0;
  48. for(DataSnapshot dataSnapshot : snapshot.getChildren()){
  49. PointValue pointValue = dataSnapshot.getValue(PointValue.class);
  50. dataPoints[index] = new DataPoint(pointValue.getxValue(), pointValue.getyValue());
  51. series.resetData(dataPoints);
  52. }
  53. }
  54. @Override
  55. public void onCancelled(@NonNull DatabaseError error) {
  56. }
  57. });
  58. }

答案1

得分: 0

  1. @Override
  2. protected void onStart() {
  3. super.onStart();
  4. FirebaseUser mAuth = FirebaseAuth.getInstance().getCurrentUser();
  5. reference.addValueEventListener(new ValueEventListener() {
  6. @Override
  7. public void onDataChange(@NonNull DataSnapshot snapshot) {
  8. DataPoint[] dataPoints = new DataPoint[(int) snapshot.getChildrenCount()];
  9. int index = 0;
  10. for(DataSnapshot dataSnapshot : snapshot.getChildren()){
  11. PointValue pointValue = dataSnapshot.getValue(PointValue.class);
  12. dataPoints[index] = new DataPoint(pointValue.getxValue(), pointValue.getyValue());
  13. series.resetData(dataPoints);
  14. }
  15. }
  16. @Override
  17. public void onCancelled(@NonNull DatabaseError error) {
  18. }
  19. });
  20. }
  21. Then you can get Uid
  22. mAuth.getUid();
英文:
  1. @Override
  2. protected void onStart() {
  3. super.onStart();
  4. FirebaseUser mAuth = FirebaseAuth.getInstance().getCurrentUser()
  5. reference.addValueEventListener(new ValueEventListener() {
  6. @Override
  7. public void onDataChange(@NonNull DataSnapshot snapshot) {
  8. DataPoint[] dataPoints = new DataPoint[(int) snapshot.getChildrenCount()];
  9. int index = 0;
  10. for(DataSnapshot dataSnapshot : snapshot.getChildren()){
  11. PointValue pointValue = dataSnapshot.getValue(PointValue.class);
  12. dataPoints[index] = new DataPoint(pointValue.getxValue(), pointValue.getyValue());
  13. series.resetData(dataPoints);
  14. }
  15. }
  16. @Override
  17. public void onCancelled(@NonNull DatabaseError error) {
  18. }
  19. });
  20. }

Then you can get Uid

  1. mAuth.getUid()

答案2

得分: 0

如果您想将数据写入用户的 UID 下,而不是由 push() 生成的键,您可以像这样操作:

  1. String id = FirebaseAuth.getInstance().getCurrentUser().getUid();
  2. long x = new Date().getTime();
  3. int y = Integer.parseInt(weight.getText().toString());
  4. PointValue pointValue = new PointValue(x, y);
  5. reference.child(id).setValue(pointValue);

如果您想为每个用户存储一系列的数据点,您需要将最后一行更改为:

  1. 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:

  1. String id = FirebaseAuth.getInstance().getCurrentUser().getUid();
  2. long x = new Date().getTime();
  3. int y = Integer.parseInt(weight.getText().toString());
  4. PointValue pointValue = new PointValue(x, y);
  5. reference.child(id).setValue(pointValue);

If you want to store a list of points for each user, you'd change that last line to:

  1. reference.child(id).push().setValue(pointValue);

So this way you get a list of auto-generated (push) IDs under each UID.

huangapple
  • 本文由 发表于 2020年9月29日 20:45:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/64119931.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定