如何从一个孩子那里获取数据

时间:2019-05-28 10:35:47

标签: android

在我的Firebase中,生成了两个孩子,一个是"Users",第二个是用户部分中的“兑换”部分,注册表格被保存,积分也被保存,并且在兑换用户时发送数据以兑换积分,我的问题是当我试图从“用户”中检索数据,然后自动将“用户”和“兑换”的数据合并。可能是因为“用户”和“兑换”具有当前用户的相同“ UID”,现在我的主要观点是以在文本视图中分别在同一活动“用户”和“兑现”中显示数据。

public class redeem extends AppCompatActivity {

TextView textname,textemail,textnumber,texttotalpoits;
EditText editname,editemail,editpaytmnumber,editredeempoints,edittotalpoints;
Button submitbtn;

private FirebaseDatabase mFirebaseDatabase;
private FirebaseAuth mAuth;
private  FirebaseAuth.AuthStateListener mAuthListener;
private DatabaseReference myRef;
private String userID;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.redeem);

    submitbtn = findViewById(R.id.submiybtn);
    textname = findViewById(R.id.textname);
    textemail = findViewById(R.id.textemail);
    textnumber = findViewById(R.id.textnumber);
    texttotalpoits = findViewById(R.id.texttotalpoints);
    editname = findViewById(R.id.editname);
    editemail = findViewById(R.id.editemail);
    editpaytmnumber = findViewById(R.id.editpaytmnumber);
    editredeempoints = findViewById(R.id.editredeempoints);
    edittotalpoints = findViewById(R.id.edittotalpoints);


    mAuth = FirebaseAuth.getInstance();
    mFirebaseDatabase = FirebaseDatabase.getInstance();
    myRef = mFirebaseDatabase.getReference();
    FirebaseUser user = mAuth.getCurrentUser();
    userID = user.getUid();

    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();

        }
    };
    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            showData(dataSnapshot);
        }

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

        }
    });
    submitbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final String name = editname.getText().toString().trim();
            final String email = editemail.getText().toString().trim();
            final String number = editpaytmnumber.getText().toString().trim();
            final String points = edittotalpoints.getText().toString().trim();
            final String redeempoints = editredeempoints.getText().toString().trim();

            if (name.isEmpty()) {
                editname.setError(getString(R.string.input_error_email));
                editname.requestFocus();
                return;
            }
            if (email.isEmpty()) {
                editemail.setError(getString(R.string.input_error_email));
                editemail.requestFocus();
                return;
            }
            if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
                editemail.setError(getString(R.string.input_error_email_invalid));
                editemail.requestFocus();
                return;
            }
            if (number.isEmpty()) {
                editpaytmnumber.setError("Please Enter Paytm Number");
                editpaytmnumber.requestFocus();
                return;
            }
            int number1 = Integer.parseInt(texttotalpoits.getText().toString());
            int number2 = Integer.parseInt(editredeempoints.getText().toString());
            if (number2 > number1){
                editredeempoints.setError("You Have No Enough Points");
                editredeempoints.setText("");
            }
            RedeemInfromation redeemInfromation = new RedeemInfromation(
                    name,
                    email,
                    number,
                    points,
                    redeempoints
            );

            FirebaseDatabase.getInstance().getReference("redeem")
                    .child(FirebaseAuth.getInstance().getCurrentUser().getUid())
                    .setValue(redeemInfromation);
        }
    });
}
public void showData(DataSnapshot dataSnapshot){
    for (DataSnapshot ds : dataSnapshot.getChildren()){
        UserInformation uInfo = new UserInformation();
        uInfo.setName(ds.child(userID).getValue(UserInformation.class).getName());
        uInfo.setEmail(ds.child(userID).getValue(UserInformation.class).getEmail());
        uInfo.setNumber(ds.child(userID).getValue(UserInformation.class).getNumber());
        uInfo.setPoints(ds.child(userID).getValue(UserInformation.class).getPoints());

        textname.setText("" + uInfo.getName());
        textemail.setText("" + uInfo.getEmail());
        textnumber.setText("" + uInfo.getNumber());
        texttotalpoits.setText("" + uInfo.getPoints());
        edittotalpoints.setText("" + uInfo.getPoints());

    }

}
@Override
protected void onStart() {
    super.onStart();
    mAuth.addAuthStateListener(mAuthListener);
}

@Override
protected void onStop() {
    super.onStop();
    mAuth.removeAuthStateListener(mAuthListener);
}
}

Here is the snapshot of my data in firebase

0 个答案:

没有答案