2014-11-05 1 views
1

sqlite 데이터베이스를 사용하여 Windows Phone 앱을 개발 중입니다. 데이터베이스를 보여주고 삭제할 특정 행을 삭제할 수 있습니다.하지만 문제는 이후입니다. 행을 선택하고 그 행이 사라지지 않는 삭제를 클릭하십시오. 삭제 된 페이지를 임차해야합니다. 모든 주요 기능 아래행을 삭제 한 후 화면에서 사라지지 않습니다.

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(); 
      //}); 
     } 
    } 

와 클래스는 모든 테이블 클래스 여기 아래 내가 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 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 구조입니다.

목록에 삭제하고 삽입해도 ListBox의 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

다시 나를 위해 당신의 시간을 주셔서 감사 할 수있다! 도움이 필요하면 목록을 사용하는 것 이외의 대안은 무엇입니까 –

+0

솔루션을 한 번 더 다시 읽으십시오. 다른 방법은 없습니다. 목록 상자를 관찰 가능한 컬렉션에 직접 연결하십시오. –

관련 문제