我尝试从json数据设置图像,但它没有出现在我的listviewitem上

时间:2015-03-30 11:53:45

标签: android json image

这是我的json

{
contacts: [
{
id: "c200",
name: "Ravi Tamada",
email: "ravi@gmail.com",
address: "xx-xx-xxxx,x - street, x - country",
gender: "male",
image: "http://www.madam.com.tr/wp-content/uploads/2014/06/Tango-Siempre-80x80.jpg",
phone: {
mobile: "+91 0000000000",
home: "00 000000",
office: "00 000000"
}
},

我解析了这个json,我设置了这些名称,电子邮件,地址,性别项目,但我无法在布局中设置图像。

这是我的视图适配器

  @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            // brand new
            convertView = LayoutInflater.from(mContext).inflate(R.layout.film_list_item, null);
            holder = new ViewHolder();

            holder.nameLabel= (TextView) convertView.findViewById(R.id.nameLabel);
            holder.emailLabel= (TextView) convertView.findViewById(R.id.emailLabel);
            holder.genderLabel= (TextView) convertView.findViewById(R.id.genderLabel);
            holder.addressLabel= (TextView) convertView.findViewById(R.id.addressLabel);
            holder.imageId= (ImageView) convertView.findViewById(R.id.imageId);


            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }

        Film day = mFilmler[position];

        byte[] decodedString = Base64.decode(String.valueOf(holder.imageId), Base64.URL_SAFE);
        Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);


        holder.nameLabel.setText(day.getName());
        holder.emailLabel.setText(day.getEmail());
        holder.genderLabel.setText(day.getGender());
        holder.addressLabel.setText(day.getAddress());
        holder.imageId.setImageURI(Uri.parse(day.getImage()));


        return convertView;
    }

    private static class ViewHolder {
        TextView nameLabel; // public by default
        TextView emailLabel;
        TextView genderLabel;
        TextView addressLabel;
        ImageView imageId;
    }

我如何在列表视图中将此图片网址设置为图片?

1 个答案:

答案 0 :(得分:0)

最快的方式?使用像毕加索这样的图书馆。它将从提供的URL中获取图像并填充ImageView。目前您还没有加载数据。您只需为其提供数据的网址。

使用Picasso的示例适配器:

https://github.com/androidfu/Now-Playing/blob/master/app/src/main/java/com/androidfu/nowplaying/app/ui/adapters/MovieAdapter.java

要添加到app/build.gradle

中的依赖项的行
compile 'com.squareup.picasso:picasso:2.3.3'
毕加索......

Picasso.with(getContext())
                    .load(day.getImage())
                    //.placeholder(R.drawable.admit_one)
                    //.error(R.drawable.emoticon_sad)
                    .into(holder.imageId, new Callback() {
                                @Override
                                public void onSuccess() {
                                }

                                @Override
                                public void onError() {
                                }
                            }
                    );