ListView xamarin(monodroid)中的图像

时间:2016-08-30 06:12:07

标签: listview xamarin xamarin.android

public class myAdapter : BaseAdapter<RootObject>
{
    readonly Activity context;
    List<RootObject> objects = new List<RootObject>();
    Bitmap bm;
    View view;
    ImageView iv;

    public myAdapter(Activity context, List<RootObject> objects)
    {
        this.context = context;
        this.objects = objects;
    }

    public override RootObject this[int position]
    {
        get { return objects[position]; }
    }

    public override int Count
    {
        get { return objects.Count; }
    }

    public override long GetItemId(int position)
    {
        return position;
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        RootObject obj = objects[position];

        view = convertView;

        if (view == null) // no view to re-use, create new
        {
            view = context.LayoutInflater.Inflate(Resource.Layout.listItem, null);
        }

        WebClient wc = new WebClient();
        wc.DownloadDataCompleted += new DownloadDataCompletedEventHandler(web_DownloadDataCompleted);
        wc.DownloadDataAsync(new Uri(obj.imageUrl));

        iv = view.FindViewById<ImageView>(Resource.Id.Image);

        view.FindViewById<TextView>(Resource.Id.tvName).Text = obj.name;
        return view;
    }


    void web_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
    {
        if (e.Error != null)
        {
        }
        else
        {
            Bitmap bm = BitmapFactory.DecodeByteArray(e.Result, 0, e.Result.Length);
            runonuithread(() =>
            {
                iv.setimagebitmap(bm);
          });
        }
    }

`我正在使用xamarin(monodroid)中的listView。我的列表视图有一个自定义适配器。每个列表项都包含图像和文本视图。图像来自网址。在我的下载图像功能中,我使用的是downloadDataAsync。但是在下载图像之前,所有其他工作都已完成,并且我的适配器的GetView方法返回视图,没有图像,只有文本。返回视图后是否有任何方法可以更新该图像资源。我实际上想先显示所有textViews,并继续下载图片。 提前致谢

1 个答案:

答案 0 :(得分:0)

AQuery库解决了我的问题。希望它也适用于其他人。