如何从我的Firebase数据库中将多个图像添加到我的ArrayList?

huangapple go评论77阅读模式
英文:

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(&quot;theimage&quot;);

        final List&lt;Integer&gt; images = new ArrayList&lt;&gt;();

        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
如何从我的Firebase数据库中将多个图像添加到我的ArrayList?

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) {

    }
});

如果你还想获取像applesamsung这样的键,那么你需要使用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&lt;String&gt; images = new ArrayList&lt;&gt;();

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&lt;String, String&gt; images = new HashMap&lt;&gt;();

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) {

    }
});

huangapple
  • 本文由 发表于 2020年1月6日 17:39:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609739.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定