JacquesD

JacquesDJacquesD Member ✭✭

Hi All A have the Following Model With SQLite Database:

    public class SQLMemberDetail
   {
    [PrimaryKey]
    public int SQL_MemberId { get; set; }
    public string SQL_Email { get; set; }
    public string SQL_Title { get; set; }
    public string SQL_Surname { get; set; }
    public string SQL_Initials { get; set; }
    public string SQL_Language { get; set; }
    public string SQL_Sex { get; set; }
    public string SQL_MedicalAid { get; set; }
    public string SQL_MemberNum { get; set; }
    public string SQL_DependantCode { get; set; }
    public string SQL_PoBox { get; set; }
    public string SQL_CellNum { get; set; }

    public override string ToString()
    {
        return this.SQL_DependantCode + " (" + this.SQL_Initials + " " + this.SQL_Surname + ")";
    }
}

I Then Create a list and populate it like This:

  public class ViewMemberPage : ContentPage
   {
    public ListView lst_SavedMembers;

    string db_Path = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "myDB.db3");

  public ViewMemberPage()
    {
        this.Title = "View Saved Members";

        var db = new SQLiteConnection(db_Path);

        StackLayout ViewMemberStackLayout = new StackLayout();

        lst_SavedMembers = new ListView();
        lst_SavedMembers.Header = "List of Saved Members";
        lst_SavedMembers.ItemsSource = db.Table<SQLMemberDetail>().OrderBy(x => x.SQL_DependantCode).ToList();
        lst_SavedMembers.HeightRequest = 50 * db.Table<SQLMemberDetail>().Count();
        lst_SavedMembers.ItemSelected += Lst_SavedMembers_ItemSelected;
        ViewMemberStackLayout.Children.Add(lst_SavedMembers);

   Content = ViewMemberStackLayout;
     }

the list gets populated with data as i step through the code and the selectedItemChangedEvent does populste other fields with relevant data.

the Problem i am having is the list is not showing Text like the attached Immage Shows.

Can anybody please help me with why this would happen.
O and yes i am quite new to Xamarin.

Tagged:

Best Answers

Answers

  • JacquesDJacquesD Member ✭✭

    JohnHardman

    Hi, Thanks for the advice i tried and it all seems good. changing the height or using verticalOptions has no effect.
    one thing is when i add my list into a Picker it displays perfectly, just i do not know how to handle

    public void Lst_SavedMembers_ItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
    sqlmemberdetail = (SQLMemberDetail)e.SelectedItem;
    {

    this event if i use a picker as for instance,

    public void pck_SavedMembers_ItemSelected(object sender, EventArgs e)
    {
    sqlmemberdetail = (SQLMemberDetail)e.SelectedItem;
    {

    does not give me the desired event effect.

    Hope this makes sense.

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    @JacquesD

    I notice your listview code is not correctly.

    You did not define the viewcell, you can refer to my code.

      internal class CustomVeggieCell: ViewCell
    { 
        public CustomVeggieCell()
    {
    
        //instantiate each of our views
        //  var image = new Image();
        Label nameLabel = new Label();
        Label typeLabel = new Label();
        StackLayout verticaLayout = new StackLayout();
        StackLayout horizontalLayout = new StackLayout() { BackgroundColor = Color.Olive };
    
        //set bindings
    
        nameLabel.SetBinding(Label.TextProperty, new Binding("Text"));
        typeLabel.SetBinding(Label.TextProperty, new Binding("Date"));
        // image.SetBinding(Image.SourceProperty, new Binding("Image"));
    
        //Set properties for desired design
        horizontalLayout.Orientation = StackOrientation.Horizontal;
        horizontalLayout.HorizontalOptions = LayoutOptions.Fill;
      //  image.HorizontalOptions = LayoutOptions.End;
        nameLabel.FontSize = 24;
    
        //add views to the view hierarchy
        verticaLayout.Children.Add(nameLabel);
        verticaLayout.Children.Add(typeLabel);
        horizontalLayout.Children.Add(verticaLayout);
       // horizontalLayout.Children.Add(image);
    
        // add to parent view
        View = horizontalLayout;
    }
    }  
    

    Here is code about listview.

           protected override async void OnAppearing()
        {
            base.OnAppearing();
            StackLayout ViewMemberStackLayout = new StackLayout();
    
            ListView lst_SavedMembers = new ListView();
            lst_SavedMembers.Header = "List of Saved Members";
    
            lst_SavedMembers.ItemTemplate= new DataTemplate(typeof(CustomVeggieCell));
            lst_SavedMembers.ItemsSource = await App.Database.GetNotesAsync();
    
    
            lst_SavedMembers.ItemSelected += Lst_SavedMembers_ItemSelected; ;
            ViewMemberStackLayout.Children.Add(lst_SavedMembers);
    
            Content = ViewMemberStackLayout;
            //   lst_SavedMembers.HeightRequest = 50 * db.Table<SQLMemberDetail>().Count();
            //   listView.ItemsSource = await App.Database.GetNotesAsync();
        }
    

    Here is my project running GIF.

    Here is my demo, you can refer to it.

  • JacquesDJacquesD Member ✭✭

    Hi All,

    So i Based my Code on this,

    why does it work in this example but not in my code. what am i doing wrong

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    @JacquesD Did you try my code? If you still did not know how to solve it, please upload your demo, I will help you to troubleshot it.

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    @JacquesD said:
    Hi All,

    So i Based my Code on this,

    why does it work in this example but not in my code. what am i doing wrong

    Did you create a CustomCell for your listview?

  • JohnHardmanJohnHardman GBUniversity mod
    edited September 2

    @JacquesD said:
    JohnHardman

    Hi, Thanks for the advice i tried and it all seems good. changing the height or using verticalOptions has no effect.

    Have you tried all the bits I mentioned?

    @JacquesD said:
    one thing is when i add my list into a Picker it displays perfectly, just i do not know how to handle

    It's best to keep one forum thread concentrated on one subject. If you want to know about using Picker, ask that in a separate thread. That way, this thread can concentrate on getting your ListView doing what you want it to do. Going off at a tanget about using Pickers just makes the thread confusing.

  • JacquesDJacquesD Member ✭✭
    edited September 4

    Hi LeonLu,

    Here is my Demo

  • NMackayNMackay GBInsider, University mod

    @JacquesD

    Hi,

    Please give your threads a meaningful title, I thought this was spam and was going to clean it up but decided to take a look 1st.

    https://forums.xamarin.com/discussion/87290/xamarin-forum-rules-and-guidelines#latest

  • JacquesDJacquesD Member ✭✭

    sorry will do that @NMackay

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    @JacquesD If you have solved this issue, can I help you to delete demo(just keep the related code) in this thread to protect your personal information.

  • JacquesDJacquesD Member ✭✭

    @LeonLu

    all is working thanks wee can delete the demo,

    I really appreciate the help

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    @JacquesD You are welcome.

  • JohnHardmanJohnHardman GBUniversity mod

    @JacquesD said:
    @LeonLu

    all is working thanks wee can delete the demo,

    I really appreciate the help

    If it was the missing ItemTemplate, don't forget to mark as Accepted Answer the first response to your original post, which said to add ItemTemplate (point 5 at https://forums.xamarin.com/discussion/comment/388012/#Comment_388012 )

Sign In or Register to comment.