Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 89864

ListView not displaying data

$
0
0

Hi Xamarin Forum

can somebody help me on this I searchbar is not displaying any query result on the page 2 or the search result page any way here is my code

page 1 XAML

<StackLayout>
            <Grid>
                <StackLayout>
                    <StackLayout Spacing="0" VerticalOptions="Start" Orientation="Horizontal">
                        <StackLayout.Margin>
                            <OnPlatform x:TypeArguments="Thickness">
                                <On Platform="iOS" Value="0,12,0,0" />
                                <On Platform="Android" Value="0,24,0,0" />
                            </OnPlatform>
                        </StackLayout.Margin>
                        <Image Source="xmark.png" Margin="30,-10,0,30" HorizontalOptions="StartAndExpand" VerticalOptions="Center" HeightRequest="20" WidthRequest="20">
                            <Image.GestureRecognizers>
                                <TapGestureRecognizer Tapped="CloseSearchPage"/>
                            </Image.GestureRecognizers>
                        </Image>
                    </StackLayout>
                    <SearchBar HeightRequest="50" x:Name="SearchBarList" Placeholder="Search Places" WidthRequest="200" SearchButtonPressed="Searchbar_searchpressed"/>
                </StackLayout>   
            </Grid>
        </StackLayout>
Code behind

public partial class SearchPage : ContentPage
    {
        public SearchPage ()
        {
            InitializeComponent ();
        }

        void CloseSearchPage(object sender, EventArgs e)
        {
            var homepage = new HomePage();
            Application.Current.MainPage = new NavigationPage(homepage);
        }

        private void Searchbar_searchpressed(object sender, EventArgs e)
        {
            var search = sender as SearchBar;
            this.Navigation.PushModalAsync(new SearchResultpage(search.Text), true);
        }
    }
Page 2 XAML

    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="ProjectX.Views.SearchResultpage">
        <ContentPage.Content>
            <StackLayout>
                <SearchBar x:Name="searchBarlist" TextChanged="SearchBar_TextChanged"/>
                <ListView  x:Name="listView" RowHeight="60" ItemTapped="SelectedPost" Margin="10,0,10,0">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell >
                                <StackLayout Orientation="Horizontal">
                                    <Image Source="{Binding Photo}" WidthRequest="40" HeightRequest="40"></Image>
                                    <StackLayout>
                                        <Label Text="{Binding PostContent}" FontSize="20"/>
                                        <Label Text="{Binding Uploader}" FontSize="15"/>
                                    </StackLayout>
                                </StackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </StackLayout>
        </ContentPage.Content>
    </ContentPage>
Page 2 Codebehind

     public class PostProperty
        {
            public string Uploader { get; set; }
            public string PhotoSource { get; set; }
            public string PostContent { get; set; }
            public ImageSource Photo
            {

                get
                {
                    byte[] bytes = Convert.FromBase64String(PhotoSource);
                    MemoryStream ms = new MemoryStream(bytes);
                    ImageSource s = ImageSource.FromStream(() => ms);
                    return s;
                }
                set
                {

                }
            }
        }

    public class ViewModels
        {
            public ViewModels()
            {
                DataSet ds = new DataSet();
                Pipeline databaseConnect = new Pipeline();

                SqlCommand selectPost = new SqlCommand("SELECT * FROM t_Post", databaseConnect.connectDB());
                SqlDataReader reader;
                reader = selectPost.ExecuteReader();
                postList = new List<PostProperty>();
                while (reader.Read())
                {
                    string postContent = (string)reader["Post_Content"];
                    string uploader = (string)reader["Uploader"];
                    var stream = reader["Photo"];
                    string imageUrl = Convert.ToBase64String((byte[])reader["Photo"]);
                    var postWall = new Post
                    {
                        PhotoSource = imageUrl,
                        PostContent = postContent,
                        Uploader = uploader
                    };
                }
            }

            public List<PostProperty> postList { get; set; }
        }


public partial class SearchResultpage : ContentPage
    {
        public PostViewModel models;

        public SearchResultpage()
        {
            InitializeComponent();

        }

        public SearchResultpage (string key)
        {
            InitializeComponent ();
            models = new PostViewModel();
            searchBarlist.Text = key;
        }

        public void SearchBar_TextChanged(object sender, TextChangedEventArgs e)
        {
            var search = sender as SearchBar;

            var suggestion = models.postList.Where(c => (c.PostContent.ToLower().Contains(search.Text.ToLower()) || c.Uploader.ToLower().Contains(search.Text.ToLower()))).ToList();
            listView.ItemsSource = suggestion;
        }

        protected async void SelectedPost(object sender, ItemTappedEventArgs args)
        {
            var house = args.Item as Post;
            await this.Navigation.PushModalAsync(new PostDetailPage(house), false);
            ((ListView)sender).SelectedItem = null;

        }
    } 

Thanks


Viewing all articles
Browse latest Browse all 89864

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>