我有一个自定义列表适配器,它有许多查询数据库并将数据添加到不同java对象或列表的方法。
在适配器中考虑的代码如下: -
static class ViewHolder {
ArrayList<String> imgUrls;
//other variables;
}
然后在getView方法中将其启动为: -
public View getView(@params) {
holder.imgUrls = new ArrayList<>();
}
//i have a method that query and retrieve data from firebase as follows:-
public void getPhoto(final ViewHolder holder) {
Query query = reference
.child(mContext.getString(R.string.dbname_collections))
.child(holder.collection.getCollection_id())
.child("photo_url");
query.keepSynced(true);
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()) {
imgUrl = singleSnapshot.getValue(String.class);
Log.d(TAG, "onDataChange: imgUrl: " + imgUrl);
holder.imgUrls.add(imgUrl);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Log.d(TAG, "getPhoto: list with values " + holder.imgUrls);
}
我在firebase控制台中的数据库如下所示: -
{
"collections": {
"-L9nNSyF7ZhEqpvmPo0Z": {
"caption": "jakhaha",
"collection_id": "-L9nNSyF7ZhEqpvmPo0Z",
"date_created": "2018-04-11T11:55:57Z",
"photo_url": [
"https://firebasestorage.googleapis.com/v0/b/backbench-72a47.appspot.com/o/collection%2Fusers%2FHb8ORoozQ3WhyxjaSyqazjHypkf2%2F2018-04-11T11%3A55%3A57Z%2Fphoto0?alt=media&token=08fdb740-9b16-455b-83ec-bfcec1455834",
"https://firebasestorage.googleapis.com/v0/b/backbench-72a47.appspot.com/o/collection%2Fusers%2FHb8ORoozQ3WhyxjaSyqazjHypkf2%2F2018-04-11T11%3A55%3A57Z%2Fphoto1?alt=media&token=eeca4b10-0729-4ca9-be18-c31d054add19",
"https://firebasestorage.googleapis.com/v0/b/backbench-72a47.appspot.com/o/collection%2Fusers%2FHb8ORoozQ3WhyxjaSyqazjHypkf2%2F2018-04-11T11%3A55%3A57Z%2Fphoto2?alt=media&token=b33c97b0-c2bf-4199-bef0-b498f505a678"
],
//this are the imageUrls i want to retrieve as a arrylist
"tags": "#NoTags",
"user_id": "Hb8ORoozQ3WhyxjaSyqazjHypkf2"
}
}
}
在我的firebase模型类中: -
public class Collection implements Parcelable {
//properties for photo (*case sensitive*)
private String caption;
private String date_created;
private String collection_id;
private String user_id;
private String tags;
//list of likes
private List<String> photo_url;
private List<Like> likes;
private List<Comment> comments;
public Collection() {
}
//constructor..getter setters etc...
}
在我的日志控制台中,我看到imgUrl的值是胖的
04-11 20:35:03.992 21269-21269/com.backbenchers.administrator.instaclone D/CollectionListAdapter: onDataChange: imgUrl://url of image 1
onDataChange: imgUrl: url of image 2
onDataChange: imgUrl: ...image 3
但它将列表显示为null或[]: -
D/CollectionListAdapter: getPhoto: list is with values : []
&#34;我想获取此firebase数据并将其添加到arraylist中。&#34;
有什么不对?
谢谢!