简体   繁体   English

当从实时数据库中检索到 android 工作室的链接时,它会添加一个标签

[英]when retrieving a link from realtime database to android studio it adds a tag

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.所以基本上当我从我的实时数据库中检索一个链接时,它会在开头添加这个“{abs =”,在最后添加这个“}”,这使得毕加索无法加载链接。 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: `这是 XML 代码:`

<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>` </androidx.constraintlayout.widget.ConstraintLayout>`

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();
               // 👆
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM