question

YusufAtasever-6378 avatar image
0 Votes"
YusufAtasever-6378 asked RobCaplan edited

Xamarin.andoid

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

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered

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();
    }
}
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.