2017-03-19 6 views
0

私はこのコードを使用して新しいSQLデータベースを作成し、アイテムを保管しています。Sqliteリストビュー内の項目を取り込む

私はこのコードでクラスを作成している:私の主な活動に続いて

class Contact 
{ 
    public string Name { get; set; } 

    public Contact(string name) 
    { 
     Name = name; 
    } 

    public Contact() 
    { 

    } 

  string dbPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "dbTest.db3"); 
    private List<string> mitems; 
    private ListView mlistview; 
    protected override void OnCreate(Bundle bundle) 
    { 
     base.OnCreate(bundle); 

     SetContentView(Resource.Layout.Main); 

     Button InsertAndShow = FindViewById<Button>(Resource.Id.button2); 
     InsertAndShow.Click += delegate 
     { 
      EditText text = FindViewById<EditText>(Resource.Id.editext1); 

      var db = new SQLiteConnection(dbPath); 
      Contact mycontact = new Contact(text.Text); 
      db.Insert(mycontact); 
      var table = db.Table<Contact>(); 
      foreach (var item in table) 
      { 


       mlistview = FindViewById<ListView>(Resource.Id.listView1); 
       mitems = new List<string>(); 

       mitems.Add(Convert.ToString(item.Name)); 

       ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, mitems); 

       mlistview.Adapter = adapter; 

      } 
     }; 

をしかし、それはすべて私の項目を表示しません。最後の1つの項目だけが表示されます。

答えて

0
mitems = new List<string>(); 

foreach (var item in table) 
{ 
    mitems.Add(Convert.ToString(item.Name)); 
} 

mlistview = FindViewById<ListView>(Resource.Id.listView1); 
ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, mitems); 
mlistview.Adapter = adapter; 
関連する問題