2014-11-05 56 views
1

我正在开发一个使用sqlite数据库的Windows Phone应用程序。我能够显示出数据库并删除特定的行我想删除。但问题是我选择行并单击删除,行在当时不会消失。我必须出租该页面才能看到它被删除。 以下这里是我使用click_delete事件删除一行后,它只是不会从屏幕上消失

public partial class History : PhoneApplicationPage 
{ 
    ObservableCollection<historyTableSQlite> DB_HistoryList = new ObservableCollection<historyTableSQlite>(); 
    DbHelper Db_helper = new DbHelper(); 
    //public static int Selected_HistoryId; 
    //int Selected_HistoryId; 
    public static int Selected_HistoryId {get; set;} 



    // string dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db.sqlite"); 

    public History() 
    { 

     InitializeComponent(); 


    } 

    protected override void OnNavigatedTo(NavigationEventArgs e) 
    { 

     Db_helper.AddInfo(); 
     ReadHistoryList_Loaded(); 
     // Selected_HistoryId = int.Parse(NavigationContext.QueryString["SelectedHistoryID"]); 
    } 

    public void ReadHistoryList_Loaded() 
    { 
     ReadAllContactsList dbhistory = new ReadAllContactsList(); 
     DB_HistoryList = dbhistory.GetAllHistory();//Get all DB contacts 
     ListData.ItemsSource = DB_HistoryList.OrderByDescending(i => i.Id).ToList(); 


     //Latest contact ID can Display first 

    } 

    public void ListData_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     if (ListData.SelectedIndex != -1) 
     { 
      historyTableSQlite listitem = ListData.SelectedItem as historyTableSQlite; 
      History.Selected_HistoryId = listitem.Id; 
     } 
    } 

    private void Delete_Click(object sender, EventArgs e) 
    { 
     Db_helper.DeleteContact(History.Selected_HistoryId); 
     NavigationService.Navigate(new Uri("/History.xaml", UriKind.Relative)); 

    } 

    private void DeleteAll_Click(object sender, EventArgs e) 
    { 
     DbHelper Db_helper = new DbHelper(); 
     Db_helper.DeleteAllContact();//delete all DB contacts 
     DB_HistoryList.Clear();//Clear collections 
     ListData.ItemsSource = DB_HistoryList; 
    } 



} 
} 
下面

类的代码与所有主要功能

public class DbHelper 
{ 


    SQLiteConnection dbConn; 


    public async Task<bool> onCreate(string DB_PATH) 
    { 
     try 
     { 
      if (!CheckFileExists(DB_PATH).Result) 
      { 
       using (dbConn = new SQLiteConnection(DB_PATH)) 
       { 
        dbConn.CreateTable<historyTableSQlite>(); 
       } 
      } 
      return true; 
     } 
     catch 
     { 
      return false; 
     } 
    } 


    private async Task<bool> CheckFileExists(string fileName) 
    { 
     try 
     { 
      var store = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync(fileName); 
      return true; 
     } 
     catch 
     { 
      return false; 
     } 
    } 

    //retrieve all list from the database 
    public ObservableCollection<historyTableSQlite> ReadHistory() 
    { 
     using (var dbConn = new SQLiteConnection(App.DB_PATH)) 
     { 
      List<historyTableSQlite> myCollection = dbConn.Table<historyTableSQlite>().ToList<historyTableSQlite>(); 
      ObservableCollection<historyTableSQlite> HistoryList = new ObservableCollection<historyTableSQlite>(myCollection); 
      return HistoryList; 
     } 
    } 

    // Insert the new info in the histrorytablesqlite table. 
    public void Insert(historyTableSQlite newcontact) 
    { 
     using (var dbConn = new SQLiteConnection(App.DB_PATH)) 
     { 
      dbConn.RunInTransaction(() => 
      { 
       dbConn.Insert(newcontact); 
      }); 
     } 
    } 

    public void AddInfo() 
    { 


      DbHelper Db_helper = new DbHelper(); 
      Db_helper.Insert((new historyTableSQlite 
      { 
       Date = DateTime.Now.ToShortDateString(), 
       Time = DateTime.Now.ToShortTimeString(), 
       Zone = Checkin.Zone_st, 
       Floor = Checkin.Floor_st, 
       latitude = Checkin.Latitud_do, 
       longtitude = Checkin.Longtitude_do 
      })); 

     } 



    // Delete specific contact 
    public void DeleteContact(int Id) 
    { 
     using (var dbConn = new SQLiteConnection(App.DB_PATH)) 
     { 
      var existingvalue = dbConn.Query<historyTableSQlite>("select * from historyTableSQlite where Id =" + Id).FirstOrDefault(); 
      if (existingvalue != null) 
      { 
       dbConn.RunInTransaction(() => 
       { 
        dbConn.Delete(existingvalue); 
       }); 
      } 
     } 
    } 

    //Delete all contactlist or delete Contacts table 
    public void DeleteAllContact() 
    { 
     using (var dbConn = new SQLiteConnection(App.DB_PATH)) 
     { 
      //dbConn.RunInTransaction(() => 
      // { 
      dbConn.DropTable<historyTableSQlite>(); 
      dbConn.CreateTable<historyTableSQlite>(); 
      dbConn.Dispose(); 
      dbConn.Close(); 
      //}); 
     } 
    } 
下面

类与所有表类

public class historyTableSQlite : INotifyPropertyChanged 
{ 
    [SQLite.PrimaryKey, SQLite.AutoIncrement] 

    public int Id 
    { 
     get; 
     set; 
    } 
    private int idValue; 

    private string dateValue = string.Empty; 

    public string Date 
    { 
     get { return this.dateValue; } 
     set 
     { 
      if (value != this.dateValue) 
      { 
       this.dateValue = value; 
       NotifyPropertyChanged("Date"); 
      } 
     } 
    } 


    private string timeValue = string.Empty; 
    public string Time 
    { 
     get { return this.timeValue; } 
     set 
     { 
      if (value != this.timeValue) 
      { 
       this.timeValue = value; 
       NotifyPropertyChanged("Time"); 
      } 
     } 
    } 

    private string floorValue = string.Empty; 
    public string Floor 
    { 
     get { return this.floorValue; } 
     set 
     { 
      if (value != this.floorValue) 
      { 
       this.floorValue = value; 
       NotifyPropertyChanged("Floor"); 
      } 
     } 
    } 

    public string zoneValue; 
    public string Zone 
    { 
     get { return this.zoneValue; } 
     set 
     { 
      if (value != this.zoneValue) 
      { 
       this.zoneValue = value; 
       NotifyPropertyChanged("Zone"); 
      } 
     } 
    } 

    private double latValue; 
    public double latitude 
    { 
     get { return latValue; } 
     set 
     { 
      if (value != this.latValue) 
      { 
       this.latValue = value; 
       NotifyPropertyChanged("Latitude"); 
      } 
     } 
    } 

    private double lonValue; 
    public double longtitude 
    { 
     get { return this.lonValue; } 
     set 
     { 
      if (value != this.lonValue) 
      { 
       this.lonValue = value; 
       NotifyPropertyChanged("Longitude"); 
      } 
     } 
    } 

    // public string isMarkPoint { get; set; } 

    public historyTableSQlite() 
    { 

    } 

    public historyTableSQlite(string date, string time, string floor, string zone, double lat, double lng) 
    { 
     Date = date; 
     Time = time; 
     Floor = floor; 
     Zone = zone; 
     latitude = lat; 
     longtitude = lng; 
    } 
    public event PropertyChangedEventHandler PropertyChanged; 

    private void NotifyPropertyChanged(String info) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(info)); 
     } 
    } 
} 

回答

0

如果您从ObservableCollection中删除该项目,它将通知ListBox更新其容器。

在你的代码有

// this is correct 
ObservableCollection<historyTableSQlite> DB_HistoryList = new ObservableCollection<historyTableSQlite>(); 

但你的问题是,你实际上并不链接您ListBox

在您的代码中,您创建了一个副本(并且给出了您正在尝试执行的最差副本),并将其设置为ListBox ItemsSource。见下面(你有这样的)

ListData.ItemsSource = DB_HistoryList.OrderByDescending(i => i.Id).ToList(); 

所以基本上,你的列表框不是ObservableCollection但它是一个List结构。

删除并插入列表不会更新列表框的UI。

摆脱此列表,找到另一种排序方式ObservableCollection

然后你就基本上可以做到这一点

ListData.ItemsSource = DB_HistoryList; // set the listbox to the actual obs collection 

DB_HistoryList.RemoveAt(i);    // remove the item at index i 
DB_HistoryList.RemoveItem(object);  // remove the object that matches 
+0

感谢您再次拿出你的时间对我来说!除了使用列表之外,还有什么替代方法,如果你可以帮忙的话 – 2014-11-06 10:28:18

+0

重新阅读一次解决方案。没有其他选择,将您的列表框直接挂接到可观察集合。 – 2014-11-06 11:02:06

相关问题