从实时数据库中取出数据

时间:2018-12-14 17:36:09

标签: android firebase-realtime-database

我的应用程序如何向实时数据库添加值:

 private void uploadFile() {
    if(imageUri != null){

        final StorageReference fileReference = storageReference.child(System.currentTimeMillis()
                + "." + getFileExtension(imageUri));

        storageTask = fileReference.putFile(imageUri);

        Task<Uri> urlTask = storageTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
            @Override
            public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
                if (!task.isSuccessful()) {
                    throw task.getException();
                }
                // Continue with the task to get the download URL
                return fileReference.getDownloadUrl();
            }
        }).addOnCompleteListener(new OnCompleteListener<Uri>() {
            @Override
            public void onComplete(@NonNull Task<Uri> task) {
                if (task.isSuccessful()) {
                    Uri downloadUri = task.getResult();
                    String miUrlOk = downloadUri.toString();

                    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
                    final String kisiId = user.getUid().toString().trim();
                    final String kisiAd = user.getDisplayName().toString().trim();
                    final String urunIsim = editurunIsim.getText().toString().trim();
                    final String urunYorum = editUrunYorum.getText().toString();


                    Map<String,Object> values = new HashMap<String,Object>();
                    values.put("kisiId", kisiId);
                    values.put("kisiAd", kisiAd);
                    values.put("urunAdi", urunIsim);
                    values.put("yorum", urunYorum);
                    Kullanici kullanici = new Kullanici(kisiId, kisiAd, urunIsim,urunYorum,miUrlOk);
                    DatabaseReference dbRef = db.getReference("Kullanici");
                    String key = dbRef.push().getKey();
                    DatabaseReference databaseReference = db.getReference("Kullanici/" + key);
                    databaseReference.setValue(kullanici);

                    String referansinAnahtari=databaseReference.getKey().toString();

                    databaseReference.child("urunYorum").child(referansinAnahtari).setValue(values);




                    Toast.makeText(getActivity(), "Upload successful", Toast.LENGTH_LONG).show();


                } else {
                    // Handle failures
                    // ...
                }
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });

    }else{
        Toast.makeText(getActivity(), "No File selected", Toast.LENGTH_SHORT).show();
    }
}

我的应用如何从实时数据库中读取值(一部分):

            kullaniciList.clear();
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {

                Kullanici kullaniciModel = postSnapshot.getValue(Kullanici.class);
                kullaniciModel.setKey(postSnapshot.getKey());
                kullaniciList.add(kullaniciModel);
            }


            adapterr.setOnItemClickListener(anasayfaFragment.this);
            adapterr.notifyDataSetChanged();
            progressCircle.setVisibility(View.INVISIBLE);

使用调试运行调试时,我的调试已关闭。我认为这是一个逻辑错误。如何从实时数据库捕获数据?

0 个答案:

没有答案