34829

getValue of database entry onitemClick

Question:

I'm having an issue getting the Value of a database entry and passing it to intent.in the following code onItemClick is passing the data of the whole database over as opposed to the item clicked. How can I specify to only pass the data of the item clicked over?? Newbie to Firebase so im at a loss, any help greatly appreciated

ValueEventListener eventListener = new ValueEventListener() { @Override public void onDataChange(@NonNull DataSnapshot dataSnapshot) { for (DataSnapshot ds : dataSnapshot.getChildren()) { id.add(ds.getKey()); String title = ds.child("title").getValue(String.class); String content = ds.child("content").getValue(String.class); String timestamp = ds.child("timestamp").getValue(String.class); list.add(title + "\n" + timestamp + "\n" + content); Log.d("TAG", title); } ListView listView = findViewById(R.id.progressList); listView.setAdapter(arrayAdapter); } @Override public void onCancelled(@NonNull DatabaseError databaseError) { } }; uidRef.addListenerForSingleValueEvent(eventListener); final ListView progressList = findViewById(R.id.progressList); progressList.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int i, long l) { ValueEventListener eventListener = new ValueEventListener() { @Override public void onDataChange(@NonNull DataSnapshot dataSnapshot) { for (DataSnapshot ds : dataSnapshot.getChildren()) { Intent intent = new Intent(list_view.this, ProgressReports.class); String title = ds.child("title").getValue(String.class); String content = ds.child("content").getValue(String.class); intent.putExtra("titleTest", title); intent.putExtra("contentTest", content); startActivity(intent); } ListView listView = findViewById(R.id.progressList); listView.setAdapter(arrayAdapter); } @Override public void onCancelled(@NonNull DatabaseError databaseError) { } }; uidRef.addListenerForSingleValueEvent(eventListener); } });

Answer1:

This is not the right way of getting data:

String title = putData.child("title").getValue(String.class);

You cannot call getValue() method on a DatabaseReference object. In order to get data from the database you need to attach a listener. Everything in Firebase is about listeners. So you should do the exact same thing that you in your first lines of code.

Recommend

  • Retrieve and check whether a particular user enter email is present in firebase
  • Firebase retrieve data Null outside method
  • Unable to detect if dataSnapshot is null or not under 'addChildEventListener()`
  • Use Lombok @RequiredArgsConstructor without @NonNull
  • Android Firebase addListenerForSingleValueEvent not called
  • Android Firebase query data
  • How to store object as string?
  • Firebase database data retrieving -Java
  • Sum of total value of rating from firebase
  • How to receive latitude and longitude data stored from Firebase and put it in a marker
  • Moving or copying data from one node to another in firebase database
  • Firebase Dynamic Link ShortUrl not working in Android
  • Paging Library invalidating data source not working
  • wrap content in JavaFX
  • How to paginate a Firebase Recyclerview
  • How to make object in List eligible for garbage collection?
  • gridview on page won't refresh, even when calling databind again
  • Firebase OnSuccessListener is being called when it should fail
  • How can I use 'addChildEventListener()' accurately for following purpose?
  • JSF binding with setValueExpression read-only?
  • Focus Traversal Policy in TitledPane
  • ASP.NET Gridview Paging Problem
  • android.app.PendingIntent cannot be accessed ouside the package
  • sending/ receiving email in Java
  • Microsoft Visual Studio Community 2015 always crashes in Windows 10 if swithed to Visual FoxPro
  • Cannot Parse HTML Data Using Android / JSOUP
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • JTable with a ScrollPane misbehaving
  • Angular 2 constructor injection vs direct access
  • Java static initializers and reflection
  • How to stop GridView from loading again when I press back button?
  • Android Google Maps API OnLocationChanged only called once
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • failed to connect to specific WiFi in android programmatically
  • UserPrincipal.Current returns apppool on IIS
  • How can I use threading to 'tick' a timer to be accessed by other threads?