[英]Populating a UIPickerView with data from Sqlite DataBase in IPhone App --
我有一個帶有表'BikeInfo'的sqlite數據庫
[BikeID, BikeName]
現在,我希望使用該數據庫中的自行車名稱填充我的pickerview。 在.Net中,它是如此簡單。 但是作為MonoTouch的完整入門者,我對如何解決該問題一無所知。 我進行了一次谷歌搜索,發現我必須創建一個從UIPickerViewModel
類擴展的自定義類,並覆蓋其各種方法。 就是這樣。 我找不到任何示例代碼,教程或任何內容。 我已經為此戰斗了三天。 我很困惑。 到目前為止,我已經編寫了一個函數,該函數從表中獲取數據並返回類對象的列表[我編寫的包含屬性BikeName和BikeId]。 也許我在所有錯誤的地方尋找。 但是我需要以任何方式做到這一點。
處理這種特定於目標的方案的任何類型的教程或C#中的示例代碼都對我有幫助。
下一個目標是顯示警報消息,該消息顯示當用戶觸摸一個特定項目時相應的自行車ID。 我認為我們必須在該自定義類中定義所選事件。 這是我從互聯網上挑選的。
謝謝。
到目前為止,這是我所做的。 我創建了一個包含屬性BikeID
和BikeName
。
namespace ASTONAPP
{
public class PickerFacilityTemplate
{
public PickerFacilityTemplate ()
{
}
public int BikeID{get; set;}
public string BikeName{get; set;}
}
}
之后,我在數據庫處理程序類中編寫了該函數,該函數返回上述類類型的類對象的列表。
public List<PickerFacilityTemplate> FetchFacility()
{
DataSet ds = new DataSet ();
string sql = "select * from BikeInfo order by BikeName";
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.BikeID=Convert.ToInt32(ds.Tables[0].Rows[i]["BikeID"].ToString());
pft.BikeName=ds.Tables[0].Rows[i]["BikeName"].ToString ();
objfcl.Add (pft);
}
this.CloseDBConnection ();
return objfcl.ToList ();
}
public class PickerDataModelSource: UIPickerViewModel
{
public PickerDataModelSource ()
{
}
public PickerDataModelSource (List<PickerFacilityTemplate> lst)
{
this.Items=lst;
}
public event EventHandler<EventArgs> ValueChanged;
public List<PickerFacilityTemplate> Items;
List<PickerFacilityTemplate> _items = new List<PickerFacilityTemplate>();
public PickerFacilityTemplate SelectedItem
{
get
{
return this._items[this._selectedIndex];
}
}
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)
{
return items[row].BikeName.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 ());
}
}
}
現在,在我有pickerview的屏幕的類文件中,我已經寫好了,
PickerDataModelSource _pickerSource;
並在ViewDidLoad
方法中:
this.Picker.Source=this._pickerSource;
this.Picker.Model=this._pickerSource;
但是,當我運行該應用程序時,我的pickerview是空白的。 我想念什么嗎? 我的邏輯錯誤在哪里?
謝謝。
如何使用選擇器的一個示例在“廚房水槽”示例中-https: //github.com/xamarin/monotouch-samples/tree/master/MonoCatalog-MonoDevelop-特別是在https://github.com/xamarin中/monotouch-samples/blob/master/MonoCatalog-MonoDevelop/PickerViewController.xib.cs
總的來說,我發現該回購對於許多mt開發任務至關重要。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.