Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Update/Refresh adapter

Xammy10Xammy10 Member ✭✭✭
edited June 2018 in Xamarin.Android

I'm trying to create a listview that can update items dynamically.

Here is my code:

            list_item.ItemClick += async delegate (object sender, AdapterView.ItemClickEventArgs e)
            {
                int num = 1;
                var items = new Model
                {
                    api1 = list_model[e.Position].var1,
                    api2 = list_model[e.Position].var2,
                    api3 = list_model[e.Position].var3,
                    api4 = list_model[e.Position].var4,
                    api5 = list_model[e.Position].var5 - num,
                };

                var client = new HttpClient();
                var data = JsonConvert.SerializeObject(items);
                var content = new StringContent(data, Encoding.UTF8, "application/json");
                var response = await client.PostAsync("http://192.168.1.69:4321/api/api/put/" + list_model[e.Position].apiID, content);
                Toast.MakeText(this, response.StatusCode.ToString(), ToastLength.Short).Show();

                //Clearing the adapter
                list_model.Clear();
                //Calling the updated adapter
                list_item.Adapter = lop;
                lop.NotifyDataSetChanged();

            };

In this code, I push an update request on the api, clearing the current list view data, and getting again the data that is already updated.
But its not working in my case, idk why but I might miss some codes or i skips some steps.
I also try "RunOnUiThread(()=>)" but nothing happened.

Thanks in advance :)

Answers

  • BillyLiuBillyLiu Member, Xamarin Team Xamurai

    @Enohara
    What is list_model? Is it a list that holds the view data?
    If so, you have only modified the value of list_model. You need to modify the adapter.
    For example:

            ListView listView = FindViewById<ListView>(Resource.Id.listView1);
            List<string> countries = new List<string>() {
                "Afghanistan","Albania","Algeria","American Samoa","Andorra",
                "Angola","Anguilla","Antarctica","Antigua and Barbuda","Argentina",
                "Armenia","Aruba","Australia","Austria","Azerbaijan"
            };
    
            ArrayAdapter<string> lstAdapter = new ArrayAdapter<string> (this, Resource.Layout.layout1, countries);
            listView.Adapter = lstAdapter;
            listView.ItemClick +=  delegate (object sender, AdapterView.ItemClickEventArgs e)
            {
                lstAdapter.Clear();
                countries[e.Position] = "AAA";
                lstAdapter.AddAll(countries);
            };
    

    And the result is:

Sign In or Register to comment.