英文:
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) {
}
})
}
}
我已经翻译了你的代码部分。
英文:
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) {
}
})
}
}
I have uploaded the images to my firebase Storage and then copied the image link to my database child "theimage". Refer uploaded Image.
put images storage link inside "theimage" node
What do you think is the best way to get all the images from my firebase Database to my ArrayList?
答案1
得分: 1
你需要循环遍历你的DataSnapshot
以将所有图像添加到ArrayList
中。此外,你的ArrayList
应该是String
类型的。请查看以下代码:
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
,请查看以下代码:
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) {
}
});
英文:
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) {
}
});
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) {
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论