I have problem in populating a table in monotouch using SQLite as database. My problem is it only selects the last data inserted and returns it as many as the number of data in the table selected.
ex. data=iPhone, number of data in a table = 30.
it returns the iPhone in the table 30 times.
Here is the code:
MainViewController
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
LoadToolbarButtons ();
LoadNavigationBarButtons ();
BL.Products MyProducts = new BL.Products();
List<Products> productList = new List<Products>();
POSDB.InitPOSDatabase ();
POSDB.GetAllProducts (productList, MyProducts);
tblProductList.Source = new ProductListDataSource(productList);
tblProductList.Delegate = new ProductListDelegate();
}
DataSource
public class ProductListDataSource : UITableViewSource
{
public List<Products> ProductList = new List<Products>();
public BL.Products myProducts = new BL.Products();
public ProductListDataSource(List<Products> productList):base()
{
this.ProductList = productList;
}
#region implemented abstract members of MonoTouch.UIKit.UITableViewSource
public override int RowsInSection (UITableView tableview, int section)
{
if (ProductList.Count == 0)
{
UIAlertView alert = new UIAlertView ("No Records!", "Please add some items to your table.", null, "OK", null);
alert.Show ();
}
return ProductList.Count;
}
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
string cellIdentifier = "Cell";
UITableViewCell cellProduct = tableView.DequeueReusableCell(cellIdentifier);
if (cellProduct == null)
{
cellProduct = new UITableViewCell(UITableViewCellStyle.Value1,cellIdentifier);
}
var products = this.ProductList[indexPath.Row];
cellProduct.TextLabel.Text =string.Format("({0}) {1}", products.ProductID, products.ProductName);
cellProduct.DetailTextLabel.Text = "$" + System.Convert.ToString(products.Price);
return cellProduct;
}
#endregion
}
Is there something wrong with my codes? Thank you in advance!