javaandroidandroid-studiofirebase-realtime-databasepicasso

I have a problem with retrieving data from firebase realtime database


so basically when i retrieve a link from my real time database it adds this "{abs=" at the beginning and this "}" at the end which makes picasso unable to load the link. i tried loading the link by pasting the link directly into picasso and it worked just fine Here is a copy of my code :

public class MainActivity extends AppCompatActivity {
private Button button;
private TextView tv0, tv1;
private String string;
private ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv0 = findViewById(R.id.textView);
    imageView = findViewById(R.id.imageView);
    tv1 = findViewById(R.id.textView2);
    button = findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("muscles");
            reference.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {
                    for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
                        string = snapshot.getValue().toString();
                    }
                    tv1.setText(string);
                    tv0.setText("{abs=https://firebasestorage.googleapis.com/v0/b/my-application-5272b.appspot.com/o/ts-space-sun-and-solar-viewing-facts-versus-fiction.webp?alt=media&token=264af080-27a5-4177-ade3-6eaf30125383}");
                    Picasso.get().load("https://firebasestorage.googleapis.com/v0/b/my-application-5272b.appspot.com/o/ts-space-sun-and-solar-viewing-facts-versus-fiction.webp?alt=media&token=264af080-27a5-4177-ade3-6eaf30125383").into(imageView);
                }

                @Override
                public void onCancelled(@NonNull DatabaseError error) {
                }
            });

        }
    });
}

and this is the XML code : `

<android.widget.Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/button"
    android:text="Button"
    android:textColor="@color/white"
    app:layout_constraintBottom_toTopOf="@+id/textView2"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="tt"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toTopOf="@+id/button"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.498"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.593"
    tools:srcCompat="@tools:sample/avatars" />

</androidx.constraintlayout.widget.ConstraintLayout>`


Solution

  • The problem is here:

    public void onDataChange(@NonNull DataSnapshot snapshot) {
        for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
            string = snapshot.getValue().toString();
        }
    

    While you're looping over the snapshot you get from the database to get to the individual child nodes, you're actually always getting the value of the parent node.

    To fix this:

    public void onDataChange(@NonNull DataSnapshot snapshot) {
        for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
            string = dataSnapshot.getValue().toString();
                   // 👆
        }