OK I have fixed the issue. Had to add an event to the PickerDataSourceClass.
The error has disappeared. But I am not getting the items in my picker
control. It is completely blank. This is how I have gone about the problem.
Please go through the code listings below to have an understanding of my
approach. Maybe you will be able to spot some logical mistakes.
Here they are:-->
PickerFacilityTemplate.cs
----------------------------------
using System;
namespace ASTONAPP
{
public class PickerFacilityTemplate
{
public PickerFacilityTemplate ()
{
}
public int AssessmentID{get; set;}
public int FacilityID{get; set;}
public string FacilityName{get; set;}
}
}
PickerFacilityTemplateGroup.cs
----------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using MonoTouch.UIKit;
namespace ASTONAPP
{
public class PickerFacilityTemplateGroup
{
public PickerFacilityTemplateGroup ()
{
}
public List<PickerFacilityTemplate> Items
{
get { return this._items; }
set { this._items = value; }
}
protected List<PickerFacilityTemplate> _items=new
List<PickerFacilityTemplate>();
}
}
PickerDataModelSource.cs
----------------------------------------------
using System;
using System.Collections;
using MonoTouch.UIKit;
using System.Configuration;
using System.IO;
using System.Collections.Generic;
using Mono.Data.Sqlite;
using System.Data;
using System.Data.Common;
namespace ASTONAPP
{
public class PickerDataModelSource: UIPickerViewModel
{
public PickerDataModelSource ()
{
}
public PickerDataModelSource (List<PickerFacilityTemplateGroup>
lst)
{
this.Items=lst;
}
public event EventHandler<EventArgs> ValueChanged;
public List<PickerFacilityTemplateGroup> Items ;
List<PickerFacilityTemplate> _items = new
List<PickerFacilityTemplate>();
public PickerFacilityTemplate SelectedItem
{
get
{
//return this._items[this._selectedIndex];
return this._items[1];
}
}
public int _selectedIndex = 0;
public override int GetRowsInComponent (UIPickerView picker, int
component)
{
return this._items.Count;
}
public override string GetTitle (UIPickerView picker, int row,
int
component)
{
List<string> titles=new List<string>();
foreach(PickerFacilityTemplate pftp in _items)
{
//return this._items[row].FacilityName.ToString
();
titles.Add (pftp.FacilityName.ToString ());
}
return titles.ToString ();
}
public override int GetComponentCount (UIPickerView picker)
{
return 1;
}
public override void Selected(UIPickerView picker, int row, int
component)
{
this._selectedIndex = row;
if (this.ValueChanged != null)
{
this.ValueChanged (this, new EventArgs ());
}
}
}
}
The function in my DBHandler where I am fetching the items from database
-------------------------------------------------------------------------------------------
public List<PickerFacilityTemplate> FetchFacility()
{
DataSet ds = new DataSet ();
string sql = "select * from ClientFacility order by
FacilityName";
this.CreateDBConnection ();
SqliteDataAdapter sda = new SqliteDataAdapter (sql,
sconn);
sda.Fill (ds);
List<PickerFacilityTemplate> objfcl=new
List<PickerFacilityTemplate>();
for(int i=0; i<ds.Tables[0].Rows.Count; i++)
{
PickerFacilityTemplate pft=new
PickerFacilityTemplate();
pft.AssessmentID=Convert.ToInt32(ds.Tables[0].Rows[i]["AssessmentID"].ToString
());
pft.FacilityID=Convert.ToInt32(ds.Tables[0].Rows[i]["FacilityID"].ToString
());
pft.FacilityName=ds.Tables[0].Rows[i]["FacilityName"].ToString
();
objfcl.Add (pft);
}
this.CloseDBConnection ();
return objfcl.ToList ();
}
The function where I am calling the above function
-----------------------------------------------------------------
public void PopulatePickerItems()
{
List<PickerFacilityTemplateGroup> pickerItems = new
List<PickerFacilityTemplateGroup> ();
PickerFacilityTemplateGroup pGroup;
pGroup=new PickerFacilityTemplateGroup();
List<PickerFacilityTemplate> pft=new
List<PickerFacilityTemplate>();
objdbh=new DBHandler();
pft=objdbh.FetchFacility ();
foreach(PickerFacilityTemplate pkft in pft)
{
pGroup.Items.Add (new PickerFacilityTemplate()
{
AssessmentID=Convert.ToInt32(pkft.AssessmentID.ToString ()),
FacilityID=Convert.ToInt32(pkft.FacilityID.ToString ()),
FacilityName=pkft.FacilityName.ToString
()
});
}
pickerItems.Add (pGroup);
this._pickerSource = new PickerDataModelSource
(pickerItems);
}
And finally
---------------------------------------------------
this.PopulatePickerItems ();
this.Picker.Source=this._pickerSource;
So where could the problem lie?
Can you spot something?
Thanks.
--
View this message in context:
http://monotouch.2284126.n4.nabble.com/Implementing-UIPickerView-with-data-from-Database-tp4655796p4655798.html
Sent from the MonoTouch mailing list archive at Nabble.com.
_______________________________________________
MonoTouch mailing list
[email protected]
http://lists.ximian.com/mailman/listinfo/monotouch