如何在不更改数据的情况下从Firebase检索数据?

时间:2019-03-02 08:01:00

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

//这是MainActivity,我已经使用firebase存储示例数据,可以在其中进行更新和检索。 但是只有在数据发生更改时才可以检索数据,但是我想查看我的数据已经在firebase中 here is the data base structure] 1

public class MainActivity extends 
 AppCompatActivity {
EditText name,weight;
Button uploadBtn;
TextView itemName,itemWeight;
DatabaseReference databaseReference=FirebaseDatabase.getInstance().getReference();
Map<String,String>values=new HashMap<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    name=findViewById(R.id.inputItemName);
    weight=findViewById(R.id.inputItemWeight);
    uploadBtn=findViewById(R.id.uploadButton);
    itemName=findViewById(R.id.resultName);
    itemWeight=findViewById(R.id.resultWeight);

//如您所见,在上载新值之前,我已经使用ValueEventListener来显示从数据库中检索到的值,但是它不起作用,除非更改了数据值(即,仅在上载新值后才有效) )。

 ValueEventListener valueEventListener=new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            String nameD=(String) dataSnapshot.child("Name").getValue();
            String weightD = (String) dataSnapshot.child("Weight").getValue();
            itemName.setText(nameD);
            itemWeight.setText(weightD+" Kg");
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    };
    databaseReference.addListenerForSingleValueEvent(valueEventListener);
    uploadBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            values.put("Name",name.getText().toString());
            values.put("Weight",weight.getText().toString());
            databaseReference.child("UToouch").setValue(values);

        }
    });

    databaseReference.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

        }

        @Override
        public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                 String nameDb=(String) dataSnapshot.child("Name").getValue();
                 String weightDb = (String) dataSnapshot.child("Weight").getValue();
                 itemName.setText(nameDb);
                 itemWeight.setText(weightDb+" Kg");
        }

        @Override
        public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

}

1 个答案:

答案 0 :(得分:0)

要获取您的NameWeight属性的值,请使用以下代码:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = rootRef.child("UToouch");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String name = dataSnapshot.child("Name").getValue(String.class);
        String weight = dataSnapshot.child("Weight").getValue(String.class);
        Log.d(TAG, name + " / " + weight);
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
    }
};
ref.addListenerForSingleValueEvent(valueEventListener);

您的logcat中的输出将是:

rittesh / pv