Note that there are some explanatory texts on larger screens.

plurals
  1. POAsynchronous image loading from url to populate listview
    primarykey
    data
    text
    <p>I'm currently experiencing a little trouble with async image loading to populate "infinite" list view(loading data from url and storing in custom adapter). I found <a href="https://github.com/Redth/MonoDroid.UrlImageViewHelper" rel="nofollow">this</a> extension methods to ImageView, which basically is port to <a href="https://github.com/koush/UrlImageViewHelper" rel="nofollow">koush/UrlImageViewHelper</a>, but the original was updated a lot, and the port is not being updated for 9 moths now... When I'm populating ImageView in my custom adapter, everything is seems to work fine and fast (I'm downloading small images from pretty fast server), but after approximately 200 images (each 15-20 kb in size) the downloading is stopping. I assume that the problem is in the images caching, but even by using the "Cleanup" method provided in the extension, nothing is changing. Does anyone knows how to fix that, or have better solution to that task? I've tried to create my own "Queue", but it has another sort of problem - when scrolling the listview, any new ImageView already have source image, that changing to the correct one, but with fast scrolling this is looking very bad. Here is my code:</p> <h2>GetView in my custom list adapter</h2> <pre><code>public override View GetView(int position, View convertView, ViewGroup parent) { //Populating the adapter with new items if (position &gt;= this._items.Count - 1) ThreadPool.QueueUserWorkItem(o =&gt; this.LoadPage()); var item = this._items[position]; View view = convertView; if (view == null) view = this._context.LayoutInflater.Inflate(Resource.Layout.CustomRowView, null); var imgUrl = string.Format(@"http://myserver.com/?action={0}&amp;url={1}", "get.thumbnail", item.Image.Url); //Here is the image loading row view.FindViewById&lt;ImageView&gt;(Resource.Id.imageView1).SetUrlDrawable(imgUrl); view.FindViewById&lt;TextView&gt;(Resource.Id.txtTitle).Text = item.Title; view.FindViewById&lt;TextView&gt;(Resource.Id.txtYear).Text = item.Information.Year; view.FindViewById&lt;TextView&gt;(Resource.Id.txtGenre).Text = item.Information.Genre; view.FindViewById&lt;TextView&gt;(Resource.Id.txtGrade).Text = item.Rating.Grade; view.FindViewById&lt;TextView&gt;(Resource.Id.txtVotes).Text = string.Format("( {0} )", item.Rating.Votes); return view; } </code></pre> <h2>LoadPage function</h2> <pre><code>public void LoadPage() { OnUpdateAnimeListStart(); //Event to display loading message try { this._items.FillListFromUrl(string.Format("http://myserver.com/page/{0}/", this._page)); } catch(Exception ex) { _context.RunOnUiThread(() =&gt; { new AlertDialog.Builder(_context) .SetPositiveButton("Ok", (sender, args) =&gt; { Intent blankIntent = new Intent(); blankIntent.SetFlags(ActivityFlags.ClearTop); int intPID = Android.OS.Process.MyPid(); Android.OS.Process.KillProcess(intPID); }) .SetMessage(ex.Message) .SetTitle("Error!") .Show(); }); } this._page++; _context.RunOnUiThread(() =&gt; { _context.FindViewById&lt;ListView&gt;(Resource.Id.listView).InvalidateViews(); }); OnUpdateAnimeListEnd(); //Event to hide loading message } </code></pre> <hr> <h2>And this is my alternative "Queue", that I talked about</h2> <pre><code> public class ImageDownloader { private List&lt;ImageView&gt; _queueImageViews; private List&lt;string&gt; _queueImageUrls; private Activity _context; public ImageDownloader(Activity context) { this._context = context; this._queueImageViews = new List&lt;ImageView&gt;(); this._queueImageUrls = new List&lt;string&gt;(); } public void DownloadImage(ImageView imgView, string url) { this._queueImageViews.Add(imgView); this._queueImageUrls.Add(url); if (this._queueImageViews.Count == 1) this._startJob(); } private void _startJob() { WebClient web = new WebClient(); web.DownloadDataCompleted += new DownloadDataCompletedEventHandler(web_DownloadDataCompleted); web.DownloadDataAsync(new Uri(this._queueImageUrls[0])); } private void _removeFromeQueue(int index = 0) { this._queueImageUrls.Remove(this._queueImageUrls[index]); this._queueImageViews.Remove(this._queueImageViews[index]); } void web_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e) { ImageView v = this._queueImageViews[0]; this._context.RunOnUiThread(() =&gt; { Bitmap bm = BitmapFactory.DecodeByteArray(e.Result, 0, e.Result.Length); v.SetImageBitmap(bm); }); this._removeFromeQueue(); if (this._queueImageViews.Count &gt; 0) this._startJob(); } } </code></pre> <p>Thank you in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload