在Xamarin iOS中从SQLite中检索多行

时间:2014-04-07 09:57:15

标签: sqlite uitableview xamarin.ios xamarin

我正在使用Xamarin iOS进行应用。 我在XCode上放了一个UITableView,所以当我点击一个按钮时,它会从数据库中检索并将其插入。我可以将它放到一行,但无法弄明白如何其中有多行数据。这是我的部分代码,我可以从中显示一行数据。

cmd.CommandType = CommandType.Text;
dr = cmd.ExecuteReader();
while (dr.Read())
    {
        var table = new UITableView(this.retrieveData.Frame);
        string[] tableItems = new String[] {dr["admin_num"] + ", " + dr["name"]};
        table.Source = new TableSource(tableItems);
        Add (table);
    }

2 个答案:

答案 0 :(得分:1)

您正在为数据中的每一行创建一个全新的TableView。相反,您应该遍历数据并创建包含要显示的所有数据的数据结构(列表,数组等),然后将该数据传递给TableView / Source。

  cmd.CommandType = CommandType.Text;
  dr = cmd.ExecuteReader();

  // you will need a class mydata with Num and Name properties
  List<mydata> data = new List<mydata>();

  while (dr.Read())
  {
    data.Add(new mydata { Num = dr["admin_num"], Name = dr["name"] });
  }

  dr.Close();

  var table = new UITableView(this.retrieveData.Frame);
  table.Source = new TableSource(data);
  Add (table);

答案 1 :(得分:0)

您需要做的是:

 public List<DemoClass> getDemoClassList()
    {
        List<DemoClass> lstDemoClass;
        DemoClass objDemoClass;
        try
        {
            String strCommandText;

            strCommandText = "SELECT * FROM DemoClass ";

            command = new SqliteCommand(strCommandText, connection);
            lstDemoClass = new List<DemoClass>();
            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    objDemoClass = new Homes(false);
                    objDemoClass.ID = Convert.ToInt32(reader[0]);
                    objDemoClass.Name = Convert.ToString(reader[1]);
                    lstDemoClass.Add(objDemoClass);

                }
            }
            return lstDemoClass;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            command.Dispose();
            command = null;
            lstDemoClass = null;
            objDemoClass = null;
        }
    }
             public void BindList()
             {   
                List<DemoClass> lstDemoClass = new List<DemoClass>();
                DemoClass hm = new DemoClass();
                lstDemoClass = (List<DemoClass>)hm.getDemoClassList();

                TableViewDataSource tdatasource = new TableViewDataSource(this, lstDemoClass);
                table.Hidden = false;
                table.DataSource = tdatasource;
                table.Delegate = new TableViewDelegate(this, table, lstDemoClass);
                table.ReloadData();
             }

getDemoClassList()将从SQLite表中提供检索到的列表,稍后您可以将列表绑定到表数据源。

<强>更新: 根据您的要求,我已使用数据源代码及其委托类更新了我的注释。 现在在同一个类中,您需要添加以下子类:

#region TableDelegate
    public class TableViewDelegate : UITableViewDelegate
    {
        private DemoPageViewController _Controller;
        private List<DemoClass> lst;

        public TableViewDelegate(DemoPageViewController controller ,UITableView tableView, List<DemoClass> tblList)
        {
            try 
            {
                this._Controller = controller;
                this.lst = tblList;
            }
            catch(Exception ex)
            {

            }
        }

        public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
        {
            try 
            {
                //This loads the activity spinner till the selection code is completed
                _Controller._loadPop = new LoadingOverlay (new System.Drawing.RectangleF(0,0,_Controller.View.Frame.Width,_Controller.View.Frame.Height),"Loading...");
                _Controller.View.Add ( _Controller._loadPop );

                // spin up a new thread to do some long running work using StartNew
                Task.Factory.StartNew (
                    // tasks allow you to use the lambda syntax to pass work
                    () => {
                    InvokeOnMainThread(delegate{

                        DemoClass f = lst[indexPath.Row];

                        //Add your code here, usually some navigation or showing a popup
                    });
                }).ContinueWith(t => InvokeOnMainThread(() => {
                    //Hide the activity spinner
                    _Controller._loadPop.Hide();
                }));

            }
            catch(Exception ex)
            {

            }
            finally     
            {
            }
        }
    }

    #endregion 

    #region TableDataSource

    private class TableViewDataSource : UITableViewDataSource
    {
        static NSString kCellIdentifier = new NSString("MyIdentifier");
        private List<DemoClass> lst;
        private DemoPageViewController controller;

        public TableViewDataSource (DemoPageViewController controller ,List<DemoClass> tblLst)
        {
            this.controller = controller;
            this.lst = tblLst;
        }

        public override int NumberOfSections (UITableView tableView)
        {
            return 1;
        }

        public override int RowsInSection (UITableView tableView, int section)
        {
            return lst.Count;
        }

        // Override to support conditional editing of the table view.
        public override bool CanEditRow (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
        {
            // Return false if you do not want the specified item to be editable.
            return false;
        }


        public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
        {
            try 
            {
                UITableViewCell cell = tableView.DequeueReusableCell (kCellIdentifier);
                if (cell == null)
                {
                    cell = new UITableViewCell (UITableViewCellStyle.Subtitle, kCellIdentifier);
                    cell.Tag = Environment.TickCount;
                }

                DemoClass objDemo = lst[indexPath.Row];
                cell.Accessory = UITableViewCellAccessory.DisclosureIndicator;

                cell.ImageView.Image = UIImage.FromFile("Images/CameraImg.png");
                cell.DetailTextLabel.Text = "Show some detail: " + objDemo.DemoDescription.ToString();
                cell.TextLabel.Text = "Some Title: " + objDemo.DemoTitle.ToString();
                return cell;
            }
            catch(Exception ex)
            {
                return null;
            }
            finally     
            {

            }
        }
    }

    #endregion 

希望它有所帮助。