Android 使用毕加索在listview中加载Firebase图像
请看,我有一个片段,设置了一个适配器,从firebase数据库中获取字符串url值,但没有加载图像 我的片段:Android 使用毕加索在listview中加载Firebase图像,android,listview,firebase,firebase-realtime-database,picasso,Android,Listview,Firebase,Firebase Realtime Database,Picasso,请看,我有一个片段,设置了一个适配器,从firebase数据库中获取字符串url值,但没有加载图像 我的片段: public class PromoFragment extends Fragment { private DatabaseReference mDatabase; List<Promo> promoList = new ArrayList<>(); ListView listViewPromo; public PromoFragment() {
public class PromoFragment extends Fragment {
private DatabaseReference mDatabase;
List<Promo> promoList = new ArrayList<>();
ListView listViewPromo;
public PromoFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDatabase = FirebaseDatabase.getInstance().getReference();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_promo, container, false);
listViewPromo = (ListView) view.findViewById(R.id.listViewPromo);
listenPromo(); //here i load my data, i currently have 3 childs, each child has imageUrl
PromoAdapter promoAdapter = new PromoAdapter(getActivity().getApplicationContext(), R.layout.list_promo, promoList);
listViewPromo.setAdapter(promoAdapter);
return view;
}
private void listenPromo() {
mDatabase.child("Promo").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get Post object and use the values to update the UI
// MainActivity.currUser = dataSnapshot.getValue(User.class);
for (DataSnapshot dsp : dataSnapshot.getChildren()) {
promoList.add(dsp.getValue(Promo.class)); //add result into array list
}
// ...
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting User Class data failed...
Log.w("Canceled", "loadUserData:onCancelled", databaseError.toException());
// ...
}
});
}
}
PromoAdapter.java类
public class PromoAdapter extends ArrayAdapter<Promo> {
Context context;
List<Promo> myList;
public PromoAdapter(Context context, int resource, List<Promo> objects) {
super(context, resource, objects);
this.context = context;
this.myList = objects;
}
@Override
public int getCount() {
if(myList != null)
return myList.size();
return 0;
}
@Override
public Promo getItem(int position) {
if(myList != null)
return myList.get(position);
return null;
}
@Override
public long getItemId(int position) {
if(myList != null)
return myList.get(position).hashCode();
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder;
if (convertView == null){
//we need a new holder to hold the structure of the cell
holder = new Holder();
//get the XML inflation service
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_promo, null);
holder.imageView = (ImageView)convertView.findViewById(R.id.imageViewPromo);
convertView.setTag(holder);
}else{
holder = (Holder)convertView.getTag();
}
Promo promo = getItem(position);
Picasso.with(context)
.load(promo.getDurl())
.placeholder(R.drawable.coin25)
.error(R.drawable.hnbspic)
.into(holder.imageView);
return convertView;
}
private class Holder{
ImageView imageView;
}
公共类PromoAdapter扩展了ArrayAdapter{
语境;
列出我的清单;
公共PromoAdapter(上下文上下文、int资源、列表对象){
超级(上下文、资源、对象);
this.context=上下文;
this.myList=对象;
}
@凌驾
public int getCount(){
如果(myList!=null)
返回myList.size();
返回0;
}
@凌驾
公共宣传项目(内部位置){
如果(myList!=null)
返回myList.get(位置);
返回null;
}
@凌驾
公共长getItemId(int位置){
如果(myList!=null)
返回myList.get(position.hashCode();
返回0;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
持有人;
if(convertView==null){
//我们需要一个新的支架来固定细胞的结构
保持架=新保持架();
//获取XML膨胀服务
LayoutFlater充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
convertView=充气机充气(R.layout.list\u促销,空);
holder.imageView=(imageView)convertView.findViewById(R.id.imageViewPromo);
convertView.setTag(支架);
}否则{
holder=(holder)convertView.getTag();
}
Promo-Promo=getItem(位置);
毕加索。与(上下文)
.load(promo.getDurl())
.占位符(R.drawable.coin25)
.错误(R.drawable.hnbspic)
.插入(支架.图像视图);
返回视图;
}
私人阶级持有者{
图像视图图像视图;
}
我所尝试的:
load(promo.getDurl())
更改为load(uri.parse(promo.getDurl())
Transformation blurTransformation = new Transformation() {
@Override
public Bitmap transform(Bitmap source) {
Bitmap blurred = Blur.fastblur(context, source, 15);
source.recycle();
return blurred;
}
@Override
public String key() {
return "blur()";
}
};
Picasso.with(context)
.load(R.drawable.coin25)
// thumbnail url goes here
.placeholder(R.drawable.coin25)
.transform(blurTransformation)
.memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
.into(holder.imageView, new Callback() {
@Override
public void onSuccess() {
Picasso.with(context)
.load(promo.getDurl()) // image url goes here
.fit()
.centerCrop()
.error(R.drawable.hnbspic)
.placeholder(holder.imageView.getDrawable())
.fit()
.into(holder.imageView);
}
@Override
public void onError() {
}
});
尝试使用此选项。如果此选项不适用于您。请让我知道在onDataChange中添加promoAdapter.notifyDataSetChanged();将适用于您
mDatabase.child("Promo").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get Post object and use the values to update the UI
// MainActivity.currUser = dataSnapshot.getValue(User.class);
for (DataSnapshot dsp : dataSnapshot.getChildren()) {
promoList.add(dsp.getValue(Promo.class)); //add result into array list
}
promoAdapter.notifyDataSetChanged();
// ...
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting User Class data failed...
Log.w("Canceled", "loadUserData:onCancelled", databaseError.toException());
// ...
}
});
addValueEventListener在后台工作,因此它将首先设置适配器,然后您将得到响应。因此,只需通知适配器就可以了。根据您的代码,适配器不一定要传递列表项。我敦促您使适配器全局声明并添加适配器。notifyDataSetChanged()当在onDataChange上检测到任何更改时,问题可能是刷新适配器,它会先设置适配器,然后获取图像url。在onDataChange中获得成功响应后,只需通知适配器imageurl应该在数组中,然后我再将其设置为adapter。addValueEventListener在后台工作,以便代码将我在Background中设置它,然后首先移动到setAdapter。你能显示你用Piccaso设置图像的完整代码吗?整个代码都在上面,在promoAdapter类中哦,你说得对,我把promoAdapter.notifyDataSetChanged()放进去了;在我的监听器中,它是有效的..,请写下答案让我接受它。对不起,不是listviewPromo.notifyDataSetChanged(),而是promoAdapter.notifyDataSetChanged();谢谢
mDatabase.child("Promo").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get Post object and use the values to update the UI
// MainActivity.currUser = dataSnapshot.getValue(User.class);
for (DataSnapshot dsp : dataSnapshot.getChildren()) {
promoList.add(dsp.getValue(Promo.class)); //add result into array list
}
promoAdapter.notifyDataSetChanged();
// ...
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting User Class data failed...
Log.w("Canceled", "loadUserData:onCancelled", databaseError.toException());
// ...
}
});
Transformation blurTransformation = new Transformation() {
@Override
public Bitmap transform(Bitmap source) {
Bitmap blurred = Blur.fastblur(context, source, 15);
source.recycle();
return blurred;
}
@Override
public String key() {
return "blur()";
}
};
Picasso.with(context)
.load(R.drawable.coin25)
// thumbnail url goes here
.placeholder(R.drawable.coin25)
.transform(blurTransformation)
.memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
.into(holder.imageView, new Callback() {
@Override
public void onSuccess() {
Picasso.with(context)
.load(promo.getDurl()) // image url goes here
.fit()
.centerCrop()
.error(R.drawable.hnbspic)
.placeholder(holder.imageView.getDrawable())
.fit()
.into(holder.imageView);
}
@Override
public void onError() {
}
});
mDatabase.child("Promo").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get Post object and use the values to update the UI
// MainActivity.currUser = dataSnapshot.getValue(User.class);
for (DataSnapshot dsp : dataSnapshot.getChildren()) {
promoList.add(dsp.getValue(Promo.class)); //add result into array list
}
promoAdapter.notifyDataSetChanged();
// ...
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting User Class data failed...
Log.w("Canceled", "loadUserData:onCancelled", databaseError.toException());
// ...
}
});