2012-02-10 69 views
2

刚开始使用Monodroid,我目前正在开发一个ListView。 我有一个列表添加到ArrayAdapter,我可以看到我的前两个项目正确。但是,当我添加第三个元素到列表中时,listview不会更新。即使我调用notifyDataSetChanged()。Monodroid ListView更新问题

private ArrayAdapter<string> la; 
private ListView list; 
private List<String> dayData = new List<String>(); 

protected override void OnCreate(Bundle bundle) 
     { 
      base.OnCreate(bundle); 
      this.SetContentView(Resource.Layout.TestLayout); 


      dayData.Add(" Test"); 
      dayData.Add(" Test2"); // Theese two elements shows up fine 

      list = this.FindViewById<ListView>(Resource.Id.menuList); 
      la = new ArrayAdapter<string>(this, Resource.Layout.list_item, dayData); 
      list.Adapter = la; 
      list.TextFilterEnabled = true; 

      dayData.Add(" Test3"); // This one is not shown 
      la.NotifyDataSetChanged(); 


     } // OnCreate 

我错过了什么线索?

回答

0

我不能完全肯定,但我认为,项目复制到ArrayAdapter,所以你需要做的是:

la.Add(" Test3"); 

,如果你想保持列表中的相同,你将不得不将其添加到list

0

试试这个添加la.notifyDataSetInvalidated();在la.NotifyDataSetChanged()之后;

0

我不知道这会帮助任何人多少,但它似乎在我的例子中工作正常。

我有一个ViewModel类,用于保存在应用程序内更新的所有数据,并在集合更改时触发“集合更新”操作。

// All within ViewModel.cs 

private Action SearchResultsUpdated; 

private List<SearchResult> m_oSearchResults; 

Public List<SearchResult> SearchResults 
{ 
    get 
    { 
     if (m_oSearchResults == null) 
      m_oSearchResults = new List<SearchResult>(); 
     return m_oSearchResults; 
    } 
    set 
    { 
     if (value != m_oSearchResults) 
     { 
      m_oSearchResults = value; 
      // 
      // Fire update event 
      if (SearchResultsUpdated != null) 
       SearchResultsUpdated(); 
     } 
    } 
} 

然后,我在适配器类中为此事件添加一个处理程序。

// All within SearchResultsAdapter.cs 

public class SearchResultsAdapter : BaseAdapter<SearchResult> 
{ 
. 
. 
    // Constructor 
    public SearchResultsAdapter (Activity oContext) 
     : base() 
    { 
     // Add handler for list refresh 
     ViewModel.SearchResultsUpdated += NotifyDataSetChanged; 
     // 
     m_oContext = oContext; 
    } 
} 

在适配器中,我使用集合ViewModel.SearchResults作为列表视图的数据上下文。希望帮助和足够让每个人都明白。

0

要更新的ListView

private ListView lvAnuncios = null; 

    protected override void OnCreate(Bundle bundle) 
    { 
     base.OnCreate(bundle); 

     this.lvAnuncios = this.FindViewById<ListView>(Resource.Id.MisAnuncios_lvAnuncios); 
    } 

    private void ReloadListView() 
    { 
     if (this.lvAnuncios.Adapter != null) 
     { 
      this.lvAnuncios.Adapter.Dispose(); 
      this.lvAnuncios.Adapter = null; 
     } 

             //Class that inherits de ArrayAdapter 
     this.lvAnuncios.Adapter = new adAnuncio(this, Resource.Layout.FilaListViewAnuncio, csVariable.objUsr.lstAnuncios); 
    }