无法从XML Feed显示图像 - holder.imageView.setImageURI(cmd.getImageUrl());返回null

时间:2014-02-11 22:40:37

标签: java android xml-parsing uri thumbnails

我正在尝试从XML Feed中获取的网址创建缩略图。我的问题是当我到达这条线时:

public  Uri getImageUrl() {
    return imageurl;
}

public void setImageUrl(Uri imageurl) {
    this.imageurl = imageurl;
}

它返回null。我不确定为什么,但自从我实现调用拉缩略图 - 我无法显示它们 - 虽然我没有任何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;

        }

    }

1 个答案:

答案 0 :(得分:0)

使用Picasso库,您可以节省很多麻烦并获得缓存和使用占位符图像等好处。在单行(Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);)中,您可以告诉URL在下载时加载到ImageView中,缓存该图像,并且通过一次方法调用,您可以设置占位符drawable。你可能会丢失一整吨的样板代码。