Ah, I found out that you have to instantiate a new adapter and set it
to ListAdapter before NotifyDataSetChanged does anything. So now my
RunOnUiThread should look like:
RunOnUiThread(() =>
{
ListAdapter = new ArrayAdapter<string>(this,
Resource.Layout.list_item, list);
((BaseAdapter)this.ListAdapter).NotifyDataSetChanged();
});
This works and updates the ListView with the new values. Excellent,
thank you Greg.
On Thu, Sep 1, 2011 at 2:36 PM, Tomasz Cielecki <[email protected]> wrote:
> Ok, so I tried to cast the adapter, but nothing happens. Here is my code:
>
> using System;
> using System.Collections.Generic;
> using System.Linq;
> using System.Text;
>
> using Android.App;
> using Android.Content;
> using Android.OS;
> using Android.Runtime;
> using Android.Views;
> using Android.Widget;
>
> using LoadSiteList;
> using Android.Util;
> using System.Threading;
>
> namespace MonoActivityFun
> {
> [Activity(Label = "My Activity")]
> public class MyListActivity : ListActivity
> {
> List<String> list;
> private ProgressDialog d;
> string tag = "TEST";
> protected override void OnCreate(Bundle bundle)
> {
> base.OnCreate(bundle);
> list = new List<String>();
> MyModel m = new MyModel();
>
> m.Items.CollectionChanged += new
> System.Collections.Specialized.NotifyCollectionChangedEventHandler(Items_CollectionChanged);
>
> ThreadPool.QueueUserWorkItem(o => m.Load());
>
> if (list.Count == 0)
> {
> list.Add("No data present");
> }
> ListAdapter = new ArrayAdapter<string>(this,
> Resource.Layout.list_item, list);
> ListView.TextFilterEnabled = true;
>
> SetContentView(ListView);
>
> ListView.ItemClick += delegate(object sender, ItemEventArgs args)
> {
> // When clicked, show a toast with the TextView text
> Toast.MakeText(Application,
> ((TextView)args.View).Text, ToastLength.Short).Show();
> };
> }
>
> void Items_CollectionChanged(object sender,
> System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
> {
> if (e.NewItems != null)
> {
> Log.Warn(tag, "MyListActivity: e.NewItems count " +
> e.NewItems.Count.ToString());
> foreach (object s in e.NewItems)
> {
> Log.Warn(tag, "MyListActivity:
> Items_CollectionChanged: " + s.ToString());
> list.Add(s.ToString());
> if (list.Contains("No data present")) {
> list.Remove("No data present");
> }
> }
> RunOnUiThread(() =>
> {
> ((BaseAdapter)this.ListAdapter).NotifyDataSetChanged();
> });
> }
> }
> }
> }
>
> I know that this code enters Items_Collection changed as i can easily
> output the things being added to list.
>
> I have tried running it on the UI thread and not, both fail to update
> the ListView with new data.
>
> On Thu, Sep 1, 2011 at 2:07 PM, Greg Shackles <[email protected]> wrote:
>> Adapters generally all inherit from BaseAdapter so they'll have access to
>> the method (this is exactly the same as the Java side, for what it's worth).
>> I don't know what your code looks like, but my guess is that you're doing
>> something along the lines of:
>> var view = FindViewById<ListView>(Resource.Id.List);
>> view.Adapter = new ArrayAdapter(.....); // or some other kind of adapter
>> view.Adapter.NotifyDataSetChanged();
>> The last line won't compile since the Adapter property on ListView is of
>> type IListAdapter
>> (http://docs.mono-android.net/index.aspx?link=P%3aAndroid.Widget.ListView.Adapter).
>> You can either cast the adapter when you want to access those methods:
>> ((ArrayAdapter)view.Adapter).NotifyDataSetChanged();
>> Or you can keep a reference to the object with its type when you create it
>> (this is the approach I'd prefer):
>> var adapter = new ArrayAdapter(......);
>> view.Adapter = adapter;
>> adapter.NotifyDataSetChanged();
>>
>> On Thu, Sep 1, 2011 at 7:54 AM, Tomasz Cielecki <[email protected]>
>> wrote:
>>>
>>> So that means I have to cast the ListAdapter to BaseAdapter to invoke
>>> that method?
>>>
>>> On Thu, Sep 1, 2011 at 1:35 PM, Greg Shackles <[email protected]> wrote:
>>> > That method is available as part of Mono for
>>> >
>>> > Android: http://docs.mono-android.net/index.aspx?link=M%3aAndroid.Widget.BaseAdapter.NotifyDataSetChanged
>>> >
>>> > On Thu, Sep 1, 2011 at 6:48 AM, Tomasz Cielecki <[email protected]>
>>> > wrote:
>>> >>
>>> >> Hello there,
>>> >>
>>> >> I have a asynchronous method loading data in the background, when that
>>> >> is finished I want to update my ListView with the data this method has
>>> >> loaded into an ObservableCollection. It seems that normally when
>>> >> coding java you can invoke the method notifyDataSetChanged on the
>>> >> ListAdapter, but this method seems to be missing in MonoDroid.
>>> >>
>>> >> So my question is, how do I update my ListView with this new data?
>>> >>
>>> >> --
>>> >> Med Venlig Hilsen / With Best Regards
>>> >> Tomasz Cielecki
>>> >> http://ostebaronen.dk
>>> >> _______________________________________________
>>> >> Monodroid mailing list
>>> >> [email protected]
>>> >>
>>> >> UNSUBSCRIBE INFORMATION:
>>> >> http://lists.ximian.com/mailman/listinfo/monodroid
>>> >
>>> >
>>> > _______________________________________________
>>> > Monodroid mailing list
>>> > [email protected]
>>> >
>>> > UNSUBSCRIBE INFORMATION:
>>> > http://lists.ximian.com/mailman/listinfo/monodroid
>>> >
>>> >
>>>
>>>
>>>
>>> --
>>> Med Venlig Hilsen / With Best Regards
>>> Tomasz Cielecki
>>> http://ostebaronen.dk
>>> _______________________________________________
>>> Monodroid mailing list
>>> [email protected]
>>>
>>> UNSUBSCRIBE INFORMATION:
>>> http://lists.ximian.com/mailman/listinfo/monodroid
>>
>>
>> _______________________________________________
>> Monodroid mailing list
>> [email protected]
>>
>> UNSUBSCRIBE INFORMATION:
>> http://lists.ximian.com/mailman/listinfo/monodroid
>>
>>
>
>
>
> --
> Med Venlig Hilsen / With Best Regards
> Tomasz Cielecki
> http://ostebaronen.dk
>
--
Med Venlig Hilsen / With Best Regards
Tomasz Cielecki
http://ostebaronen.dk
_______________________________________________
Monodroid mailing list
[email protected]
UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid