当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Monodroid ListView update problems

问题描述:

Just started with Monodroid and I'm currently working on a ListView.

I got a List added to the ArrayAdapter and i can see my first two items correctly. However when i add a third element to the list, the listview doesnt update. Even though i call notifyDataSetChanged().

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

Any clues on what i missed?

网友答案:

Found a solution myself at another forum. Somehow the ArrayAdapter does'nt take notice of list changes when using a List. Instead use a Android.Runtime.JavaList.

You can find the discussion here: http://mono-for-android.1047100.n5.nabble.com/Update-ListView-when-dataset-changes-td4757874.html

Works like a charm! :)

网友答案:

I am not entirely sure, but I think that the items are copied into the ArrayAdapter, thus what you need to do is:

la.Add(" Test3");

if you want to keep the list the same you will have to add it to list as well.

网友答案:

try this Add la.notifyDataSetInvalidated(); after la.NotifyDataSetChanged();

网友答案:

I'm not sure how much this will help anyone, but it seems to work fine in my instance.

I have a ViewModel class that keeps all the data I update within the App and trigger a "Collection Updated" action when a collection changes.

// 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 ();
        }
    }
}

I then add a handler for this event within the adapter class.

// 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;
    }
}

Within the adapter I use the collection ViewModel.SearchResults as the data context for the list view. Hope that helps and is thorough enough for everyone to understand.

网友答案:

To update the 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);
    }
分享给朋友:
您可能感兴趣的文章:
随机阅读: