使用getuid代替getkey插入Firebase实时数据库

时间:2020-09-29 12:45:16

标签: java android firebase-realtime-database firebase-authentication

我正在将数据插入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) {

        }
    });
}

2 个答案:

答案 0 :(得分:1)

@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()

答案 1 :(得分:1)

如果您想将数据写入用户的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列表。

相关问题