C# Monodroid列表视图更新问题

C# Monodroid列表视图更新问题,c#,android,listview,xamarin.android,C#,Android,Listview,Xamarin.android,刚从Monodroid开始,我现在正在开发一个ListView。 我在ArrayAdapter中添加了一个列表,可以正确地看到前两项。但是,当我向列表中添加第三个元素时,listview不会更新。即使我调用notifyDataSetChanged() private ArrayAdapter la; 私有列表视图列表; 私有列表日数据=新列表(); 创建时受保护的覆盖无效(捆绑包) { base.OnCreate(bundle); SetContentView(Resource.Layout.T

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

private ArrayAdapter la;
私有列表视图列表;
私有列表日数据=新列表();
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.TestLayout);
dayData.添加(“测试”);
dayData.Add(“Test2”);//这两个元素显示得很好
list=this.findviewbyd(Resource.Id.menuList);
la=新阵列适配器(此,Resource.Layout.list_项,dayData);
list.Adapter=la;
list.TextFilterEnabled=true;
dayData.Add(“Test3”);//不显示这个
la.NotifyDataSetChanged();
}//OnCreate

我错过了什么,有什么线索吗

我不完全确定,但我认为项目已复制到
阵列适配器中,因此您需要做的是:

la.Add(" Test3");

如果要保持列表不变,还必须将其添加到
列表中。

尝试此添加la.notifyDataSetValidated();在la.NotifyDataSetChanged()之后

我自己在另一个论坛上找到了一个解决方案。不知何故,ArrayAdapter在使用列表时没有注意到列表的更改。而是使用Android.Runtime.JavaList

您可以在此处找到讨论:


工作起来很有魅力!:)

我不确定这对任何人都有多大帮助,但在我的例子中似乎效果不错

我有一个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 ();
        }
    }
}
//都在ViewModel.cs中
私人行动搜索结果更新;
私有列表搜索结果;
公开列表搜索结果
{
得到
{
if(m_oSearchResults==null)
m_oSearchResults=新列表();
返回m_搜索结果;
}
设置
{
if(值!=m_)
{
m_oSearchResults=值;
//
//火灾更新事件
如果(SearchResultUpdate!=null)
SearchResultsUpdate();
}
}
}
然后,我在适配器类中添加此事件的处理程序

// 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;
    }
}
//SearchResultsAdapter.cs中的所有内容
公共类SearchResultsAdapter:BaseAdapter
{
.
.
//建造师
公共搜索结果适配器(活动oContext)
:base()
{
//添加列表刷新处理程序
ViewModel.SearchResultsUpdate+=NotifyDataSetChanged;
//
m_oContext=oContext;
}
}
在适配器中,我使用集合ViewModel.SearchResults作为列表视图的数据上下文。希望对大家有所帮助,并且足够全面,大家都能理解。

更新列表视图

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);
    }
private ListView lvAnuncios=null;
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
this.lvAnuncios=this.FindViewById(Resource.Id.MisAnuncios\u lvAnuncios);
}
私有void重载列表视图()
{
if(this.lvAnuncios.Adapter!=null)
{
这个.lvAnuncios.Adapter.Dispose();
this.lvAnuncios.Adapter=null;
}
//继承de ArrayAdapter的类
this.lvAnuncios.Adapter=new adAnuncio(this,Resource.Layout.FilaListViewAnuncio,csVariable.objUsr.lstAnuncios);
}
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);
    }