Update/Refresh ListView or ListView Item without starting from the top

Hey there.

Is there a way to update/refresh my ListView or ListView Item? Currently the only way to update/refresh my ListView is this:

    public void NewsList_Selected(Object sender, SelectedItemChangedEventArgs e)
    {
        var a = e.SelectedItem as NewsEntry;
        var b = from c in newsEntries
                where (a == c)
                select c;
        foreach(NewsEntry d in b)
        {
            d.Text = d.TextFull;
        }

        // Below is my update/refresh thing
        NewsList.ItemsSource = null;
        NewsList.ItemsSource = newsEntries;
    }

But that means that if I scroll down in my ListView and select an Item I will jump to the top of my ListView again. But I need to stay at the same spot I left. Is there a solution?

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    What do you mean 'refresh it'? You shouldn't have to 'refresh' a view. When you update the binded object the view will update automatically.

    {reading your code more}

    You shouldn't have to do any of that mess. No need for it with binding. Clearly something is wrong with your binding if you're having to do that.

  • axbeitaxbeit Member ✭✭

    @ClintStLaurent said:
    What do you mean 'refresh it'? You shouldn't have to 'refresh' a view. When you update the binded object the view will update automatically.

    {reading your code more}

    You shouldn't have to do any of that mess. No need for it with binding. Clearly something is wrong with your binding if you're having to do that.

    Hi

    I did it this way could you tell where my mistake is?

    .xaml

            <ListView x:Name="NewsList" ItemSelected="NewsList_Selected" HasUnevenRows="true">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Frame BorderColor="Black">
                                <StackLayout>
                                    <StackLayout>
                                        <Image HeightRequest="50" WidthRequest="50" Source="https://xamarin.com/content/images/pages/forms/example-app.png" />
                                    </StackLayout>
                                    <Label Text="{Binding Title}" />
                                    <Label Text="{Binding Text}" />
                                </StackLayout>
                            </Frame>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
    

    .cs

    public partial class NewsPage : ContentPage
    {
        ObservableCollection<NewsEntry> newsEntries = new ObservableCollection<NewsEntry>();
    
        public NewsPage()
        {
            InitializeComponent();
    
            NewsList.ItemsSource = newsEntries;
    
            MessagingCenter.Subscribe<WriteArticle, NewsEntry>(this, "Spiderman", (p, item) =>
            {
                var finishedNews = item as NewsEntry;
    
                //
                if (finishedNews.TextFull.Length > 10)
                {
                    finishedNews.Text = finishedNews.TextCut;
                }
                else finishedNews.Text = finishedNews.TextFull;
                //
    
                newsEntries.Insert(0, finishedNews);
            });
        }
    

    I hope this is what you need. Those are the Bindings you mean I guess? (Binding Title, Binding Text, NewsList.ItemsSource = newsEntries)

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭
    edited November 2018

    I did it this way could you tell where my mistake is?

    Clearly you know how to do binding because I see it in your ListView. So what is your ContentPage full of direct UI element changes like its the year 2001?
    finishedNews.Text = finishedNews.TextCut;

    Oh... Your collection is a field not a property
    ObservableCollection<NewsEntry> newsEntries = new ObservableCollection<NewsEntry>();
    You can't bind to fields. Binding has to be done to properties because they implement INotifyPropertyChanged interface. notice how Property is right there in the name?

    I'm going to guess you haven't gone through any tutorials, course or books on Xamarin before jumping in and trying to code something up from scratch. That never goes well. You need to understand the foundation concepts like MVVM before you can design your program. Its like needing to understand construction techniques and materials before you can architect a house.

    Stop working on this project for a couple weeks to a month. Just work on learning. WORK some tutorials from start to end. Build the sample apps the authors are walking you through. Then dissect those apps to gain real comprehension of all the relationships and moving parts. Then take that new understanding and start version two of this app from scratch.

    Free textbook
    https://blogs.msdn.microsoft.com/microsoft_press/2016/03/31/free-ebook-creating-mobile-apps-with-xamarin-forms/

    Xamarin University
    https://university.xamarin.com/

    Xamarin University has dozens of tutorial classes on YouTube if you use their search feature.

    And of course there are dozens of Xamarin bloggers out there.
    http://redpillxamarin.com/2018/03/12/2018-101-vs2017-new-solution/

Sign In or Register to comment.