如何从Firebase到列表视图中检索特定用户的数据

时间:2018-06-26 08:57:57

标签: android firebase firebase-realtime-database

这是我的当前/特定登录用户如何添加到数据库的代码,我需要帮助以Listview或recyclerView检索这些特定用户的信息。我不希望其他用户获取其他用户的数据。

这也是我的数据库的结构,在用户水费支付详细信息中,生成的第一个节点是用户ID,用户ID之后的下一个节点是交易ID。

数据库结构: Structure of my database

mAuth = FirebaseAuth.getInstance();
currentID = mAuth.getCurrentUser().getUid();
databaseWater= FirebaseDatabase.getInstance().getReference().child("users water bill payment details").child(currentID);

progressBar= new ProgressDialog(this);
moneynumberwat = (EditText) findViewById(R.id.moneynumberwat);
moneypinwat = (EditText) findViewById(R.id.moneypinwat);
meterwat = (EditText) findViewById( R.id.meterwat);
user_idwat = (EditText) findViewById(R.id.user_idwat);
amountwat = (EditText) findViewById(R.id.amountwat);
modePaywat = (Spinner) findViewById(R.id.modePaywat);
Paywater = (Button) findViewById(R.id.Paywater);

Paywater.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        addPayWaterInfo();
    }
});

setubk();
setupbk();
}


private void setubk() {
    ImageButton btn2 = findViewById(R.id.bord);
    btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
            Intent intent = new Intent(PayOffWater.this, Interface.class);
            startActivity(intent);
        }
    });
}

private void setupbk() {
    ImageButton btn2 = findViewById(R.id.bak);
    btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
            Intent intent = new Intent(PayOffWater.this, Services.class);
            startActivity(intent);
        }
    });
}

private void addPayWaterInfo() {
    String moneynumw = moneynumberwat.getText().toString();
    String pincodelitw = moneypinwat.getText().toString();
    String meterw = meterwat.getText().toString();
    String customerw = user_idwat.getText().toString();
    String cashw = amountwat.getText().toString();
    String mpayw = modePaywat.getSelectedItem().toString();

        if( moneynumw.isEmpty()||moneynumw.length()>10 || moneynumw.length()<10 ||
                pincodelitw.length()>4 || pincodelitw.length()<4 ||meterw.isEmpty()||cashw.isEmpty()|| mpayw.isEmpty()){
            Toast.makeText(this,"provide correct information before payment can be successful",Toast.LENGTH_LONG).show();

        } else{
            String id = databaseWater.push().getKey();
            com.example.iamstix_jnr.utility.PayOf.Pay pay = new Pay(moneynumw,pincodelitw,meterw,customerw,cashw,mpayw);
            databaseWater.child(id).setValue(pay);

            finish();
            Intent intent = new Intent(PayOffWater.this,Success.class);
            startActivity(intent);
        }
    }

2 个答案:

答案 0 :(得分:0)

您必须导航到最后一个ID。

    databaseWater= FirebaseDatabase.getInstance().getReference().child("users water bill payment details").child(currentID).child("id of no 3.");

enter image description here

答案 1 :(得分:0)

您必须在数据库引用上添加ChildEventListener,并在每个回调上添加或删除或更新您在recyclerview上具有的适配器

databaseWater.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
            //todo add to adapter
        }

        @Override
        public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
            //todo update adapter
        }

        @Override
        public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
            //todo remove from adapter
        }

        @Override
        public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
            //todo update adapter

        }

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

        }
    });