[英]Unable to Display Image from XML Feed - holder.imageView.setImageURI(cmd.getImageUrl()); returns null
I'm attempting to create a thumbnail from a URL obtained from an XML feed. 我试图根据从XML提要获得的URL创建缩略图。 My problem is when I reach the line: 我的问题是到达线路时:
public Uri getImageUrl() {
return imageurl;
}
public void setImageUrl(Uri imageurl) {
this.imageurl = imageurl;
}
it is returning null. 它返回null。 I'm not sure exactly why but ever since I implemented the call to pull the thumbnail - I have not been able to display them - although I do not have any nullpointer exceptions and the code seems to execute without error. 我不确定为什么会这样,但是自从我实现了拉缩略图的调用以来-我一直无法显示它们-尽管我没有任何nullpointer异常,并且代码似乎可以正确执行。
public class CustomListViewAdapter extends ArrayAdapter<Cmd> {
Activity context;
List<Cmd> videos;
public CustomListViewAdapter(Activity context, List<Cmd> videos) {
super(context, R.layout.list_item2, videos);
this.context = context;
this.videos = videos;
}
/* private view holder class */
private class ViewHolder {
ImageView imageView;
TextView txtSuccess;
TextView txtCmd;
TextView txtPrice;
}
public void run(Cmd cmd) {
Intent intent = new Intent(context, ViewVideo.class);
String txt=cmd.getVideoURL();
intent.putExtra("videofilename", txt);
context.startActivity(intent);
}
public Cmd getItem(int position) {
return videos.get(position);
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item2, null);
holder = new ViewHolder();
holder.txtSuccess = (TextView) convertView
.findViewById(R.id.success);
holder.txtCmd = (TextView) convertView.findViewById(R.id.cmd);
holder.txtPrice = (TextView) convertView.findViewById(R.id.price);
holder.imageView = (ImageView) convertView
.findViewById(R.id.thumbnail);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final Cmd cmd = (Cmd) getItem(position);
holder.txtSuccess.setText(cmd.getVideoName());
holder.txtSuccess.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
run(cmd);
}
});
holder.txtCmd.setText(cmd.getCmd());
holder.txtCmd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
run(cmd);
}
});
// holder.txtPrice.setText(cmd.getVideoURL());
// holder.txtPrice.setOnClickListener(new View.OnClickListener() {
//
// public void onClick(View v) {
// run(cmd);
// }
// });
holder.imageView.setImageURI(cmd.getImageUrl());
holder.imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
run(cmd);
}
});
return convertView;
}
}
You could save yourself a lot of headache and gain benefits like caching and using a placeholder image by using the Picasso library. 您可以省去很多麻烦,并通过使用Picasso库获得诸如缓存和使用占位符图像之类的好处。 In a single line ( Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
) you can tell a URL to load into an ImageView when it has downloaded, have that image cached and, with one more method call, you can set a placeholder drawable. 在一行中( Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
),您可以告诉URL在具有以下条件时加载到ImageView中下载,缓存该图片,然后再调用一个方法,即可设置一个可绘制的占位符。 And you can lose a whole tonne of boilerplate code. 您可能会损失整吨的样板代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.