Forum Xamarin.Forms
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 one value in Binding

ganesh96ganesh96 Member ✭✭✭

I have a list which is created using Binding. I have created a demo project to explain my problem better.

Now I want to update only one item in this list, based on another item. So for example: I want to update the value "Text2", then the code should now that Text1 and Text2 are a pair, so that it knows which value needs to be updated.

I hope it clear what I mean...

The demo project can be found in the attachment.

Tagged:

Best Answer

Answers

  • CorwynCorwyn USMember ✭✭✭

    Sadly, I am not comprehending where you are encountering an issue.
    Can you give an example of what you trying to do and where you are encountering your issue?

  • ganesh96ganesh96 Member ✭✭✭

    Hi @Corwyn

    Okay I'll try to explain it.

    Every 10 seconds, the app should get information from a API. When this information arrives, three things can happen:

    • If the information that arrives is new, it must be added to the listview.
    • If the information is the same as already in the listview, nothing happens.
    • If the information is the same as in the listview with some changes, that specific item in the listview should be updated.

    Now the value of Text1 will be the reference value, meaning that when the value of Text1 does not exist in the listview, it should add the entire model to the listview.
    However, if the value of Text1 does exist in the listview, it needs to check if the other item, Text2, should be updated or not.

    No my problem: how to do this update?

    I hope this was clear.

  • CorwynCorwyn USMember ✭✭✭

    Have you tried using BinarySearch(https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.binarysearch?view=netcore-3.1) on the list to identify the item to update? Then, if it exists, use the returned position to update the list item?

  • ganesh96ganesh96 Member ✭✭✭

    Hi @Corwyn

    I never heard of that, so I tried it. I got some results, but I'm stuck and the part where the code has to check one row based on a given index to see if the other values in that row differ.

    Here is some code I came up with:

        public class LimitsComparer : IComparer<Model>
        {
    
            public int Compare(Model x, Model y)
            {
                return x.Text1.CompareTo(y.Text1);
            }
    
        }
    
        private void Button_Clicked(object sender, EventArgs e)
        {
            var mainIndex = information.BinarySearch(new Model { Text1 = "Text 1" }, new LimitsComparer());
    
            if (mainIndex < 0)
            {
                information.Add(new Model
                {
                    Text1 = "Text 5",
                    Text2 = "Text 6"
                });
            }
            else
            {
                //Get all items in a row, based on the value of Text1
                if () //If values are not the same
                {
                    //Update row based on found index
                }
            }
        }
    
  • CorwynCorwyn USMember ✭✭✭

    Aren't you just trying to do something similar to this? (very ugly code here)

            private void Button_Clicked(object sender, EventArgs e)
            {
                string searchvalue = text1searchvalue.Text;
                information.Sort(new LimitsComparer());
    
                var mainIndex = information.BinarySearch(new Model { Text1 = searchvalue }, new LimitsComparer());
    
                if (mainIndex < 0)
                {
                    information.Add(new Model
                    {
                        Text1 = searchvalue,
                        Text2 = text2value.Text
                    });
                }
                else
                {
                    information[mainIndex].Text2 = text2value.Text;
                    //Get all items in a row, based on the value of Text1
                }
            }
    
                </ListView.ItemTemplate>
            </ListView>
            <Label Text="Text1"/>
            <Entry x:Name="text1searchvalue" ></Entry>
            <Label Text="Text2"></Label>
            <Entry x:Name="text2value"></Entry>
            <Button x:Name="button" Text="click me" Clicked="Button_Clicked"/>
        </StackLayout>
    
    
Sign In or Register to comment.