Xamarin.andoid

Yusuf Atasever 1 Reputation point
2021-04-04T09:53:53.057+00:00

How can l update a list on oncreate with a method ?

Developer technologies .NET Xamarin
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. JarvanZhang 23,971 Reputation points
    2021-04-05T05:40:37.747+00:00

    How can l update a list on oncreate with a method ?

    Hi, YusufAtasever. Could you post more details about the requirement? Do you mean updating the listView? If so, change the data list and then call the NotifyDataSetChanged command.

    public class MainActivity : AppCompatActivity
    {
        List<CustomModel> list;
        CustomAdapter adapter;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.activity_main);
    
            list = new List<CustomModel>();
            adapter = new CustomAdapter(list);
    
            ListView listView = FindViewById<ListView>(Resource.Id.listview_);
            listView.SetAdapter(adapter);
    
            var btn = FindViewById<Button>(Resource.Id.btn);
            btn.Click += Btn_Click;
        }
    
        private void Btn_Click(object sender, EventArgs e)
        {
            list.Add(xx);
            adapter.NotifyDataSetChanged();
        }
    }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.