monodroid CustomAdapter NotifyDataSetChanged无法正常工作

时间:2012-07-24 12:35:18

标签: xamarin.android adapter

我的问题是((BaseAdapter)listAdapter).NotifyDataSetChanged();不起作用。我每隔X分钟使用计时器获取新闻。我已经调试并看到了dataadapter的更改,但在屏幕上没有任何变化。 如果我在屏幕上看到缩放新数据。 listview点击事件未在加载时触发。更改listadapter后,执行缩放单击事件。

你可以帮我吗?

[Activity (Label = "SportNews")]            
public class SportNews : Activity
{
    RssFeedItemAdapter listAdapter;
    JavaList<FeedItem> items = new JavaList<FeedItem>();
    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        SetContentView(Resource.Layout.list_view);

        items = RssHelper.Instance.SportNews();

        //ListAdapter = new ArrayAdapter<string> (this, Resource.Layout.list_item, items);
        listAdapter = new RssFeedItemAdapter(this,items);

        ListView listView = FindViewById<ListView>(Resource.Id.listView);

        listView.Adapter = listAdapter;

        ((BaseAdapter)listAdapter).NotifyDataSetChanged();

        listView.ItemClick += new EventHandler<AdapterView.ItemClickEventArgs>(listView_ItemClick);

        Timer timer = new Timer(PushNews, null, TimeSpan.Zero, TimeSpan.FromMinutes(15)); // get time from user preferences...
    }
    private void PushNews(object state)
    {
        items = RssHelper.Instance.SportNews();
        JavaList<FeedItem> feeds = new JavaList<FeedItem>();
        feeds.Add(new FeedItem()
        {
         NewsPicture = "http://www.website.com/pic/test.jpg",
         NewsTitle = "Test news",
         NewsUrl = "http://www.google.com",
        });
        listAdapter = new RssFeedItemAdapter(this,feeds);
        ((BaseAdapter)this.listAdapter).NotifyDataSetChanged();
    }

    void listView_ItemClick(object sender, Android.Widget.AdapterView.ItemClickEventArgs e)
    {
        //Get our item from the list adapter
        var item = this.listAdapter.GetItemAtPosition(e.Position);

        var intent = new Intent();
        intent.SetClass(this, typeof(NewsView));
        intent.PutExtra("Url", item.NewsUrl);
        StartActivity(intent);
    }
}

0 个答案:

没有答案