You could save yourself a lot of headache and gain benefits like caching and using a placeholder image by using the Picasso library. 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. And you can lose a whole tonne of boilerplate code.
Unable to Display Image from XML Feed - holder.imageView.setImageURI(cmd.getImageUrl()); returns null
-
10-10-2022 - |
Pergunta
I'm attempting to create a thumbnail from a URL obtained from an XML feed. 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. 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.
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;
}
}
Solução
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow