如何将Firebase数据库中的多个图像添加到ArrayList中 [英] How do i add multiple Images from my Firebase Database to my ArrayList
问题描述
public class getFirebase extends AppCompatActivity {
DatabaseReference dbRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_get_firebase);
dbRef = FirebaseDatabase.getInstance().getReference().child("theimage");
final List<Integer> images = new ArrayList<>();
dbRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull com.google.firebase.database.DataSnapshot dataSnapshot) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
})
}
}
我已将图像上传到我的Firebase存储,然后将图像链接复制到我的数据库子代"theimage".请参考上传的图片.
I have uploaded the images to my firebase Storage and then copied the image link to my database child "theimage". Refer uploaded Image.
在"theimage"节点内放置图像存储链接
put images storage link inside "theimage" node
您认为将所有图像从Firebase数据库获取到ArrayList的最佳方法是什么?
What do you think is the best way to get all the images from my firebase Database to my ArrayList?
推荐答案
您必须遍历DataSnapshot
才能将所有图像放入ArrayList
.除此之外,您的ArrayList
应该是String
类型.检查以下内容:
You have to loop through your DataSnapshot
to get all the images into ArrayList
. Beside this your ArrayList
should be String
type. Check below:
final List<String> images = new ArrayList<>();
dbRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
images.add(childSnapshot.getValue(String.class));
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
如果您还想获取 apple , samsung 之类的密钥,则必须使用Map
而不是ArrayList
.检查以下内容:
If you also want to get the key like apple, samsung then you have to use Map
instead of ArrayList
. check below:
final Map<String, String> images = new HashMap<>();
dbRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
images.put(childSnapshot.getKey(), childSnapshot.getValue(String.class));
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
这篇关于如何将Firebase数据库中的多个图像添加到ArrayList中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!