使用毕加索在列表视图中加载图像
Firebase load image in listview using picasso
请看一下,我有一个片段,设置一个适配器,从 firebase 数据库中获取字符串 url 值,但没有图像加载。
我的片段:
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());
// ...
}
});
}
}
我的fragment_promo.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PromoFragment">
<ListView
android:id="@+id/listViewPromo"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</ListView>
</RelativeLayout>
list_promo.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/imageViewPromo" />
</LinearLayout>
promo.java class
每个 object 我有 3 个变量,但我只会在这种情况下使用 durl(url 下载)。
public class Promo {
private String durl;
private Boolean active;
private String timestamp;
public Promo() { //default constructor
}
public Promo(String durl, Boolean active, String timestamp) {
this.durl = durl;
this.active = active;
this.timestamp = timestamp;
}
public String getDurl() {
return durl;
}
public void setDurl(String durl) {
this.durl = durl;
}
public Boolean getActive() {
return active;
}
public void setActive(Boolean active) {
this.active = active;
}
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
}
PromoAdapter.java class
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;
}
我尝试过的:
检查浏览器中的图片url是否有效
listenPromo 也成功地获取了所有 children 图像 url(我之前测试过它以在使用 picasso 之前从数据库中获取数据)
将load(promo.getDurl())
更改为load(uri.parse(promo.getDurl()))
添加占位符和错误图片,也没有图片出现。我的片段是空白的。
请帮忙..如果代码正常,那么它应该显示 3 张图片。
感谢您的关注。
请使用此代码:
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() {
}
});
试试这个。如果这对 you.then 不起作用,请告诉我
添加promoAdapter.notifyDataSetChanged();在 onDataChange 中,它将为您工作。
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 在后台工作,所以它会先设置 Adater,然后你得到你的 response.so 只是通知适配器将为你工作。
根据您的代码,适配器不一定传递列表项。我敦促您使 适配器全局声明 并在 onDataChange[ 上检测到任何更改时添加 adapter.notifyDataSetChanged() =15=].
请看一下,我有一个片段,设置一个适配器,从 firebase 数据库中获取字符串 url 值,但没有图像加载。
我的片段:
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());
// ...
}
});
}
}
我的fragment_promo.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PromoFragment">
<ListView
android:id="@+id/listViewPromo"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</ListView>
</RelativeLayout>
list_promo.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/imageViewPromo" />
</LinearLayout>
promo.java class
每个 object 我有 3 个变量,但我只会在这种情况下使用 durl(url 下载)。
public class Promo {
private String durl;
private Boolean active;
private String timestamp;
public Promo() { //default constructor
}
public Promo(String durl, Boolean active, String timestamp) {
this.durl = durl;
this.active = active;
this.timestamp = timestamp;
}
public String getDurl() {
return durl;
}
public void setDurl(String durl) {
this.durl = durl;
}
public Boolean getActive() {
return active;
}
public void setActive(Boolean active) {
this.active = active;
}
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
}
PromoAdapter.java class
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;
}
我尝试过的:
检查浏览器中的图片url是否有效
listenPromo 也成功地获取了所有 children 图像 url(我之前测试过它以在使用 picasso 之前从数据库中获取数据)
将
load(promo.getDurl())
更改为load(uri.parse(promo.getDurl()))
添加占位符和错误图片,也没有图片出现。我的片段是空白的。
请帮忙..如果代码正常,那么它应该显示 3 张图片。
感谢您的关注。
请使用此代码:
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() {
}
});
试试这个。如果这对 you.then 不起作用,请告诉我
添加promoAdapter.notifyDataSetChanged();在 onDataChange 中,它将为您工作。
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 在后台工作,所以它会先设置 Adater,然后你得到你的 response.so 只是通知适配器将为你工作。
根据您的代码,适配器不一定传递列表项。我敦促您使 适配器全局声明 并在 onDataChange[ 上检测到任何更改时添加 adapter.notifyDataSetChanged() =15=].