firebase的图像无法显示
我已经尽力了,最后我不得不问这个问题
这是我的适配器类
public class ViewPagerAdapter extends PagerAdapter {
Context context;
public String[] imageUrls ;
public ViewPagerAdapter(Context context,String[] imageUrls){
this.context=context;
this.imageUrls=imageUrls;
}
@Override
public int getCount() {
return imageUrls.length;
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
return view==o;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
Picasso.get()
.load(imageUrls[position])
.fit()
.centerCrop()
.into(imageView);
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
}
这是我从Firebase获取数据的另一门课
private void loadImages(String insideHostelID) {
ImageDatabaseReference.child(insideHostelID).child("INSIDE_PICTURES").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Inside_location inside_location=dataSnapshot.getValue(Inside_location.class) ;
// Toast.makeText(hostelDetails.this,""+ inside_location.getPIC9(),Toast.LENGTH_SHORT).show();
imageUrls = new String[]{
inside_location.getPIC1(), inside_location.getPIC2(),
inside_location.getPIC3(), inside_location.getPIC4(),
inside_location.getPIC5(), inside_location.getPIC6(),
inside_location.getPIC7(), inside_location.getPIC8(),
inside_location.getPIC9(), inside_location.getPIC10(),
};
viewPagerAdapter = new ViewPagerAdapter(getBaseContext(),imageUrls);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
insideHostelImage.setAdapter(viewPagerAdapter);
}
不知道是什么问题
答案 0 :(得分:1)
您正在设置适配器,然后才能从Firebase收到任何响应。 只需修改如下代码即可。
private void loadImages(String insideHostelID) {
ImageDatabaseReference.child(insideHostelID).child("INSIDE_PICTURES").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Inside_location inside_location=dataSnapshot.getValue(Inside_location.class) ;
// Toast.makeText(hostelDetails.this,""+ inside_location.getPIC9(),Toast.LENGTH_SHORT).show();
imageUrls = new String[]{
inside_location.getPIC1(), inside_location.getPIC2(),
inside_location.getPIC3(), inside_location.getPIC4(),
inside_location.getPIC5(), inside_location.getPIC6(),
inside_location.getPIC7(), inside_location.getPIC8(),
inside_location.getPIC9(), inside_location.getPIC10(),
};
viewPagerAdapter = new ViewPagerAdapter(getBaseContext(),imageUrls);
insideHostelImage.setAdapter(viewPagerAdapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
然后在适配器类中的方法下面添加/替换。
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}